Bird
Raised Fist0
Selenium Javatesting~10 mins

Parallel execution configuration in Selenium Java - Test Execution Trace

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of configuring parallel execution in Selenium with TestNG?
easy
A. To run multiple tests at the same time and save execution time
B. To run tests one after another in a fixed order
C. To increase the number of test cases manually
D. To disable test execution temporarily

Solution

  1. Step 1: Understand parallel execution concept

    Parallel execution means running tests simultaneously instead of sequentially.
  2. Step 2: Identify the benefit of parallel execution

    Running tests at the same time saves overall test execution time.
  3. Final Answer:

    To run multiple tests at the same time and save execution time -> Option A
  4. Quick Check:

    Parallel execution = run tests simultaneously [OK]
Hint: Parallel means running tests together to save time [OK]
Common Mistakes:
  • Thinking parallel means running tests one by one
  • Confusing parallel execution with increasing test count
  • Assuming parallel disables tests
2. Which XML attribute correctly sets the number of threads for parallel execution in TestNG?
easy
A. thread-count
B. parallel-count
C. max-threads
D. execution-threads

Solution

  1. Step 1: Recall TestNG XML attributes for parallelism

    TestNG uses thread-count to specify how many threads run tests in parallel.
  2. Step 2: Verify other options are incorrect

    Attributes like parallel-count, max-threads, and execution-threads are not valid TestNG attributes.
  3. Final Answer:

    thread-count -> Option A
  4. Quick Check:

    Thread count = number of parallel threads [OK]
Hint: Use thread-count to set parallel threads in TestNG XML [OK]
Common Mistakes:
  • Using wrong attribute names like parallel-count
  • Confusing thread-count with parallel attribute
  • Misspelling thread-count
3. Given this TestNG XML snippet, how many tests will run in parallel?
<suite name="Suite" parallel="tests" thread-count="3">
  <test name="Test1"></test>
  <test name="Test2"></test>
  <test name="Test3"></test>
  <test name="Test4"></test>
</suite>
medium
A. All 4 tests run in parallel
B. 3 tests run in parallel, 1 waits
C. Only 1 test runs at a time
D. 2 tests run in parallel, 2 wait

Solution

  1. Step 1: Understand thread-count and parallel attribute

    parallel="tests" means tests run in parallel. thread-count="3" limits max parallel threads to 3.
  2. Step 2: Apply thread-count to number of tests

    There are 4 tests but only 3 threads, so 3 tests run simultaneously and 1 waits.
  3. Final Answer:

    3 tests run in parallel, 1 waits -> Option B
  4. Quick Check:

    thread-count limits parallel tests to 3 [OK]
Hint: thread-count limits max parallel tests running [OK]
Common Mistakes:
  • Assuming all tests run parallel ignoring thread-count
  • Confusing parallel="tests" with parallel="methods"
  • Ignoring that thread-count caps parallelism
4. You configured parallel="methods" and thread-count="2" in TestNG XML, but tests still run sequentially. What is the likely cause?
medium
A. TestNG does not support parallel execution
B. thread-count must be set to 1 for parallel methods
C. Test methods share state and are not thread-safe
D. parallel attribute should be set to "tests" for methods

Solution

  1. Step 1: Understand parallel methods and thread safety

    parallel="methods" runs test methods in parallel only if they are thread-safe and independent.
  2. Step 2: Identify cause of sequential execution

    If tests share data or state, TestNG runs them sequentially to avoid conflicts.
  3. Final Answer:

    Test methods share state and are not thread-safe -> Option C
  4. Quick Check:

    Shared state blocks parallel method execution [OK]
Hint: Ensure test methods are independent for parallel execution [OK]
Common Mistakes:
  • Setting thread-count to 1 disables parallelism
  • Using wrong parallel attribute value
  • Believing TestNG lacks parallel support
5. You want to run test classes in parallel but ensure each class uses its own WebDriver instance to avoid conflicts. Which TestNG XML configuration is best?
hard
A. <suite name="Suite" parallel="instances" thread-count="4">
B. <suite name="Suite" parallel="tests" thread-count="4">
C. <suite name="Suite" parallel="methods" thread-count="4">
D. <suite name="Suite" parallel="classes" thread-count="4">

Solution

  1. Step 1: Understand parallel modes in TestNG

    parallel="classes" runs test classes in parallel, isolating each class's WebDriver instance.
  2. Step 2: Match requirement to configuration

    Running classes in parallel with thread-count=4 allows 4 classes to run simultaneously, each with its own driver.
  3. Final Answer:

    <suite name="Suite" parallel="classes" thread-count="4"> -> Option D
  4. Quick Check:

    parallel="classes" isolates WebDriver per class [OK]
Hint: Use parallel="classes" for separate WebDriver per class [OK]
Common Mistakes:
  • Using parallel="methods" causing shared driver issues
  • Choosing parallel="tests" which runs test tags, not classes
  • Using parallel="instances" which is less common and may confuse
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard