开发者

how to pass new instance of FirefoxDriver to each new thread?

public class Tester implements Runnable {
    public Tester() {
        // Init WebDriver
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        WebDriver browser1 = new FirefoxDriver(firefoxProfile);
        WebDriver 开发者_C百科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.


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.


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜