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);
              }
 }
7
  • sqa.stackexchange.com/editing-help Commented Feb 21, 2014 at 12:40
  • why would you want this? Commented Feb 23, 2014 at 20:54
  • Hello Erki, I was faced this question in interview. Commented Feb 24, 2014 at 8:48
  • @QA4it - I edited the question to mention that you were asked this in an interview. Commented Mar 18, 2014 at 11:15
  • @Kate Paulk - Ok, sir. Commented Mar 18, 2014 at 11:19

1 Answer 1

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");
  }
}
1
  • This is GREAT answer and solution for me my friend... Really very thank to you.. AWESOME :) ... Commented Feb 21, 2014 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.