Parallel execution configuration in Selenium Java - Build an Automation Script
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 LoginTest { private WebDriver driver; @BeforeMethod public void setUp() { driver = new ChromeDriver(); } @Test public void testLogin() { driver.get("https://example.com/login"); String threadName = Thread.currentThread().getName(); System.out.println("LoginTest running on thread: " + threadName); Assert.assertEquals(driver.getTitle(), "Login Page"); } @AfterMethod public void tearDown() { if (driver != null) { driver.quit(); } } } // Another test class 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 SearchTest { private WebDriver driver; @BeforeMethod public void setUp() { driver = new ChromeDriver(); } @Test public void testSearch() { driver.get("https://example.com/search"); String threadName = Thread.currentThread().getName(); System.out.println("SearchTest running on thread: " + threadName); Assert.assertTrue(driver.getTitle().contains("Search")); } @AfterMethod public void tearDown() { if (driver != null) { driver.quit(); } } } /* TestNG XML suite file (testng.xml): <?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="LoginTest"> <classes> <class name="LoginTest"/> </classes> </test> <test name="SearchTest"> <classes> <class name="SearchTest"/> </classes> </test> </suite> */
The solution includes two separate test classes: LoginTest and SearchTest. Each class creates its own WebDriver instance in the @BeforeMethod and closes it in @AfterMethod to avoid sharing drivers between threads.
The TestNG XML suite file configures parallel execution by setting parallel="tests" and thread-count="2". This means TestNG will run the two test classes at the same time in separate threads.
Inside each test, we print the current thread name to the console to confirm parallel execution. Assertions check the page titles to verify the tests run correctly.
This setup follows best practices by isolating WebDriver instances per test and using TestNG's built-in parallel execution features.
Now add data-driven testing to LoginTest with 3 different username and password combinations
Practice
Solution
Step 1: Understand parallel execution concept
Parallel execution means running tests simultaneously instead of sequentially.Step 2: Identify the benefit of parallel execution
Running tests at the same time saves overall test execution time.Final Answer:
To run multiple tests at the same time and save execution time -> Option AQuick Check:
Parallel execution = run tests simultaneously [OK]
- Thinking parallel means running tests one by one
- Confusing parallel execution with increasing test count
- Assuming parallel disables tests
Solution
Step 1: Recall TestNG XML attributes for parallelism
TestNG usesthread-countto specify how many threads run tests in parallel.Step 2: Verify other options are incorrect
Attributes likeparallel-count,max-threads, andexecution-threadsare not valid TestNG attributes.Final Answer:
thread-count -> Option AQuick Check:
Thread count = number of parallel threads [OK]
- Using wrong attribute names like parallel-count
- Confusing thread-count with parallel attribute
- Misspelling thread-count
<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>
Solution
Step 1: Understand thread-count and parallel attribute
parallel="tests" means tests run in parallel. thread-count="3" limits max parallel threads to 3.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.Final Answer:
3 tests run in parallel, 1 waits -> Option BQuick Check:
thread-count limits parallel tests to 3 [OK]
- Assuming all tests run parallel ignoring thread-count
- Confusing parallel="tests" with parallel="methods"
- Ignoring that thread-count caps parallelism
parallel="methods" and thread-count="2" in TestNG XML, but tests still run sequentially. What is the likely cause?Solution
Step 1: Understand parallel methods and thread safety
parallel="methods" runs test methods in parallel only if they are thread-safe and independent.Step 2: Identify cause of sequential execution
If tests share data or state, TestNG runs them sequentially to avoid conflicts.Final Answer:
Test methods share state and are not thread-safe -> Option CQuick Check:
Shared state blocks parallel method execution [OK]
- Setting thread-count to 1 disables parallelism
- Using wrong parallel attribute value
- Believing TestNG lacks parallel support
Solution
Step 1: Understand parallel modes in TestNG
parallel="classes" runs test classes in parallel, isolating each class's WebDriver instance.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.Final Answer:
<suite name="Suite" parallel="classes" thread-count="4"> -> Option DQuick Check:
parallel="classes" isolates WebDriver per class [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
