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.

27 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

  • venkatesh says:

    Hi,

    Is there any possible way to multiple class in selenium webdriver run through jmeter.anybody known means share me ….

  • rohit says:

    plz provide me all the links regarding this domain

  • siva prasad chinta says:

    Hi I am not getting dropdown list at Classa name and Test Method in Junit Request at JMeter Thread group. I converted my class files as Jar files and added that jar file at JMeter/lib/junit. and i added selenium server and stand alone file.

    I am using this environment selenium 2.30.0 + Junit4 + JMeter .

    Please told this why classname and test methods are not displaying to me.

    It will be very helpful to me.

  • admin says:

    @venkatesh
    see my article for using DynamicSuite to run all test classes in a test package.
    Copy the classes in the article so you can customize how you run classes.
    If you run a test class makred with @RunWIth(DynamicSuite.class) all classes in the package and its sub packages will get executed. If you only want to execute tests in one package mark it with @NonRecursice annotaiton. Check out the article for more details.

    Create an entry class as below:

    @RunWith(DynamicSuite.class)
    public class EntryPoint {
    
        @Test
        public void dummy(){
            System.out.println("EntryPoint.dummy !");
        }
    }
    

    That’s all you will need. In JMeter select EntryPoint as the test class and dummy as the test method.
    With that all yout test classes and all test methods will get executed.
    cheers

  • admin says:

    @siva prasad chinta
    make sure that ‘Search for JUnit 4 annotations (instead of JUnit 3)’ is checked in the Junit Request window

    cheers

  • Paul says:

    Hi,
    I agree that selenium can be a great supplement for your JMeter testing. Here’s an article in which JMeter-as-a-Service startup Blazemeter describes integrating JMeter into its product.
    http://blazemeter.com/blog/user-experience-monitoring-selenium
    This is a a great option if you have a budget and want to take your JMeter testing to the next level.
    Paul

  • admin says:

    @paul,
    Definitely looks interesting.

  • Sachin says:

    Nice article, working fine, thanks a lot

  • Sachin says:

    I guess for my automation, i cannot completely replace firefox driver with html unit.

    even after enabling the javascript for htmlunit, still it doesnt work

    saying “Error loading JavaScript from xxxx”

    and if javascript is disabled, it doesnt find many elements.

    Any help?

  • admin says:

    @Sachin,
    can you provide bit more details about the error and the kind of application you are trying to test.
    cheers.

  • Bob says:

    Well, useful article but for beginners will be difficult to compile test and run with JMeter. There is easier way, you do not need compile JUnit tests each time, you can easily use WebDriver Sampler, write your tests in JMeter and run them.

  • Pingback: Integrating Selenium with JMeter for Load Testing

  • Pingback: jmeter |

  • shagufta says:

    dummy method is not working when trying to run thru Jmeter. Though I am able to see dummy method as part of EntryPoint class. I have implemented the DyanamicSuite class and other as mentioned in your other post.

    Is there anything else you can guide me?

  • glinius says:

    The easier way to integrate JMeter with Selenium WebDriver is using WebDriver Sampler available via JMeter Plugins. It provides a special sampler which allows to invoke WebDriver methods, measure sample time, latency, etc.

  • I don’t know whether it’s just me or if perhaps everybody else experiencing issues with your site.
    It looks like some of the text in your posts are running off the
    screen. Can someone else please provide feedback and let me know if this is
    happening to them too? This may be a issue with my web browser because I’ve had this
    happen previously. Appreciate it

  • Jura El says:

    Thanks for this article, it has solved some of the problems I’ve been having.

    I still have trouble with the step where I’m supposed to build the project. I set the maven jar plugin dependency as you said to (except I updated to version 2.5), but it won’t create the jar files. Or maybe it’s creating them somewhere I didn’t think to look.

    I use IntelliJ 13. Rebuilding the project only seems to update target\test-classes\MyTest.class

    If you have any ideas why it won’t create jar files, I’d be grateful to hear them.

  • Jeff says:

    Why did you bother to put the selenium-firefox-driver in the dependency if you knew that you were going to change it to selenium-htmlunit-driver before running mvn install? I am a JMeter user and I am new to Maven.

  • jay says:

    If we use headless growing for load testing using selenium driver,is it still going to be problem.

  • Deepali says:

    0

    down vote

    I am getting following error when I run my junit thru jmeter. Can you please help me resolve this

    jmeter.protocol.java.sampler.JUnitSampler: Trying to find constructor with one String parameter returned error



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>