Parallel Testing using Selenium and TestNG
Parallel execution is a very important concept in the field of automated testing. All the work we do at the same time always saves time. Similarly, in end-to-end testing of an application, running tests in parallel instead of sequentially saves us completion time; This will save us more time in the remaining stages of the software testing lifecycle and application delivery.
Run parallel testing to improve our Selenium testing by running the most tests in the shortest amount of time. It helps large organizations run large-scale experiments. However, automation testers need to remember the following points before testing the same; for example, avoiding dependence of one test on another and adjusting parameters so that the test cannot be repeated successfully over several successful experiments.
Parallel tests using TestNG and Selenium are a powerful way to run multiple tests simultaneously, thereby reducing overall execution time. Here are the steps to run parallel tests using TestNG and Selenium:
Step 1: Create a Maven Project
If you are using Maven, you can create a new Maven project using the IDE or the Maven command line. Open the command line interface and navigate to the directory where you want to create the project. Then run the following command:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-selenium-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will set the Maven project template.
Step 2: Add Dependencies
Open the pom.xml file in the project and add dependencies for TestNG and Selenium WebDriver. An example of how to add dependencies.
<dependencies>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<!-- Selenium WebDriver Dependencies -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
Note: Make sure to replace the versions with the latest ones available.
Step 3: Set up a Java Project
If you are not using Maven or Gradle, you can set up a Java project by creating a new Java project and adding TestNG and Selenium WebDriver libraries to your IDE (Eclipse, IntelliJ, etc.). Structure of the workbook. You can download the JAR file from a web link or use a build engine like Apache Ivy.
Step 4: Create a Test Group
Now you can start creating a test group. You can organize your tests as ‘@Test’, ‘@BeforeTest’, ‘@AfterTest’ etc. Publish with TestNG annotations. and write Selenium WebDriver code in this process to run your tests.
Step 5: Write TestNG XML Configuration
Create a TestNG XML configuration file (‘testng.xml’) where you can define your test suites, test classes, and any parameters. Here’s a basic example.
As TestNG Test, the tests will be successful only if we follow the above rules. To run them in parallel, go to testng.xml and write the following code:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing Suite">
<test name="Parallel Tests" parallel="methods">
<classes>
<class name="ParallelTest" />
</classes>
</test>
</suite>
Since our goal is to parallelize the process, parallel objects are assigned the “method” value.
Run the XML file as TestNG Suite and note that both drives must be opened together to prove that we are on the right track. Using the benchmarking in TestNG we can save a lot of time and solve more questions.
Step 6: Run Tests
You can now run tests from the IDE or using the TestNG command line or Maven commands. If you are using Maven, you can use the following command to run the test:
mvn test
Step7: View Results
After you complete the test, you can view the test results in the console output or in the HTML report generated by TestNG.
We will use TestNG to perform the testing procedure parallel. So when we run the test suite all the methods in the @Test annotation will run in parallel. The code below will launch the driver for two different browsers (Chrome and Firefox) simultaneously.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.*;
import org.testng.annotations.Test;
public class ParallelTest {
public WebDriver driver;
@Test
public void FirefoxTest() {
//Initializing the firefox driver (Gecko)
driver = new FirefoxDriver();
driver.get("https://www.demoqa.com");
driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div/div[1]/div/div[1]")).click();
driver.quit();
}
@Test
public void ChromeTest()
{
//Initialize the chrome driver
driver = new ChromeDriver();
driver.get("https://www.demoqa.com");
driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div[2]/div/div[1]/div/div[1]")).click();
driver.quit();
}
}
By following these steps you will set up your project with TestNG and Selenium WebDriver dependencies, allowing you to write and execute automated tests efficiently.
Parallel Testing with Selenium
In parallel testing, multiple tests can be run simultaneously in different execution modes, reducing execution time. This approach is particularly useful when running tests across multiple browsers or operating systems because it simplifies cross-browser testing. TestNG is often used with Selenium to provide seamless integration for execution.
Table of Content
- Parallel Testing using Selenium and TestNG
- Difference between Parallelization vs Serialization
- Time Taken Report of Parallelization vs Serialization
- Advantages of Parallel Testing
- Disadvantages of Parallel Testing
- Conclusion
- FAQs
Using parallel behavior in the TestNG.xml configuration file, TestNG can execute independent tests in parallel, simplifying the testing process and increasing efficiency.
Contact Us