0
0
Selenium Javatesting~5 mins

Parallel execution configuration in Selenium Java

Choose your learning style9 modes available
Introduction

Parallel execution lets you run multiple tests at the same time. This saves time and helps find problems faster.

You want to test your website on different browsers at once.
You have many test cases and want to finish testing quickly.
You want to check how your app works when many users use it simultaneously.
You want to use your computer's full power to run tests faster.
Syntax
Selenium Java
In TestNG XML file:

<suite name="SuiteName" parallel="tests" thread-count="3">
  <test name="Test1">
    <classes>
      <class name="com.example.TestClass1"/>
    </classes>
  </test>
  <test name="Test2">
    <classes>
      <class name="com.example.TestClass2"/>
    </classes>
  </test>
  <test name="Test3">
    <classes>
      <class name="com.example.TestClass3"/>
    </classes>
  </test>
</suite>

The parallel attribute can be set to tests, classes, or methods to control what runs in parallel.

The thread-count sets how many threads (tests) run at the same time.

Examples
This runs two tests in parallel, each test can have multiple classes.
Selenium Java
<suite name="Suite" parallel="tests" thread-count="2">
  <test name="TestA">
    <classes>
      <class name="com.example.TestA"/>
    </classes>
  </test>
  <test name="TestB">
    <classes>
      <class name="com.example.TestB"/>
    </classes>
  </test>
</suite>
This runs three classes in parallel inside one test.
Selenium Java
<suite name="Suite" parallel="classes" thread-count="3">
  <test name="Test">
    <classes>
      <class name="com.example.Test1"/>
      <class name="com.example.Test2"/>
      <class name="com.example.Test3"/>
    </classes>
  </test>
</suite>
This runs test methods inside one class in parallel.
Selenium Java
<suite name="Suite" parallel="methods" thread-count="4">
  <test name="Test">
    <classes>
      <class name="com.example.TestClass"/>
    </classes>
  </test>
</suite>
Sample Program

This Java test class uses TestNG to run two tests in parallel if configured in the TestNG XML file with parallel="methods" and thread-count="2". Each test opens a browser, checks the page title, and prints a success message.

Selenium Java
package com.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelTest {
    WebDriver driver;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
    }

    @Test
    public void testGoogle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        assert title.contains("Google");
        System.out.println("Google test passed");
    }

    @Test
    public void testBing() {
        driver.get("https://www.bing.com");
        String title = driver.getTitle();
        assert title.contains("Bing");
        System.out.println("Bing test passed");
    }

    @AfterMethod
    public void teardown() {
        driver.quit();
    }
}
OutputSuccess
Important Notes

Make sure your tests do not share data that can cause conflicts when running in parallel.

Use thread-safe WebDriver instances to avoid errors.

Configure your TestNG XML file correctly to enable parallel execution.

Summary

Parallel execution runs tests at the same time to save time.

Set parallel and thread-count in TestNG XML to control parallelism.

Write tests that can run independently without sharing data.