It’s a simple TestNG tutorial that explains how to use TestNG threads for Selenium load testing. Also, it’s true that we can utilize TestNG to perform load testing by creating multiple threads. We can even re-execute a test the number of times we want.
TestNG is indeed a quite resourceful test automation framework. And integrating it with Webdriver only makes it better to perform automated testing. In this post, we are going to expose two powerful attributes of the @Test annotation of TestNG.
These are <invocationCount> and <threadPoolSize>. The first attribute specifies the exact number of times a test method will get called. Whereas the latter sets the total number of threads that will run to call the test method.
Also, you should know that we can include both the above @Test attributes individually in a test and can use them in combination as well. In this TestNG tutorial, we’ll give you examples of using both these attributes.
Next, if you are a beginner and starting to learn TestNG and Selenium, then please go through the below two posts. They’ll help you in ramping up quickly on using these tools.
- Step-by-Step Guide to Create Your First WebDriver Project
- How to Write a TestNG-WebDriver Test Case from Scratch
How to use TestNG threads for Selenium load testing.
Before diving into the core part of this TestNG tutorial, first get to know about the tools required to build the code samples.
1. TestNG 6.9.10
2. WebDriver 2.53.1
3. Maven 4.0.0
If you haven’t already used Maven, then just read this post to set up Maven with TestNG and Selenium in five minutes.
Now, first of all, we’ll create a POM file to download all the above tools and run the test project.
1. POM file used to run the samples.
You can copy this POM file into your project.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.techbeamers</groupId> <artifactId>loadtesting</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Load Testing</name> <description>Selenium Load Testing Example Using TestNG and Maven</description> <!-- Add Following Lines in Your POM File --> <properties> <selenium.version>2.53.1</selenium.version> <testng.version>6.9.10</testng.version> </properties> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>${selenium.version}</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
2. TestNG example using @Test(invocationCount=?) attribute.
The below code will show how the <invocationCount> attribute calls a method multiple times.
package com.techbeamers.loadtesting; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit; //-- import org.openqa.selenium.By; //-- import org.openqa.selenium.WebDriver; //-- import org.openqa.selenium.WebElement; //-- import org.openqa.selenium.firefox.FirefoxDriver; //-- import org.testng.Assert; //-- import org.testng.annotations.AfterClass; //-- import org.testng.annotations.BeforeClass; //-- import org.testng.annotations.Test; public class LoadTestMultipleTimes { private WebDriver driver; @BeforeClass public void beforeClass() { } @AfterClass public void afterClass() { } @Test(invocationCount = 2) public void testGoogleSearch() { driver = new FirefoxDriver(); Date date = null; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com"); String search_text = "Google Search"; WebElement search_button = driver.findElement(By.name("btnK")); String text = search_button.getAttribute("value"); date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); String timstamp = sdf.format(date); System.out.println(timstamp + ": Search button text is " + driver.getTitle()); Assert.assertEquals(text, search_text, "Text not found!"); driver.quit(); } }
Also Read: Understanding TestNG Assertions
3. TestNG example using @Test(invocationCount=?, threadPoolSize=?) attribute.
In this TestNG example, we are using both attributes together while keeping their value the same. It’ll let each test method call run on a separate thread. To check, copy/paste the below code in your project.
We’ve added thread ID and timestamp in the log messages so that you can identify the thread and the execution time of the test running.
package com.techbeamers.loadtesting; //-- import java.text.SimpleDateFormat; //-- import java.util.Date; //-- import java.util.concurrent.TimeUnit; //-- import org.openqa.selenium.By; //-- import org.openqa.selenium.WebDriver; //-- import org.openqa.selenium.WebElement; //-- import org.openqa.selenium.firefox.FirefoxDriver; //-- import org.testng.Assert; //-- import org.testng.annotations.AfterClass; //-- import org.testng.annotations.BeforeClass; //-- import org.testng.annotations.Test; public class LoadTestSingleTestPerThread { @BeforeClass public void beforeClass() { } @AfterClass public void afterClass() { } @Test(invocationCount = 2, threadPoolSize = 2) public void testGoogleSearch() { System.out.printf("%n[+] Thread Id : %s is started!\n", Thread.currentThread().getId()); WebDriver driver = new FirefoxDriver(); Date date = null; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com"); String search_text = "Google Search"; WebElement search_button = driver.findElement(By.name("btnK")); String text = search_button.getAttribute("value"); date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); String timstamp = sdf.format(date); System.out.println(timstamp + " [Thread-" + Thread.currentThread().getId() + "]- Search button text is " + driver.getTitle()); Assert.assertEquals(text, search_text, "Text not found!"); System.out.printf("%n[-] Thread Id : %s Ended.\n", Thread.currentThread().getId()); driver.quit(); } }
4. TestNG example to generate load using multiple threads.
Here, we’ll show how to use a limited thread pool for processing a large number of Web requests. In this example, we are using a test that sends a request to Google and verifies the text at the search button.
This test will create a number of threads as specified by the <threadPoolSize> attribute. And, each thread will send multiple Web requests as a part of the load test.
package com.techbeamers.loadtesting; //-- import java.text.SimpleDateFormat; //-- import java.util.Date; //-- import java.util.concurrent.TimeUnit; //-- import org.openqa.selenium.By; //-- import org.openqa.selenium.WebDriver; //-- import org.openqa.selenium.WebElement; //-- import org.openqa.selenium.firefox.FirefoxDriver; //-- import org.testng.Assert; //-- import org.testng.annotations.AfterTest; //-- import org.testng.annotations.BeforeTest; //-- import org.testng.annotations.Test; public class LoadTestMultipleTestPerThread { @BeforeTest public void beforeTest() { } @AfterTest public void afterTest() { } @Test(invocationCount = 10, threadPoolSize = 5) public void testGoogleSearch() { System.out.printf("%n[+] Thread Id : %s is started!\n", Thread.currentThread().getId()); WebDriver driver = new FirefoxDriver(); Date date = null; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com"); String search_text = "Google Search"; WebElement search_button = driver.findElement(By.name("btnK")); String text = search_button.getAttribute("value"); date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); String timstamp = sdf.format(date); System.out.println(timstamp + " [Thread-" + Thread.currentThread().getId() + "]- Search button text is " + driver.getTitle()); Assert.assertEquals(text, search_text, "Text not found!"); System.out.printf("%n[-] Thread Id : %s Ended.\n", Thread.currentThread().getId()); driver.quit(); } }
Take This Quiz: Selenium WebDriver TestNG Quiz
Summary – How to Use TestNG Threads for Selenium Load Testing.
We are hopeful that this post will enable you to use TestNG and Selenium for load testing. Also, you would’ve learned to use TestNG threads for running WebDriver tests.
It would be great if you let us know your feedback on this post. Also, you can ask us to write on a topic of your choice. We’ll add it to our writing plan.
If you enjoyed the post, then please don’t leave without sharing it with friends and on social media.
Keep Learning,
TechBeamers.