tags:

views:

79

answers:

2
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.

A: 

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

}
Andreas_D
This opens up the first thread's browser, but nothing happens. I close it, and the second thread's browser opens up, and things work as they should. How can I have both instances running concurrently ?
Kim Jong Woo
ah giving separate profiles for each instances, solved the problem.
Kim Jong Woo
+1  A: 

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.

Gopi