Matt
Matt

Reputation: 1033

How to instantiate Remote Web Driver so I can run tests in parallel using Java and Selenium with Serenity

I have written a Test Automation Framework using Java, Selenium, Cucumber, Serenity and Feature Files that is working fine when the tests are ran one at a time.

I would like to be able to run the tests in parallel but when I attempt this, several browsers open but only one at a time ever has anything happening in it - most of the tests fail.

I think this is because of the way I am declaring and instantiating the Remote Web Driver.

I have a base class (FrameworkInitialize) where I declare a driver as a public static RemoteWebDriver object as shown below:

public class FrameworkInitialize {
    public static RemoteWebDriver driver;
}

All other classes call this base class.

The reason for declaring driver as public static is that 'driver' could then be used anywhere within the solution without passing it around.

However, I think this is causing problems when trying to run in parallel - I believe it is bad practice to declare your driver as static.

I have another class called StartLocalWebBrowser where the driver is instantiated as shown below:


public class StartLocalWebBrowser extends FrameworkInitialize {

    public static void StartBrowser(String browserType) {
        {

            switch (browserType) {

                case "chrome": {
                    System.setProperty("webdriver.chrome.driver", properties.getProperty("ChromeDriver"));
                    ChromeOptions chromeOptions = new ChromeOptions();

                    driver = new ChromeDriver(chromeOptions);
                    break;
                }

                case "firefox": {
                    System.setProperty("webdriver.gecko.driver", properties.getProperty("GeckoDriver"));
                    driver = new FirefoxDriver();
                    break;

        }
    }

}

So, what is the correct way to change my code so that it will work in parallel?

Upvotes: 0

Views: 779

Answers (2)

Matt
Matt

Reputation: 1033

I have found a quick solution to this:

In the POM, in the maven-failsafe-plugin section I have added the following:

<forkCount>5</forkCount>
<reuseForks>true</reuseForks>

And I have also added config for cucumber-jvm-parallel-plugin in the POM.

The tests are now running in parallel.

Not best practice having driver declared as static but it is working and saves me having to pass then driver object around the code.

Upvotes: 1

Alexey R.
Alexey R.

Reputation: 8676

Basically how you instantiate your WebDriver is important indeed but it is worth thinking out a lot more other things from architecture standpoint which, having poorly developed, might make results of your tests useless.

RemoteWebDriver will let you prallelize one test by running it on several browsers. As far as I understand you want to run several tests simultaneously. Thus your second option is more relevant. However I would recommend to use JUnit/TestNG frameworks to run your tests and Maven Surefire plugin that supports several ways of parallelization.

In order not to care much about race conditions thread safety and synchronization I would recommend to distribute your tests among different test classes and use "fork" mode so that Maven Surefire will run your test classes in different JVMs where inside test methods would be running one-by-one as in your regular implementation.

P.S. - When you run tests in parallel you should also be aware of that your tests share the resources of your test environment (not only the objects in your code or whatever). So race conditions might occur when they attempt to perform actions which for example change database entries, etc. This decreases the isolation level of each your particular test within the environment.

Upvotes: 2

Related Questions