Parallel Execution in Selenium Grid with Docker – Easy steps to setup

Introduction

In today’s fast-paced development environments, test automation engineers face increasing pressure to execute large suites of tests quickly and efficiently. A key solution to speeding up test execution is parallel execution, particularly using Selenium Grid with docker. Selenium Grid allows you to distribute and run tests on multiple machines, browsers, and platforms simultaneously, drastically reducing the time needed for test runs.

In this blog post, we’ll dive deep into the what, why, and how of implementing parallel execution in Selenium Grid with docker, along with code snippets and best practices for test automation engineers to maximize test efficiency.

What is Selenium Grid?

Selenium Grid is a part of the Selenium suite designed for running multiple tests across different browsers, operating systems, and machines in parallel. It uses a hub-node architecture where:

• The hub manages the test execution and distributes the tests to different nodes.

• The nodes are where the tests are executed, each node can run tests on different browsers and OS combinations.

By leveraging Selenium Grid, we can execute tests concurrently across different environments, helping reduce the overall test time significantly.

Why Use Parallel Execution in Selenium Grid with Docker?

When working with large test suites, sequential execution can become time-consuming, especially when testing across multiple browser versions or operating systems. Parallel execution allows:

• Faster feedback: Shorter test execution time leads to quicker feedback loops.

• Cross-browser testing: Testing on different browsers simultaneously ensures your application works across all supported platforms.

• Efficient use of resources: Running tests on different machines concurrently maximizes hardware utilization.

• Cost reduction: Parallel testing reduces the need for additional infrastructure or test environments.

How to Implement Parallel Execution in Selenium Grid with Docker

Step 1: Setting Up Selenium Grid with Docker

The first step is to set up Selenium Grid using a Hub and one or more Nodes. You can do this using Docker containers or on physical/virtual machines.

Setting Up Hub

You can start the Selenium Grid hub by running the following command:

docker run -d -p 4444:4444 --name selenium-hub selenium/hub

Setting Up Nodes

To start a node that connects to the hub, you can use this command:

docker run -d --link selenium-hub:hub selenium/node-chrome
docker run -d --link selenium-hub:hub selenium/node-firefox

These commands create Chrome and Firefox nodes and link them to the hub.

Step 2: Configuring Parallel Execution in TestNG

Now that we have our Grid setup, let’s configure TestNG for parallel execution. TestNG offers a simple way to run tests in parallel using its XML configuration file.

Example TestNG XML for Parallel Execution

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ParallelTests" parallel="tests" thread-count="4">
    <test name="ChromeTests">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="com.example.tests.ChromeTest"/>
        </classes>
    </test>
    <test name="FirefoxTests">
        <parameter name="browser" value="firefox"/>
        <classes>
            <class name="com.example.tests.FirefoxTest"/>
        </classes>
    </test>
</suite>

In the above XML file:

• parallel=”tests” instructs TestNG to run each <test> in parallel.

• thread-count=”4″ limits the number of parallel threads to 4.

• parameter tags allow you to pass browser-specific parameters to your tests.

Step 3: Writing the Selenium Test for Parallel Execution

You’ll need to adjust your test code to support parallel execution by passing the browser type and configuring WebDriver accordingly.

Example Code for Parallel Execution

public class BaseTest {
    protected WebDriver driver;

    @Parameters("browser")
    @BeforeMethod
    public void setUp(String browser) {
        if (browser.equalsIgnoreCase("chrome")) {
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions());
        } else if (browser.equalsIgnoreCase("firefox")) {
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new FirefoxOptions());
        }
    }

    @Test
    public void exampleTest() {
        driver.get("https://example.com");
        Assert.assertEquals(driver.getTitle(), "Example Domain");
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

• The @Parameters(“browser”) annotation allows TestNG to pass the browser parameter to your test.

• The RemoteWebDriver is used to interact with Selenium Grid, where the URL points to the hub.

Best Practices for Parallel Execution in Selenium Grid With Docker

1. Optimize Thread Count: Setting too many threads can overwhelm your Grid, leading to timeouts or slowdowns. Monitor your hardware and adjust thread-count in your TestNG XML.

2. Separate Test Data: Ensure your tests do not rely on shared resources (like databases or files) to avoid data corruption when running tests in parallel.

3. Stateless Tests: Always ensure your tests are stateless and independent of each other to prevent race conditions.

4. Use Assertions Wisely: Avoid long chains of assertions in a single test method. Break them into smaller, isolated tests for better test reporting and execution speed.

Conclusion

By implementing parallel execution in Selenium Grid with docker, test automation engineers can drastically improve the efficiency of their test suites. Leveraging this technique not only reduces overall execution time but also ensures your application is thoroughly tested across multiple environments.

Adopting best practices like optimizing thread count, ensuring stateless tests, and configuring your CI/CD pipeline for parallel execution will maximize the benefits of Selenium Grid.

If you’re looking to speed up your test cycles and deliver faster, more reliable results, parallel execution in Selenium Grid is a must-have in your automation toolkit.

By following these steps, you can harness the power of parallel execution in Selenium Grid to streamline your testing process and keep pace with the demands of modern software development.


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts