
👉Introduction
Parallel Execution in Selenium: When executing Selenium test cases, running tests sequentially can be time-consuming. Parallel execution helps in reducing test execution time by running multiple test cases simultaneously. However, handling multiple sessions properly is critical to avoid conflicts.
Key Topics Covered in This Guide:
✅ Parallel Execution in Selenium: Why do we need Session Handling in Selenium?
✅ How to execute parallel tests using TestNG?
✅ Managing test dependencies using dependsOnMethods
👉Why Do We Need Session Handling in Selenium?
Parallel Execution in Selenium: During Selenium test execution, WebDriver interacts with the browser continuously. If another test starts execution on the same machine and browser before the current test finishes, session conflicts may occur.
🔹 Solution: Session Handling in Selenium ensures that each WebDriver instance has a unique session ID, preventing overlap.
👉How Does Selenium WebDriver Handle Sessions?
Parallel Execution in Selenium: In Selenium WebDriver’s source code, there is a built-in variable called sessionId.
🔹 Each WebDriver instance generates a unique session ID when a browser session is started.
🔹 All commands executed in that session apply only to that specific browser instance.
Example: How Selenium Manages Sessions?
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SessionHandlingExample {
public static void main(String[] args) {
WebDriver driver1 = new ChromeDriver(); // New session created
driver1.get(“https://www.google.com”);
WebDriver driver2 = new ChromeDriver(); // Another session created
driver2.get(“https://www.facebook.com”);
}
}
Expected Outcome:
✔ driver1 opens Google in one session
✔ driver2 opens Facebook in another session
Since two separate instances of WebDriver are created, they run independently, without overlapping sessions.
👉How to Run Tests in Parallel Using TestNG?
Parallel Execution in Selenium: If you need to execute multiple tests simultaneously, you can use the parallel execution feature of TestNG.
TestNG’s Parallel Execution Attribute
TestNG allows parallel execution by using the parallel attribute in the testng.xml file. It supports four values:
1️⃣ tests → Runs all <test> cases in parallel
2️⃣ classes → Runs all classes in parallel
3️⃣ methods → Runs all methods with @Test in parallel
4️⃣ instances → Runs test cases in the same instance in parallel
Example: Running Test Cases in Parallel
Step 1: Create Test Class
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class ParallelExecutionExample {
@Test
public void testGoogle() {
WebDriver driver = new ChromeDriver();
driver.get(“https://www.google.com”);
System.out.println(“Google Test – ” + Thread.currentThread().getId());
driver.quit();
}
@Test
public void testFacebook() {
WebDriver driver = new ChromeDriver();
driver.get(“https://www.facebook.com”);
System.out.println(“Facebook Test – ” + Thread.currentThread().getId());
driver.quit();
}
}
Step 2: Configure testng.xml for Parallel Execution
xml
<suite name=”ParallelTestSuite” parallel=”methods” thread-count=”2″>
<test name=”ParallelTest”>
<classes>
<class name=”ParallelExecutionExample”/>
</classes>
</test>
</suite>
Expected Outcome:
✔ Both testGoogle() and testFacebook() run in separate threads, executing in parallel.
✔ thread-count=”2″ ensures that two tests run simultaneously.
👉Setting Test Case Order & Dependencies in TestNG
Parallel Execution in Selenium: In some cases, test cases must run in a specific sequence. For example, a login test should run before a profile update test.
🔹 Solution: Use dependsOnMethods to set test dependencies.
Example: Setting Test Execution Order
java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestDependencyExample {
WebDriver driver;
@Test
public void openBrowser() {
driver = new ChromeDriver();
driver.get(“https://www.example.com”);
System.out.println(“Opened Browser”);
}
@Test(dependsOnMethods = “openBrowser”)
public void loginTest() {
System.out.println(“Executing Login Test”);
// Login code here
}
@Test(dependsOnMethods = “loginTest”)
public void profileUpdateTest() {
System.out.println(“Executing Profile Update Test”);
// Profile update code here
}
}
Expected Execution Order:
✔ openBrowser() → Runs first
✔ loginTest() → Runs second (depends on openBrowser())
✔ profileUpdateTest() → Runs last (depends on loginTest())
If openBrowser() fails, the dependent tests (loginTest(), profileUpdateTest()) will be skipped.
👉Key Takeaways on Parallel Execution & Session Handling
✔ Parallel Execution in Selenium: Selenium WebDriver assigns a unique session ID to each instance.
✔ Parallel execution in TestNG can be configured using parallel and thread-count.
✔ Test dependencies can be managed using dependsOnMethods in TestNG.
✔ Session handling ensures that different WebDriver instances do not interfere with each other.
👉Conclusion
🔹 Running parallel tests in Selenium can significantly reduce execution time.
🔹 Session handling ensures each WebDriver instance works independently.
🔹 Using TestNG dependencies, you can define execution order and test case priority.
By implementing these TestNG techniques, you can optimize your Selenium automation framework for faster and more efficient test execution!