how to pass new instance of FirefoxDriver to each new thread?
Question
public class Tester implements Runnable {
public Tester() {
// Init WebDriver
FirefoxProfile firefoxProfile = new FirefoxProfile();
WebDriver browser1 = new FirefoxDriver(firefoxProfile);
WebDriver browser2 = new FirefoxDriver(firefoxProfile);
}
public static void main(String[] args) {
Runnable tester = new Tester();
Thread worker1 = new Thread(tester);
Thread worker2 = new Thread(tester);
worker1.start();
worker2.start();
}
public void run(WebDriver driver) {
login(driver, "username", "password", "http://someurl.com/login");
}
}
I am trying to pass the driver argument to run() method, but does it take arguments ? Where do I pass the browser1 and browser 2 ?
My end goal is to have multiple instances of firefox browser running the same tests.
Solution
Add a constructor, that takes the browser as an argument. Store the reference in a private field and you can use the Browser in the run method:
public class Tester implements Runnable {
private WebDriver browser;
public Tester(WebDriver browser) {
this.browser = browser;
}
public static void main(String[] args) {
Thread worker1 = new Thread(new Tester(new FirefoxDriver(firefoxProfile)));
Thread worker2 = new Thread(new Tester(new FirefoxDriver(firefoxProfile)));
worker1.start();
worker2.start();
}
public void run() {
login(browser, "username", "password", "http://someurl.com/login");
}
}
OTHER TIPS
run()
method in thread does not have any parameters.
You can do it using 'setters' on your implementation of thread. With this your code should look like -
Tester tester = new Tester();
tester.setBrowser1(new FirefoxDriver( ....
tester.setBrowser2(new FirefoxDriver( ....
Thread worker2 = new Thread(tester);
your Tester will have browser1 and browser2 as instance variables. Or you may also set browser1 and browser2 through the constructor of Tester.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow