TestNG + Selenium – Load Testing Example

testng-selenium-load-test

In this tutorial, we will show you how to use @Test attributes invocationCount and threadPoolSize to perform a load test or stress test on a website.

Tools used :

  1. TestNG 6.8.7
  2. Selenium 2.39.0
  3. Maven 3

P.S We are using the Selenium library to automate browsers to access a website.

1. Project Dependency

Get TestNG and Selenium libraries.

pom.xml

  <properties>
	<testng.version>6.8.7</testng.version>
	<selenium.version>2.39.0</selenium.version>
  </properties>

  <dependencies>
	<dependency>
		<groupId>org.testng</groupId>
		<artifactId>testng</artifactId>
		<version>${testng.version}</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.seleniumhq.selenium</groupId>
		<artifactId>selenium-java</artifactId>
		<version>${selenium.version}</version>
	</dependency>
   <dependencies>

2. @Test(invocationCount=?)

This invocationCount determined how many times TestNG should run this test method.

Example 2.1


  @Test(invocationCount = 10)
  public void repeatThis() {
    //...
  }

Output – The repeatThis() method will run 10 times.


PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis

Example 2.2 – Uses Selenium to open a Firefox browser and load “Google.com”. This test is to make sure the page title is always “Google”.


package com.mkyong.testng.examples.loadtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultipleThreads {

	@Test(invocationCount = 5)
	public void loadTestThisWebsite() {

		WebDriver driver = new FirefoxDriver();		
		driver.get("http://www.google.com");
		System.out.println("Page Title is " + driver.getTitle());
		Assert.assertEquals("Google", driver.getTitle());
		driver.quit();

	}
}

Output – You will notice that Firefox browser will prompts out and close 5 times.


Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite

3. @Test(invocationCount = ?, threadPoolSize = ?)

The threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.

Example 3.1 – Start a thread pool, which contains 3 threads, and run the test method 3 times.


  @Test(invocationCount = 3, threadPoolSize = 3)
  public void testThreadPools() {

	System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
		
  }

Output – The test method run 3 times, and each received its own thread.


[ThreadUtil] Starting executor timeOut:0ms workers:3 threadPoolSize:3
Thread Id : 10
Thread Id : 12
Thread Id : 11
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools

Example 3.2 – Start a thread pool, which contains 3 threads, and run the test method 10 times.


  @Test(invocationCount = 10, threadPoolSize = 3)
  public void testThreadPools() {

	System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
		
  }

Output – The test method run 10 times, and the threads are reused.


[ThreadUtil] Starting executor timeOut:0ms workers:10 threadPoolSize:3
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools

4. Load Test Example

By combining both TestNG multiple threads and Selenium powerful browser automation. You can create a simple yet powerful load test like this :

TestMultipleThreads.java

package com.mkyong.testng.examples.loadtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultipleThreads {
	
  @Test(invocationCount = 100, threadPoolSize = 5)
  public void loadTest() {

	System.out.printf("%n[START] Thread Id : %s is started!", 
                                  Thread.currentThread().getId());
		
	WebDriver driver = new FirefoxDriver();
	driver.get("http://yourwebsite.com");
		
	//perform whatever actions, like login, submit form or navigation
		
	System.out.printf("%n[END] Thread Id : %s", 
                                  Thread.currentThread().getId());
		
	driver.quit();

  }
}

Output – Above test will start a thread pool of 5, and send 100 URL requests to a specified website.


[ThreadUtil] Starting executor timeOut:0ms workers:100 threadPoolSize:5

[START] Thread Id : 11 is started!
[START] Thread Id : 14 is started!
[START] Thread Id : 10 is started!
[START] Thread Id : 12 is started!
[START] Thread Id : 13 is started!
[END] Thread Id : 11
[START] Thread Id : 11 is started!
[END] Thread Id : 10
[START] Thread Id : 10 is started!
[END] Thread Id : 13
[START] Thread Id : 13 is started!
[END] Thread Id : 14
[START] Thread Id : 14 is started!
[END] Thread Id : 12
[START] Thread Id : 12 is started!
[END] Thread Id : 13
......
FAQs
Q : For load testing with Selenium and TestNG, why only one browser is prompts out at a time?
A : Your test method is completed too fast, try putting a Thread.sleep(5000) to delay the execution, now, you should notice multiple browsers prompt out simultaneously. (For demonstration purpose only).

Download Source Code

Download – TestNG-LoadTest-Example.zip (15 kb)

References

  1. Selenium website
  2. Wikipedia : Load Testing

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
TigerRomanStephen Bassakhileshbrandon Recent comment authors
newest oldest most voted
brandon
Guest
brandon

i love mkyong!! i love this site!
so cooooooooool!!!

Roman
Guest
Roman

This generally looks like a visual testing task. Are you limited to Selenium only? In case there is an option for you to try something else for this task, you could do that with a visual regression testing tool. Such as Screenster (http://screenster.io) which is actually an alternative to Selenium for visual/CSS testing tasks. Screenster is a test automation tool which performs screenshot-based comparison of different versions of your web pages. First it creates a visual baseline for a page, taking a screenshot for each user action. During the next run it takes a new screenshot at each step, compares… Read more »

Stephen Bass
Guest
Stephen Bass

One thing to note for people using these examples: WebDriver objects within Selenium are not thread-safe. Here’s an example of that in a fairly modern web driver extension for a library called PhantomJS: . While this is not the same as a vanilla WebDriver through Selenium, the reason for its lack of thread safety is due to the fact that WebDriver itself is not thread-safe (see ). The code above is proven to work as a load-test, because of its output verifying that there are indeed unique threads being spawned. There is, however, no verification that there are unique sessions… Read more »

akhilesh
Guest
akhilesh

i need to open a site 100 time at a same time not a single sec time delay. Please provide me a help

Sorin
Guest
Sorin

For example 2.2, instead of
Assert.assertEquals(“Google”, driver.getTitle());
you should have
Assert.assertEquals(driver.getTitle(), “Google”);
because first comes actual, then expected.
In your example I agree that it makes no difference, but if you have a failed assert, the error will be misleading.

Tiger
Guest
Tiger

Funny :)

TKCG
Guest
TKCG

If the method has a for loop, and the loop is set to go until i =< 600, and we use threadPoolSize = 6. Will each thread loop until 600 separately (total 3600 loops), or will all threads share a single for loop to make a total of 600 loops? How can we set it so where all threads share the for loop?

rajasekhar meegada
Guest
rajasekhar meegada

Thanks a lot for this post and thank you for your clear and helpful explanation.

biswajit
Guest
biswajit

Hi,

thanks for such wonderful post . I need u r help for configurating Maven with selenium+TestNg. If there is any such example. Please let me guide that. I want to learn maven..seriousely and there is no such guide for it. means I am not that much techinical towards Maven configuration.

Pls help me.

Ganesh revankar
Guest
Ganesh revankar

Awesome post so simple and nicely explained. thanks a lot for this one. You really rock.
would like to see more examples of testng using dataprovider and groups. Thanks a lot.