Performance testing with Selenium and JMeter

18 Nov, 2012

In this blog I will show a way to do performance testing with Selenium. The reason I use Selenium for performance testing is that some applications use proprietary protocols between the application layer in the browser and the server.
So just capturing the traffic between the server and replaying modified traffic is not that simple.
An example is testing GWT applications. In a previous blog I wrote why this is difficult.
To create a test script in Selenium the first thing I do is record a test with Selenium IDE
After recording a script I export the script to JUnit3 (Remote Control). This will generate a JUnit test script which can be run to test the application.
The next thing you need is a solution to run a lot of JUnit test cases at the same moment.
Here you see a visual representation of the whole test chain.

To archive this I created a lot of Virtual hosts, installed selenium server on the hosts and wrote the name of the server together with the selenium server port in a csv file.
After this I added the following code to a base class witch will be the base class for all the JUnit test classes:

protectedstaticinthostCounter = 0;

protectedstaticsynchronizedvoid initializeSeleniumHosts(){
    //code to read file csv file with hosts
    //other initializations
protectedstaticfinalThreadLocalseleniumRunner = newThreadLocal(){
    protected Object initialValue(){
        String[] hostFields = seleniumHosts.get(hostCounter).split(“:”);
        returnnew DefaultSelenium(hostFields[0], Integer.parseInt(hostFields[1]), “*firefox”, “http://targetHost+baseUrl”+”/”);
publicvoid setUp(){
    selenium = (DefaultSelenium)seleniumRunner.get();
publicvoid tearDown(){


With this code each JUnit tests case will be run on the next host in the csv file.
During the first manual runs you can see where a script fails. But when you use > 10 vm’s to do the testing you can’t see where the script gives errors.
To solve this problem I introduced a custom fail method in the base class.
The fail method logs the line where if fails in a logfile. This helps fixing the script to be sure it does not fail on timeouts with fixed timeouts.
The fail method:

publicstaticvoid fail(String message){    try{            throw new Exception();   
}catch(Exception e){
        StackTraceElement[] stacktrace = e.getStackTrace();
        logFile.write(“FAIL: ;” + message+“;”+stacktrace[1]+“\n”);
    }catch(Exception ee){


Here you see a code snippet on how to use it:

for (int second = 0;; second++) {    if (second >= 15) fail(“timeout”); Thread.sleep(4000);       try { if (selenium.isElementPresent(“//input[@id=’someId’]”))
    } catch (Exception e) {}

This method of testing helped me a lot to do performance testing on GWT and applications and Java applets.
I hope this information will help you to do performance testing on frameworks which use custom protocols between the client and the server. Good luck.

Newest Most Voted
Inline Feedbacks
View all comments
R Gill
R Gill
8 years ago

however i can’t imagine this setup to scale up for load testing where one needs to simulate hundreds or thousands of users unless you have the amount of vms (to run Selenium controlled browsers) available that you can control from the jmeter + junit setup, which in the end is really expensive setup. A rather cheaper option is to create custom samplers(Plugins) for those protocols only if not already available OOB jmeter, and try to simulate the browser behavior as closely as possible within jmeter, including any ajax and or other required protocols requests.
you are absolutely right that its not easy but in my opinion and experience its easier then setting up a grid of selenium machines for performance testing.
P.S. One jmeter machine with 2 CPU, 4 GB can easily simulate upto 1000 threads for a complicated script (~50 dynamic reqs, regex post samplers etc).

Dmitri T.
Dmitri T.
8 years ago

There is an extension for JMeter which allows configuring and invoking Browser through WebDriver Sampler (you need to get it from JMeter Plugins site and drop contents to lib and lib/ext folders)

6 years ago

Very informative post and it was quite helpful to me. I also wrote something similar lines on need for software performance testing –

Explore related posts