1

I was asked in an interview whether Selenium Webdriver supported parameterized constructors.

Example:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public   class Cons {
        public  String username="u";
        public  String pswd="p";
        public  String baseurl="url";
        public  WebDriver d;

public  Cons(String username, String pswd){
            d = new FirefoxDriver();
            d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            d.get(baseurl);
                d.findElement(By.name("username")).sendKeys(this.username);
                d.findElement(By.name("pwd")).sendKeys(this.pswd);
              }
 }
  • sqa.stackexchange.com/editing-help – user246 Feb 21 '14 at 12:40
  • why would you want this? – Erki M. Feb 23 '14 at 20:54
  • Hello Erki, I was faced this question in interview. – QA4it Feb 24 '14 at 8:48
  • @QA4it - I edited the question to mention that you were asked this in an interview. – Kate Paulk Mar 18 '14 at 11:15
  • @Kate Paulk - Ok, sir. – QA4it Mar 18 '14 at 11:19
2

Yes, We can write webdriver code in constructor as well. I tried to create an example with your given code. Hope this helps !

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; 
import   org.openqa.selenium.firefox.FirefoxDriver;

public class Test { 

public String baseurl="http://aavtrain.com/";
public WebDriver d;

public Test(String username, String pswd)
{ 
d = new FirefoxDriver();
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.get(baseurl); 
d.findElement(By.name("user_name")).sendKeys(username);
d.findElement(By.name("password")).sendKeys(pswd);
}

  public static void main(String aregs[])
  {
    Test t=new Test("abc","cde");
  }
}
| improve this answer | |
  • This is GREAT answer and solution for me my friend... Really very thank to you.. AWESOME :) ... – QA4it Feb 21 '14 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.