Interview #101: Selenium - How to run tests in parallel?

Interview #101: Selenium - How to run tests in parallel?

Running Selenium tests in parallel is essential for improving test execution speed and efficiency, especially when dealing with large test suites. There are multiple ways to achieve parallel execution in Selenium, depending on the framework and tools used. Below are the common approaches:

Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245

1. Using TestNG for Parallel Execution

TestNG is one of the most commonly used frameworks for running parallel tests in Selenium. It provides built-in support for parallel execution through its XML configuration.

Steps to Run Selenium Tests in Parallel using TestNG:

  1. Define Parallel Execution in testng.xml

TestNG allows parallel execution at different levels:

  • Methods (parallel="methods")
  • Classes (parallel="classes")
  • Tests (parallel="tests")
  • Instances (parallel="instances")

Example testng.xml file for running test classes in parallel:

<suite name="ParallelSuite" parallel="classes" thread-count="3">
    <test name="ChromeTest">
        <classes>
            <class name="com.tests.ChromeTest" />
        </classes>
    </test>

    <test name="FirefoxTest">
        <classes>
            <class name="com.tests.FirefoxTest" />
        </classes>
    </test>

    <test name="EdgeTest">
        <classes>
            <class name="com.tests.EdgeTest" />
        </classes>
    </test>
</suite>        

Here, thread-count="3" means that up to three threads will execute in parallel.

  1. Modify Test Classes to Support Parallel Execution Each test class should launch and quit the WebDriver instance separately:

public class ChromeTest {
    WebDriver driver;

    @BeforeClass
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testGoogleSearch() {
        driver.get("https://www.google.com");
        Assert.assertEquals(driver.getTitle(), "Google");
    }

    @AfterClass
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}        

This ensures that each test has its own WebDriver instance.


2. Using Selenium Grid for Parallel Execution

Selenium Grid is a tool that allows tests to be executed in parallel on multiple machines (nodes) connected to a central hub.

Steps to Set Up Parallel Execution with Selenium Grid:

  1. Start the Selenium Server Hub Run the following command to start the hub:

java -jar selenium-server-4.x.x.jar hub        

  1. Register Nodes to the Hub Start multiple browser nodes with:

java -jar selenium-server-4.x.x.jar node --hub https://localhost:4444        

  1. Modify Your Test Code to Use RemoteWebDriver Example of running tests on different browsers using Selenium Grid:

public class GridTest {
    WebDriver driver;

    @BeforeClass
    @Parameters("browser")
    public void setup(String browser) throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();

        if (browser.equalsIgnoreCase("chrome")) {
            capabilities.setBrowserName("chrome");
        } else if (browser.equalsIgnoreCase("firefox")) {
            capabilities.setBrowserName("firefox");
        }

        driver = new RemoteWebDriver(new URL("https://localhost:4444"), capabilities);
    }

    @Test
    public void testTitle() {
        driver.get("https://www.google.com");
        Assert.assertEquals(driver.getTitle(), "Google");
    }

    @AfterClass
    public void teardown() {
        driver.quit();
    }
}        

This test will run on different browsers in parallel using Selenium Grid.


3. Using JUnit 5 with Parallel Execution

JUnit 5 provides a way to enable parallel execution via its configuration.

Steps to Enable Parallel Execution in JUnit 5:

  1. Modify the junit-platform.properties File Add the following configuration:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent        

  1. Run Test Classes in Parallel Example:

@ExtendWith(SeleniumExtension.class)
public class ParallelJUnitTest {
    private WebDriver driver;

    @BeforeEach
    void setup() {
        driver = new ChromeDriver();
    }

    @Test
    void testGoogleSearch() {
        driver.get("https://www.google.com");
        Assertions.assertEquals("Google", driver.getTitle());
    }

    @AfterEach
    void teardown() {
        driver.quit();
    }
}        

This enables JUnit to run tests concurrently.


4. Using Maven Surefire & Failsafe Plugins

For Maven-based projects, the maven-surefire-plugin or maven-failsafe-plugin can be used for parallel execution.

Steps to Enable Parallel Execution in Maven:

  1. Configure pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <parallel>methods</parallel>
                <threadCount>3</threadCount>
            </configuration>
        </plugin>
    </plugins>
</build>        

  1. Run Tests with Maven

mvn test        

This will run tests in parallel using multiple threads.


Conclusion

Parallel execution of Selenium tests significantly reduces execution time and enhances test efficiency. The best approach depends on your project needs:

  • TestNG is the easiest for parallel execution within a single machine.
  • Selenium Grid is best for distributing tests across multiple machines.
  • JUnit 5 offers built-in parallelism with modern configuration.
  • Maven Surefire Plugin integrates well with CI/CD pipelines.

Would you like help implementing parallel execution for your specific setup? ??


Mohd Fahad

Staff Engineer at Nagarro || Selenium || playwright || API Automaion-RestAssured || Core Java || Basic JS || TestNG || BDD Cucumber || Junit || Jenkins

1 周

Very informative

回复
Vladimir Golendukhin

Manager of Application Systems

1 周

5. Running tests in CI/CD like bamboo on more than one agent

回复

要查看或添加评论,请登录

Software Testing Studio | WhatsApp 91-9606623245的更多文章

社区洞察