Parallel Execution in Selenium
โก Smart Summary
Sessions, parallel run, and dependency in Selenium combine a unique sessionId per WebDriver instance with TestNG suite controls, letting many browser windows run independent or ordered scripts at once and finish the regression cycle faster.

To understand how to run scripts in parallel, you first need to know how Selenium isolates each browser through sessions.
Why do we need Session Handling?
During test execution, the Selenium WebDriver has to interact with the browser at every step to execute given commands. While that test runs, another user may launch a second script on the same machine and the same type of browser.
In such a situation, we need a mechanism so the two executions do not overlap. Selenium solves this through session handling.
How to achieve Session Handling in Selenium WebDriver?
If you inspect the Selenium WebDriver source, you will find a variable called sessionId. Each time you create a new WebDriver instance, a fresh sessionId is generated and attached to that Firefox, Chrome, or IE driver.
Every subsequent command runs only inside that particular browser session.
Because this is built into WebDriver, there is no need to assign the sessionId manually.
Code Example: Two different sessions are generated for two WebDriver instances.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SessionHandling { public static void main(String... strings) { // First session of WebDriver WebDriver driver = new FirefoxDriver(); // Go to guru99 demo site driver.get("https://demo.guru99.com/V4/"); // Second session of WebDriver WebDriver driver2 = new FirefoxDriver(); // Go to guru99 demo site driver2.get("https://demo.guru99.com/V4/"); } }
How to run Parallel Tests with Selenium
There are situations where you want to run multiple tests at the same time, such as cross-browser checks or large smoke suites.
In such cases, the parallel attribute on the TestNG suite tag does the work.
The parallel attribute of the suite tag accepts four values:
| Attribute value | Meaning |
|---|---|
| tests | All test cases inside a <test> tag of the testing XML file run in parallel. |
| classes | All test cases inside a Java class run in parallel. |
| methods | All methods annotated with @Test execute in parallel. |
| instances | Tests in the same instance run in parallel, while two methods of two different instances run in different threads. |
The thread-count attribute decides how many threads are allocated for that execution. Pair it with the TestNG framework and install TestNG in Eclipse before running. For scale beyond a single machine, route the same suite through Selenium Grid 4 for distributed parallel execution.
Complete Example: three test cases run in parallel and fill login data on https://demo.guru99.com/.
The complete project looks like:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class TestGuru99MultipleSession { @Test public void executeSessionOne() { // First session of WebDriver System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://demo.guru99.com/V4/"); // Find the user name text box and fill it driver.findElement(By.name("uid")).sendKeys("Driver 1"); } @Test public void executeSessionTwo() { // Second session of WebDriver System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://demo.guru99.com/V4/"); driver.findElement(By.name("uid")).sendKeys("Driver 2"); } @Test public void executeSessionThree() { // Third session of WebDriver System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://demo.guru99.com/V4/"); driver.findElement(By.name("uid")).sendKeys("Driver 3"); } }
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods"> <test name="testGuru"> <classes> <class name="TestGuru99MultipleSession"/> </classes> </test> </suite>
Test Case order and Dependency
You can set the order and dependency of test case execution. Suppose you have two test cases, testGuru99TC1 and testGuru99TC2, and you want testGuru99TC2 to run before testGuru99TC1. Use the dependsOnMethods attribute (or dependsOnGroups for group-level chaining) to fix that order. For wider automation framework design, compare JUnit vs TestNG, and review the findElement reference if your dependent tests share locators.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods"> <test name="testGuru"> <classes> <class name="TestGuru99MultipleSession"> <include name="testGuru99TC1" dependsOnMethods="testGuru99TC2"/> <include name="testGuru99TC2"/> </class> </classes> </test> </suite>
The TestNG runtime, hosted at testng.org, evaluates these declarations before launching threads, so dependent methods always start after their prerequisites finish.





