0
0
Selenium Javatesting~10 mins

Test parallelization in CI in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs two simple Selenium tests in parallel using TestNG in a Continuous Integration (CI) environment. It verifies that both tests execute independently and pass without interference.

Test Code - TestNG
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelTest {
    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test(timeOut = 10000)
    public void testGoogleTitle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        Assert.assertEquals(title, "Google");
    }

    @Test(timeOut = 10000)
    public void testBingTitle() {
        driver.get("https://www.bing.com");
        String title = driver.getTitle();
        Assert.assertEquals(title, "Bing");
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 10 Steps
StepActionSystem StateAssertionResult
1TestNG starts test suite with parallel execution enabledTestNG initializes two test methods to run in parallel threads-PASS
2First thread runs testGoogleTitle: opens Chrome browserChrome browser instance 1 opens at blank page-PASS
3Second thread runs testBingTitle: opens Chrome browserChrome browser instance 2 opens at blank page-PASS
4First thread navigates to https://www.google.comBrowser instance 1 shows Google homepagePage title is 'Google'PASS
5Second thread navigates to https://www.bing.comBrowser instance 2 shows Bing homepagePage title is 'Bing'PASS
6First thread asserts page title equals 'Google'Title retrieved from browser instance 1Assert.assertEquals(title, "Google")PASS
7Second thread asserts page title equals 'Bing'Title retrieved from browser instance 2Assert.assertEquals(title, "Bing")PASS
8First thread closes browser instance 1Browser instance 1 closed-PASS
9Second thread closes browser instance 2Browser instance 2 closed-PASS
10TestNG completes all parallel test executionsAll tests finished with no failuresAll assertions passedPASS
Failure Scenario
Failing Condition: If the browser driver is not thread-safe or shared incorrectly, tests interfere causing wrong page titles or exceptions
Execution Trace Quiz - 3 Questions
Test your understanding
What ensures that the two tests run at the same time in this TestNG setup?
ACalling driver.get() twice in the same test
BEnabling parallel="methods" with thread-count="2" in TestNG suite configuration
CUsing @BeforeMethod and @AfterMethod annotations
DRunning tests sequentially in one thread
Key Result
Always create and quit a separate WebDriver instance per test method when running tests in parallel to avoid conflicts and ensure reliable results.