Skip to content
Advertisement

How to execute selenium script in a lot of threads?

I like to test my site in a lot of threads. But when I trying to do that, I see one problem. All motions when I like are happening in last opened window. So, the first window just stuck in background.

public class Core extends Thread{

    private static FirefoxDriver firefoxDriver;

    public Core(){
        firefoxDriver = new FirefoxDriver();
    }

    @Override
    public void run() {
        firefoxDriver.get("https://google.com/");
        firefoxDriver.close();
    }
}

public class Main {
  
    public static void main(String[] args) throws AWTException {
        
        System.setProperty("webdriver.gecko.driver", "/home/riki/Downloads/geckodriver-v0.30.0-linux64/geckodriver");

        Core core = new Core();
        Core core2 = new Core();

        core.start();    // This thread is stuck in back
        core2.start();   // This thread goes to google.com twice
    }
}
   

I really don’t understand why it happens. You can see it here. After that the code has run, the first window keeps hanging in back. It don’t close. When the second thread closes after executing code

Advertisement

Answer

This is because of you using static field for Forefox driver.

Static means the one per all instances. So remove static here.

private FirefoxDriver firefoxDriver;

and after, each thread will use it’s own firefoxDriver field.

Static fields should be used carefully if you going to modify them.

Advertisement