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.

  • ๐Ÿงฉ Key Principle: Every new WebDriver instance generates a fresh sessionId, so two drivers never share commands or cookies on the same machine.
  • โš™๏ธ Implementation: Set parallel=”methods” (or tests, classes, instances) plus thread-count on the TestNG suite tag of testng.xml to fan out execution.
  • ๐Ÿงต Parallelism: Use one WebDriver per @Test method to avoid race conditions on shared browser windows during concurrent runs.
  • ๐Ÿ”— Dependency: Order tests with dependsOnMethods inside testng.xml include tags or the @Test annotation when one method must precede another.
  • ๐Ÿค– AI Use: Modern AI helpers predict optimal thread-count, group safe parallel methods, and surface flaky candidates from historical run data.

How to Run Parallel Execution in Selenium

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.

Session Handling

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.

Achieve Session Handling in Selenium WebDriver

Every subsequent command runs only inside that particular browser session.

Achieve Session Handling in Selenium WebDriver

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.

SessionHandling.java
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.

Run Parallel Tests with Selenium

The parallel attribute of the suite tag accepts four values:

Attribute valueMeaning
testsAll test cases inside a <test> tag of the testing XML file run in parallel.
classesAll test cases inside a Java class run in parallel.
methodsAll methods annotated with @Test execute in parallel.
instancesTests 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:

Run Parallel Tests with Selenium

TestGuru99MultipleSession.java
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

testng.xml (parallel methods)
<?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.

testng.xml (dependsOnMethods)
<?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.

FAQs

A session is a unique sessionId generated each time a new WebDriver instance is created. Every command from that driver routes to the matching browser session, isolating one test from another.

The parallel attribute on the suite tag accepts tests, classes, methods, or instances. TestNG spawns multiple threads (set by thread-count) and runs the chosen unit concurrently, cutting total execution time.

Use the dependsOnMethods attribute inside the include tag of testng.xml, or @Test(dependsOnMethods = “name”) in code. TestNG runs the prerequisite first and skips the dependent test on failure.

AI tools cluster related tests, predict optimal thread-count from past runtimes, and reorder suites so slow tests start first, shortening overall wall-clock time for the build pipeline.

Yes. AI parses call graphs and historical failure logs to suggest dependsOnMethods links, flag circular dependencies, and propose safe parallel groups, reducing manual upkeep of testng.xml.

Summarize this post with: