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);
}
}
asked Feb 21, 2014 at 10:41
-
sqa.stackexchange.com/editing-helpuser246– user2462014年02月21日 12:40:36 +00:00Commented Feb 21, 2014 at 12:40
-
why would you want this?Erki M.– Erki M.2014年02月23日 20:54:23 +00:00Commented Feb 23, 2014 at 20:54
-
Hello Erki, I was faced this question in interview.QA4it– QA4it2014年02月24日 08:48:47 +00:00Commented Feb 24, 2014 at 8:48
-
@QA4it - I edited the question to mention that you were asked this in an interview.Kate Paulk– Kate Paulk2014年03月18日 11:15:56 +00:00Commented Mar 18, 2014 at 11:15
-
@Kate Paulk - Ok, sir.QA4it– QA4it2014年03月18日 11:19:19 +00:00Commented Mar 18, 2014 at 11:19
1 Answer 1
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");
}
}
-
This is GREAT answer and solution for me my friend... Really very thank to you.. AWESOME :) ...QA4it– QA4it2014年02月21日 16:59:30 +00:00Commented Feb 21, 2014 at 16:59
default