Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

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 http://demo.mahara.org
  • enter "student1" in the user name field
  • enter "Testing1" in the password field
  • click on login button
  • Assert that "Dashboard" is displayed.

Implementation

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 LoginTest.java 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();  
  @Before
  public void setUp() throws Exception {
    //use Firefox driver
    driver = new FirefoxDriver();
    //use demo.mahara.org site for testing
      baseUrl = "http://demo.mahara.org";
      //timeout if site page does not load in 30 seconds
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }
  @After
  public void tearDown() throws Exception {
    //quit the test
    driver.quit();
      String verificationErrorString = verificationErrors.toString();
      if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
      }
  }
  @Test
  public void test() throws InterruptedException {
    //navigate to base url
    driver.get(baseUrl + "/");
    //clear username filed
      driver.findElement(By.id("login_login_username")).clear();
      //enter user name
      driver.findElement(By.id("login_login_username")).sendKeys("student1");
      //clear password
      driver.findElement(By.id("login_login_password")).clear();
      //enter password
      driver.findElement(By.id("login_login_password")).sendKeys("Testing1");
      //click on submit button
      driver.findElement(By.id("login_submit")).click();
      //assert the Dashboard link text
      for (int second = 0;; second++) {
        if (second >= 60fail("timeout");
        try if (isElementPresent(By.linkText("Dashboard"))) breakcatch (Exception e) {}
        Thread.sleep(1000);
      }

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

    private boolean isAlertPresent() {
      try {
        driver.switchTo().alert();
        return true;
      catch (NoAlertPresentException e) {
        return false;
      }
    }

    private String closeAlertAndGetItsText() {
      try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
          alert.accept();
        else {
          alert.dismiss();
        }
        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.