In the ever-evolving world of software testing, speed and efficiency are crucial for successful software development. As applications grow in complexity, running tests sequentially can lead to longer test cycles, ultimately slowing down the development process. One effective way to optimize test execution is through parallel testing. In this post, we will delve into Selenium Parallel Test Execution with TestNG and provide you with an example to help you get started.
Parallel testing allows you to execute multiple tests simultaneously, leveraging the power of multi-threading. This means you can run tests for different browser instances or even different tests in the same browser at the same time, significantly reducing overall execution time.
Before diving into the code, you need to set up Selenium with TestNG in your Java project. Here are the steps required to prepare your environment:
Create Maven Project: Begin by creating a Maven project in your preferred IDE, such as IntelliJ or Eclipse.
Add Dependencies: Include the following dependencies in your pom.xml
file for Selenium and TestNG:
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> </dependencies>
Configure TestNG for Parallel Execution: Create a testng.xml
file in the root directory of your project. This XML file will determine how your tests are executed. Here’s a sample configuration that enables parallel execution:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite" parallel="tests" thread-count="4"> <test name="Test1"> <classes> <class name="tests.TestClass1"/> </classes> </test> <test name="Test2"> <classes> <class name="tests.TestClass2"/> </classes> </test> </suite>
In this example, we configured TestNG to run two tests (TestClass1
and TestClass2
) in parallel, utilizing a thread count of 4.
Let’s create two simple test classes to illustrate parallel execution:
TestClass1.java
package tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestClass1 { WebDriver driver; @BeforeClass public void setup() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void navigateToGoogle() { driver.get("http://www.google.com"); System.out.println("TestClass1: Title is - " + driver.getTitle()); } @AfterClass public void teardown() { driver.quit(); } }
TestClass2.java
package tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestClass2 { WebDriver driver; @BeforeClass public void setup() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void navigateToBing() { driver.get("http://www.bing.com"); System.out.println("TestClass2: Title is - " + driver.getTitle()); } @AfterClass public void teardown() { driver.quit(); } }
To execute your parallel tests, simply run the testng.xml
file you created. You can do this from your IDE, command line, or by configuring your CI/CD pipeline. You will observe that both tests run simultaneously in their browser instances.
With this setup, you are well on your way to executing your Selenium tests in parallel using TestNG. By taking advantage of parallel testing, you’ll not only speed up your testing but also increase the productivity of your development cycle.
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation
18/09/2024 | UI Automation
21/09/2024 | UI Automation
21/09/2024 | UI Automation