Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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);
              }
 }
share|improve this question
    
    
why would you want this? –  Erki M. Feb 23 at 20:54
    
Hello Erki, I was faced this question in interview. –  QA4it Feb 24 at 8:48
    
@QA4it - I edited the question to mention that you were asked this in an interview. –  Kate Paulk Mar 18 at 11:15
    
@Kate Paulk - Ok, sir. –  QA4it Mar 18 at 11:19

1 Answer 1

up vote 0 down vote accepted

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");
  }
}
share|improve this answer
    
This is GREAT answer and solution for me my friend... Really very thank to you.. AWESOME :) ... –  QA4it Feb 21 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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