Integrating Selenium with JMeter for Load Testing

July 1st, 2012 | Posted by admin in java | JUnit

Using Selenium for load testing isn’t considered a good practice presumably because Selenium is intended for functional testing rather than load testing and as such its not the right tool for load testing.
Just because Selenium was intended for functional and regression testing doesn’t mean that it cannot be used to aid  load testing applications with JMeter -especially if  the application under test makes heavy use of ajax.

JMeter can be used to record browser actions using proxy, but since JMeter  doesn’t handle Javascript, it cannot be  used on its own to simulate realistic user interaction with a  Javascript/Ajax  heavy web application.
JMeter does what it does very well, but there’s no reason not to make it do things even better if tools like Selenium can aid it.
Selenium can be used to record user-interaction with a web application where lots of interactions happen  using Ajax.

And its not complicated to integrate Selenium with JMeter.

Create a test project to test the generated unit test. Add Junit and Selenium Firefox Driver dependency to the project.
Example Maven dependency.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.24.1</version>
<scope>test</scope>
</dependency>

Record user actions  using Selenium IDE and export the script as JUnit test case.
Here’s an example of a simple user interaction  where user visits a site and logs in.

 


public class LoginTest extends TestCase {

    private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();

public void setUp() throws Exception {

        driver = new HtmlUnitDriver();
baseUrl = "http://localhost:8080";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

public void testLogin() throws Exception {

driver.get(baseUrl + "/home.htm");
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("dummyuser");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("dummypwd");
driver.findElement(By.id("btnLogin")).click();
driver.findElement(By.id("label-10")).click();

}

public void tearDown() throws Exception {

driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

}

Run the unit test. Selenium will open Firefox and replay the actions previously recorded.

Now that we know the test works and we can see it working visually, change the Selenium Maven dependency from Firefox Driver to HtmlUnit Driver.
We don’t want JMeter to open hundreds of firefox browsers whe we do the actual testing so we’ll use HtmlUnit Headless browser instead.

Replace the selenium dependency added earlier with the below HtmlUnit Driver dependency.

 <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.24.1</version>
<scope>test</scope>
</dependency>

Add the following to the build->plugins node so that the project generated a jar file for our test class.


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Run mvn install or use your favourite IDE to build the project, maven will create a jar file with junit test classes in the target folder in <PROJECT-NAME>-<VERSION>-tests.jar format.

Download JMeter if you haven’t already got a copy of it.
Download selenium-java-2.24.1.jar and selenium-server-standalone-2.24.1.jar files from Selenium download page and copy them over to lib folder in JMeter installation folder.
Copy the junit test class jar file created by maven to lib/junit folder in JMeter installation folder.

Start up JMeter and create a Test plan. Add ThreadGroup to the test plan and add Junit Request Sampler to the Thread Group from the context menu [add->Sampler->Junit Request].
The Junit Request window should display the Junit Test Class (com.bs.webtest.LoginTest ) as well as the test method (loginTest) automatically picked up by JMeter.
Now simply configure the ThreadGroup for the number of Threads(users) you want to create and configure Loop Count for number of iteration and you are ready to go.

 

 

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

6 Responses

  • shashank says:

    Hi,

    Your steps are right but when more than one user enter with same credentials then corresponding no of browser will open and only last user will be continue with web site.
    I already face this problem .
    do you have any tips .????

    Thanks
    shashank

  • admin says:

    shashank ,
    I don’t have any issues with running multiple users with the same credentials.
    To troubleshoot your issue, try running the test with 5 users with a single loop count and ramp-up period of 15-30 seconds to give you enough space between each run. Run your project in debug mode and put break-points in your login handler. And say if your user is redirected to account.htm page, put a breakpoint on the handler that serves up the accounts.htm page and verify that both pages get hit 5 times each.
    If your test isn’t hitting the server as expected, add a ‘View results in Table’ Listener to your test plan and see if it shows any error.
    cheers

  • Daniel says:

    This is a excellent and useful post, but if you have a detailed steps for the same, I request you to please provide me as I am new to J-Meter.

    Thanks in Advance :)

  • Performancetester says:

    Hi Admin,

    After completing the jmeter step num of vsuer and thread, how to execute the test from selenium or jmeter,
    will there be a html result file generated of all the graphs i add in jmeter



Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>