Learn to Use TestNG Threads for Selenium Load Testing

It’s a simple TestNG tutorial which 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 no. 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 @Test annotation of TestNG.

These are <invocationCount> and <threadPoolSize>. The first attribute specifies the exact no. of times a test method will get called.  And the latter sets the total no. 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 do go through the below two posts. They’ll help you in ramping up quickly on using these tools.

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 in your project.


2. TestNG example using @Test(invocationCount=?) attribute.

The below code will show how does the <invocationCount> attribute call a method multiple times.


3. TestNG example using @Test(invocationCount=?, threadPoolSize=?) attribute.

In this TestNG example, we are using both the attributes together while keeping their value same. It’ll let each test method call run on a separate thread. To check, copy/paste 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.


4. TestNG example to generate load using multiple threads.

Here, we’ll show how to use a limited thread pool for processing a large no. 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 no. of threads as specified by the <threadPoolSize> attribute. And each thread will send multiple Web requests as a part of the load test.


Summary – How to Use TestNG Threads for Selenium Load Testing.

We are hopeful that this post will enable you in using 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’d enjoyed the post, then please don’t leave without sharing it with friends and on social media.

Keep Learning,


Leave a Reply