Interview #101: Selenium - How to run tests in parallel?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
Running Selenium tests in parallel is essential for improving test execution speed and efficiency, especially when dealing with large test suites. There are multiple ways to achieve parallel execution in Selenium, depending on the framework and tools used. Below are the common approaches:
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245
1. Using TestNG for Parallel Execution
TestNG is one of the most commonly used frameworks for running parallel tests in Selenium. It provides built-in support for parallel execution through its XML configuration.
Steps to Run Selenium Tests in Parallel using TestNG:
TestNG allows parallel execution at different levels:
Example testng.xml file for running test classes in parallel:
<suite name="ParallelSuite" parallel="classes" thread-count="3">
<test name="ChromeTest">
<classes>
<class name="com.tests.ChromeTest" />
</classes>
</test>
<test name="FirefoxTest">
<classes>
<class name="com.tests.FirefoxTest" />
</classes>
</test>
<test name="EdgeTest">
<classes>
<class name="com.tests.EdgeTest" />
</classes>
</test>
</suite>
Here, thread-count="3" means that up to three threads will execute in parallel.
public class ChromeTest {
WebDriver driver;
@BeforeClass
public void setup() {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
}
@Test
public void testGoogleSearch() {
driver.get("https://www.google.com");
Assert.assertEquals(driver.getTitle(), "Google");
}
@AfterClass
public void teardown() {
if (driver != null) {
driver.quit();
}
}
}
This ensures that each test has its own WebDriver instance.
2. Using Selenium Grid for Parallel Execution
Selenium Grid is a tool that allows tests to be executed in parallel on multiple machines (nodes) connected to a central hub.
Steps to Set Up Parallel Execution with Selenium Grid:
java -jar selenium-server-4.x.x.jar hub
java -jar selenium-server-4.x.x.jar node --hub https://localhost:4444
public class GridTest {
WebDriver driver;
@BeforeClass
@Parameters("browser")
public void setup(String browser) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
if (browser.equalsIgnoreCase("chrome")) {
capabilities.setBrowserName("chrome");
} else if (browser.equalsIgnoreCase("firefox")) {
capabilities.setBrowserName("firefox");
}
driver = new RemoteWebDriver(new URL("https://localhost:4444"), capabilities);
}
@Test
public void testTitle() {
driver.get("https://www.google.com");
Assert.assertEquals(driver.getTitle(), "Google");
}
@AfterClass
public void teardown() {
driver.quit();
}
}
This test will run on different browsers in parallel using Selenium Grid.
3. Using JUnit 5 with Parallel Execution
JUnit 5 provides a way to enable parallel execution via its configuration.
Steps to Enable Parallel Execution in JUnit 5:
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
@ExtendWith(SeleniumExtension.class)
public class ParallelJUnitTest {
private WebDriver driver;
@BeforeEach
void setup() {
driver = new ChromeDriver();
}
@Test
void testGoogleSearch() {
driver.get("https://www.google.com");
Assertions.assertEquals("Google", driver.getTitle());
}
@AfterEach
void teardown() {
driver.quit();
}
}
This enables JUnit to run tests concurrently.
4. Using Maven Surefire & Failsafe Plugins
For Maven-based projects, the maven-surefire-plugin or maven-failsafe-plugin can be used for parallel execution.
Steps to Enable Parallel Execution in Maven:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<parallel>methods</parallel>
<threadCount>3</threadCount>
</configuration>
</plugin>
</plugins>
</build>
mvn test
This will run tests in parallel using multiple threads.
Conclusion
Parallel execution of Selenium tests significantly reduces execution time and enhances test efficiency. The best approach depends on your project needs:
Would you like help implementing parallel execution for your specific setup? ??
Staff Engineer at Nagarro || Selenium || playwright || API Automaion-RestAssured || Core Java || Basic JS || TestNG || BDD Cucumber || Junit || Jenkins
1 周Very informative
Manager of Application Systems
1 周5. Running tests in CI/CD like bamboo on more than one agent