Parallel Test Execution Using TestNG and Selenium Grid
What is Parallel Test Execution?
Parallel test execution means running multiple tests simultaneously to reduce total test execution time. Instead of running tests one after another, tests run concurrently on multiple threads or machines.
What is Selenium Grid?
Selenium Grid allows you to run tests on different machines (nodes) in parallel, managing different browsers, OS, and versions centrally via a Hub.
Why Combine TestNG + Selenium Grid?
TestNG provides built-in support for parallel execution.
Selenium Grid provides infrastructure to run tests across multiple machines or browsers.
Combined, they enable scalable, fast cross-browser testing.
How to Set up Parallel Execution Using TestNG and Selenium Grid
Step 1: Set Up Selenium Grid
Download Selenium Server standalone jar (Selenium Grid).
Start the hub:
bash
Copy
Edit
java -jar selenium-server-standalone.jar -role hub
Start one or more nodes and register them to the hub:
bash
Copy
Edit
java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register
Nodes can be on different machines or same machine with different browsers.
Step 2: Create TestNG XML for Parallel Execution
Use the parallel attribute in your testng.xml file. You can set parallel execution on:
tests: Parallel execution of <test> tags.
classes: Parallel execution of classes.
methods: Parallel execution of test methods.
Example testng.xml:
xml
Copy
Edit
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="ParallelSuite" parallel="tests" thread-count="3">
<test name="ChromeTests">
<parameter name="browser" value="chrome"/>
<classes>
<class name="tests.SampleTest"/>
</classes>
</test>
<test name="FirefoxTests">
<parameter name="browser" value="firefox"/>
<classes>
<class name="tests.SampleTest"/>
</classes>
</test>
<test name="EdgeTests">
<parameter name="browser" value="edge"/>
<classes>
<class name="tests.SampleTest"/>
</classes>
</test>
</suite>
Here, three tests run in parallel with different browsers.
Step 3: Write Selenium Test Class to Use Grid and Browser Parameter
Example Java Test class SampleTest.java:
java
Copy
Edit
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import java.net.URL;
public class SampleTest {
WebDriver driver;
@Parameters("browser")
@BeforeMethod
public void setup(String browser) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
switch (browser.toLowerCase()) {
case "chrome":
caps.setBrowserName("chrome");
break;
case "firefox":
caps.setBrowserName("firefox");
break;
case "edge":
caps.setBrowserName("MicrosoftEdge");
break;
default:
throw new IllegalArgumentException("Browser " + browser + " not supported");
}
// Connect to Selenium Grid Hub
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
}
@Test
public void testGoogleTitle() {
driver.get("https://www.google.com");
System.out.println("Title on " + driver.getCapabilities().getBrowserName() + ": " + driver.getTitle());
assert driver.getTitle().contains("Google");
}
@AfterMethod
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
Step 4: Run the TestNG Suite
Run your testng.xml file as TestNG suite. The tests will execute in parallel on Selenium Grid, each on different browser nodes.
Key Notes:
Make sure nodes support requested browsers.
Tune thread-count based on your node capacity.
You can also parallelize at classes or methods level.
Using Selenium Grid helps scale tests across machines, browsers, and OS.
Learn Selenium JAVA Training in Hyderabad
Read More
How to Handle Captchas and File Uploads in Selenium
Logging in Selenium Tests with Log4j
Page Object Model (POM) in Selenium with Java
Integrating Selenium with Maven and Jenkins for CI/CD
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment