Is parameterized constructor (Java) supported in selenium webdriver?



  • 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);
                  }
     }
    


  • 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");
      }
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2