Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Use Selenium WebDriver JUnit with JMeter

This example explains the basic design, functionality and usage of new JUnit Sampler for JMeter. We can use Selenium WebDriver with JUnit 4 tests in JMeter to do performance or stress tests.

For technical reference, visit the manual on Apache site

Test Case: do a performance test for Mahara ePortfolio Application Login Module. Test steps are as follows:

  • navigate to the site
  • enter "student1" in the user name field
  • enter "Testing1" in the password field
  • click on login button
  • Assert that "Dashboard" is displayed.


Step 1: create a Java project as shown on the picture below. project name is "JMeterJUnitProject", add a package named "com.seleniummaster.jmeterjunit" and add a JUnit test class "LoginTest". In the build path, add JUnit and Selenium Jar libraries.


Step 2: write the following code in the class

package com.seleniummaster.jmeterjunit;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginTest {
  private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();  
  public void setUp() throws Exception {
    //use Firefox driver
    driver = new FirefoxDriver();
    //use site for testing
      baseUrl = "";
      //timeout if site page does not load in 30 seconds
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  public void tearDown() throws Exception {
    //quit the test
      String verificationErrorString = verificationErrors.toString();
      if (!"".equals(verificationErrorString)) {
  public void test() throws InterruptedException {
    //navigate to base url
    driver.get(baseUrl + "/");
    //clear username filed
      //enter user name
      //clear password
      //enter password
      //click on submit button
      //assert the Dashboard link text
      for (int second = 0;; second++) {
        if (second >= 60fail("timeout");
        try if (isElementPresent(By.linkText("Dashboard"))) breakcatch (Exception e) {}

      assertEquals("Dashboard", driver.findElement(By.linkText("Dashboard")).getText());
  private boolean isElementPresent(By by) {
      try {
        return true;
      catch (NoSuchElementException e) {
        return false;

    private boolean isAlertPresent() {
      try {
        return true;
      catch (NoAlertPresentException e) {
        return false;

    private String closeAlertAndGetItsText() {
      try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
        else {
        return alertText;
      finally {
        acceptNextAlert = true;

Step 3: run the test as JUnit Test and make sure that the test should pass.

Step 4: export the test as java "Jar" file. Make sure you save the exported Jar file under Jmeter\lib\junit folder.

Step 5: start JMeter and create a test plan project and name it JmeterJUnitTestPlan. Add a thread Group, enter 5 in Thread Group (user) and name it as "Thread Group 5 user". Add a Sampler ->JUnit Request; Add a listener ->View Result in Table.

Open the JUnit Request sampler and select "com.seleniummaster.jmeterjunit.LoginTest" in the classname. Select "test" in the TestMethod. Make sure that "Search for JUnit 4 annotations" checkbox is checked. All other parameters can be default.

Step 6: run the JMeter Test Plan and check the result in the "View Results in Table".  The result shows that test run 5 times and average sample time is 6959 seconds.

Note: if you have multiple tests in the test class, you can select the test under the Method Name drop down list.