0
0
Selenium Javatesting~10 mins

Parallel execution configuration in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to configure and run Selenium tests in parallel using TestNG. It verifies that two tests run simultaneously on different browsers and both pass successfully.

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

public class ParallelTest {
    private WebDriver driver;

    @BeforeMethod
    @Parameters({"browser"})
    public void setUp(String browser) {
        if (browser.equalsIgnoreCase("chrome")) {
            driver = new ChromeDriver();
        } else if (browser.equalsIgnoreCase("firefox")) {
            driver = new FirefoxDriver();
        }
        driver.manage().window().maximize();
    }

    @Test
    public void openGoogle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        Assert.assertTrue(title.contains("Google"));
    }

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

/* TestNG XML configuration for parallel execution:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="ParallelSuite" parallel="tests" thread-count="2">
  <test name="ChromeTest">
    <parameter name="browser" value="chrome"/>
    <classes>
      <class name="ParallelTest"/>
    </classes>
  </test>
  <test name="FirefoxTest">
    <parameter name="browser" value="firefox"/>
    <classes>
      <class name="ParallelTest"/>
    </classes>
  </test>
</suite>
*/
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1TestNG suite starts with parallel tests configured for Chrome and Firefox browsersTestNG initializes two test threads for ChromeTest and FirefoxTest-PASS
2ChromeTest thread calls setUp with parameter 'chrome', ChromeDriver instance is created and browser window maximizedChrome browser window is open and ready-PASS
3FirefoxTest thread calls setUp with parameter 'firefox', FirefoxDriver instance is created and browser window maximizedFirefox browser window is open and ready-PASS
4ChromeTest thread navigates to https://www.google.comChrome browser displays Google homepagePage title contains 'Google'PASS
5FirefoxTest thread navigates to https://www.google.comFirefox browser displays Google homepagePage title contains 'Google'PASS
6ChromeTest thread calls tearDown and closes Chrome browserChrome browser is closed-PASS
7FirefoxTest thread calls tearDown and closes Firefox browserFirefox browser is closed-PASS
8TestNG suite completes with both tests passing in parallelNo browsers open, test suite finishedBoth tests passed successfullyPASS
Failure Scenario
Failing Condition: If the browser driver executable is missing or the browser fails to launch
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @Parameters annotation do in this test?
AIt closes the browser after the test
BIt passes the browser name to the setUp method to decide which browser to launch
CIt runs the test only once regardless of browser
DIt maximizes the browser window
Key Result
Configuring parallel execution in TestNG requires setting parameters in the XML suite file and using parameterized setup methods to launch different browsers simultaneously, improving test speed and coverage.