Integrating Selenium Scripts with JMeter for Load Testing

Integrating Selenium Scripts with JMeter for Load Testing

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Integrating Selenium scripts with JMeter can significantly enhance your testing capabilities by combining functional and load testing. This article will guide you through the steps to achieve this integration successfully.

Possible Ways to Run Selenium Test Cases from JMeter

There are several methods to execute Selenium test cases within JMeter:

  • Using JUnit Request Sampler
  • Using BeanShell Sampler
  • Using JSR223 Sampler with Groovy

1. JUnit Request Sampler

The JUnit Request Sampler method is beneficial if you want to reuse already automated Selenium scenarios instead of rewriting scripts for the WebDriver Sampler.

Setup

  1. Download the Selenium Java client libraries and add them to the JMeter classpath (e.g., %JMETER_HOME%/lib/).
  2. Ensure the Selenium server is running with the command: java -jar selenium-server-standalone-${version}.jar
  3. Export the Selenium test plan as a .jar file and save it to %JMETER_HOME%/lib/junit/.

Configuration

  1. Add a JUnit Request Sampler to your JMeter test plan.
  2. Set the Class Name and Test Method according to your Selenium test plan.
  3. Optionally, configure JMeter to search for JUnit 4 annotations by checking the appropriate checkbox.

Example Code

Here is an example of a JUnit 4 test class:


package com.example.tests;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        driver = new FirefoxDriver();
        driver.get("http://www.google.com/");
    }

    @Test
    public void testGoogleSearch() {
        Assert.assertEquals("Google", driver.getTitle());
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
    

2. BeanShell Sampler

In this method, the Selenium test scenario is executed directly in JMeter’s BeanShell Sampler.

Setup

  1. Download the Selenium libraries and add them to the JMeter classpath.
  2. Start the Selenium server (if using Selenium RC).

Example Code


import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

Boolean result = true;

try {
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
    selenium.start();
    selenium.windowMaximize();
    selenium.open("/");
    selenium.waitForPageToLoad("30000");

    if (!selenium.isTextPresent("Google")) result = false;
} catch (Exception ex) {
    ex.printStackTrace();
    IsSuccess = false;
    ResponseCode = "500";
    ResponseMessage = ex.getMessage();
} finally {
    selenium.stop();
}

IsSuccess = result;
return result;
    

3. JSR223 Sampler with Groovy

This method uses the JSR223 Sampler with Groovy for executing Selenium test scenarios, offering better performance than the BeanShell Sampler.

Setup

  1. Download the Selenium libraries and add them to the JMeter classpath.
  2. Download the latest Groovy binary distribution and add the groovy-all-${VERSION}.jar to %JMETER_HOME%/lib/.
  3. Restart JMeter.

Example Code


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

Boolean result = true;

try {
    driver = new HtmlUnitDriver();
    driver.setJavascriptEnabled(true);
    driver.get("http://www.google.com/");

    if (!driver.getTitle().contains("Google")) result = false;
} catch (Exception ex) {
    ex.printStackTrace();
    log.error(ex.getMessage());
    SampleResult.setSuccessful(false);
    SampleResult.setResponseCode("500");
    SampleResult.setResponseMessage(ex.getMessage());
} finally {
    driver.quit();
}

SampleResult.setSuccessful(result);
return result;
    

Conclusion

Integrating Selenium scripts with JMeter can provide a comprehensive testing solution, combining the strengths of functional and load testing. Each method described above offers unique advantages, and the choice of method will depend on your specific requirements and existing setup.

For those looking to streamline their testing processes even further, consider exploring Repeato. Repeato is a no-code test automation tool for iOS and Android, enabling you to create, run, and maintain automated tests rapidly. With its computer vision and AI capabilities, Repeato offers an intuitive test recorder and a scripting interface for advanced use cases. It also supports testing websites inside Android emulators or devices, with explicit web testing support coming soon.

Like this article? there’s more where that came from!