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.

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.