---
description: To understand how to run selenium scripts in parallel with Multiple Browsers. Let's first understand Session Handling. During test execution, the Selenium WebDriver has to interact with the browser all the time to e
title: Parallel Execution in Selenium
image: https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png
---

[Skip to content](#main)

**⚡ 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.

Read More

![How to Run Parallel Execution in Selenium](https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png)

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]()](https://www.guru99.com/images/AdvanceSelenium/071514_0645_SessionsPar1.png)

In such a situation, we need a mechanism so the two executions do not overlap. [Selenium](https://www.guru99.com/selenium-tutorial.html) 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]()](https://www.guru99.com/images/AdvanceSelenium/071514_0645_SessionsPar2.png)

Every subsequent command runs only inside that particular browser session.

[![Achieve Session Handling in Selenium WebDriver]()](https://www.guru99.com/images/AdvanceSelenium/071514_0645_SessionsPar3.png)

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/");
    }
}

```

**Don't Miss:**

- [Live Selenium Webdriver Testing Project](https://www.guru99.com/live-ecommerce-project.html)
- [Apache ANT Tutorial](https://www.guru99.com/using-apache-ant-with-selenium.html)
- [How to Handle Cookies in Selenium WebDriver](https://www.guru99.com/handling-cookies-selenium-webdriver.html)
- [TestNG Listeners in Selenium](https://www.guru99.com/listeners-selenium-webdriver.html)

## 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](https://www.guru99.com/cross-browser-testing-using-selenium.html) or large smoke suites.

In such cases, the `parallel` attribute on the TestNG suite tag does the work.

[![Run Parallel Tests with Selenium]()](https://www.guru99.com/images/AdvanceSelenium/071514_0645_SessionsPar4.png)

The parallel attribute of the suite tag accepts four values:

| Attribute value | Meaning |
| --- | --- |
| tests | All [test cases](https://www.guru99.com/test-case.html) inside a \<test> tag of the [testing](https://www.guru99.com/software-testing.html) XML file run in parallel. |
| classes | All test cases inside a [Java](https://www.guru99.com/java-tutorial.html) 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](https://www.guru99.com/all-about-testng-and-selenium.html) and [install TestNG in Eclipse](https://www.guru99.com/install-testng-in-eclipse.html) before running. For scale beyond a single machine, route the same suite through [Selenium Grid 4](https://www.guru99.com/introduction-to-selenium-grid.html) 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]()](https://www.guru99.com/images/AdvanceSelenium/071514_0645_SessionsPar5.png)

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](https://www.guru99.com/test-case.html) 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](https://www.guru99.com/test-automation-framework.html) design, compare [JUnit vs TestNG](https://www.guru99.com/junit-vs-testng.html), and review the [findElement](https://www.guru99.com/find-element-selenium.html) 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](https://testng.org/), evaluates these declarations before launching threads, so dependent methods always start after their prerequisites finish.

## FAQs

🧩 What is a session in Selenium WebDriver?

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.

⚙️ How does the parallel attribute in TestNG work?

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.

🔗 How do I make one test case depend on another in TestNG?

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.

🤖 How does AI assist parallel test scheduling in Selenium?

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.

🧠 Can AI automate test dependency mapping in TestNG suites?

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:

ChatGPT Perplexity Grok Google AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png","url":"https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png","width":"700","height":"250","caption":"How to Run Parallel Execution in Selenium","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html","name":"Parallel Execution in Selenium"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#webpage","url":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html","name":"Parallel Execution in Selenium","dateModified":"2026-06-17T16:52:12+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Selenium","headline":"Parallel Execution in Selenium","description":"To understand how to run selenium scripts in parallel with Multiple Browsers. Let&#039;s first understand Session Handling. During test execution, the Selenium WebDriver has to interact with the browser all the time to e","keywords":"selenium","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"dateModified":"2026-06-17T16:52:12+05:30","image":{"@id":"https://www.guru99.com/images/how-to-run-parallel-execution-in-selenium.png"},"copyrightYear":"2026","name":"Parallel Execution in Selenium","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a session in Selenium WebDriver?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How does the parallel attribute in TestNG work?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How do I make one test case depend on another in TestNG?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"How does AI assist parallel test scheduling in Selenium?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can AI automate test dependency mapping in TestNG suites?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}],"@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#schema-1115734","isPartOf":{"@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/sessions-parallel-run-and-dependency-in-selenium.html#webpage"}}]}
```
