JMeter-Selenium Webdriver Integration

Reading Time : 6min read
JMeter-Selenium Webdriver Integration

The combination of using JMeter and some simple scripting to invoke Selenium code allows you to measure how fast HTML pages take to load. And with Headless browser execution, the test execution time can be reduced up to 30% and lets you continue to use your computer while the tests execute in the background.

Find out why performance testing is essential in establishing an exceptional customer experience. Contact Kobiton Today!

As Web driver restricts our ability to scale due to its high usage of CPU, combining it with a  well-organized JMeter load Test helps.

One of the most significant advantages we have here is achieving the performance matrix by avoiding multiple browsers opening up and minimizing the overall CPU usage and speed.

There are two primary ways to achieve this integration:

  • We can do client-side performance analysis using ‘Web driver Samplers’ in JMeter using Selenium/Web driver Support Plugin
  • Selenium Scripts, which are already created, using Eclipse IDE can be integrated by exporting Selenium scripts jars and placing the jars in lib Junit folder
Get a free trial from Kobiton today!

Using Selenium/Webdriver Support Plugin

Open JMeter after successful installation.

As we all know, JMeter is an open-source java application. There are custom plugins which one needs to install separately after JMeter installation, so one can use additional features using the Plugins Manager.

Install custom Plugin using Plugin Manager

1. Download JMeter Plugin Manager jars file and add these jars to it: jmeter/lib/ext directory folder and Restart JMeter (Go to File->Restart )

  • Navigate Plugins Manager after restarting JMeter, navigate to “Options” from JMeter IDE and click on Plugins Manager as seen in screenshot
  • Now click on the Available tab of Plugin Manager and type Selenium/Webdriver Support
    And select  Selenium/Webdriver Support, click on Apply changes and restart JMeter
  • Create a Test Plan and add a thread group
    Right-click on Test Plan and Add Thread group

2. Add Web driver Sampler

  • The execution and collection of Performance metrics on the Browser (client-side) can be achieved using the WebDriver Sampler.

    With the help of WebDriver sampler, every browser consumes a significant amount of resources and every thread will have a single browser instance.

  • Right-click on Thread group, click on Add>Sampler>jp@gc – WebDriver Sampler

3. Add any Listener

  • Right-click on Thread group, click on Add>Listener>View Result tree

4. Add Config Element : Right-click on Thread Group-> New set of Configuration Element will be visible after one import Selenium/Webdriver

  • Support Plugin such as:jp@gc – Chrome Driver Config
    jp@gc- Firefox Driver Config
    jp@gc-HtmlUnit Driver Config
    jp@gc-Internet Explorer Driver Config
    jp@gc-Remote Driver Config

    Add “jp@gc – Chrome Driver Config” under thread group

5. Download Driver and Provide Path

6. Provide Chrome driver path in Chrome driver Config

  • Go to Chrome Driver Config and click on chrome tab next to proxy tab and provide the path of Chromedriver.exe

7. Add your selenium scripts to Webdriver Sampler

  • Click on jp@gc – WebDriver Sampler
  • Select your script language in which you have written your selenium code for example JavaScript
  • Under the script field add your script between
  • WDS.sampleResult.sampleStart() and WDS.sampleResult.sampleEnd()
  • Sample Selenium code in java for searching Automation in web browser

  driver.browser.get('https://google.com')driver.sampleResult.sampleEnd()  driver.sampleResult.sampleStart()  driver.browser.get("https://www.google.com/");  var searchBox = driver.browser.findElement(By.name("q"));  searchBox.click();  searchBox.sendKeys('Test Automation');  searchBox.sendKeys(Keys.ENTER); So while introducing your scripts in jp@gc - WebDriver Sampler one has to make the following changes:
  
   Replace driver keyword with WDSReplace By class with org.openqa.selenium SAMPLE Script for jp@gc - WebDriver Sample
    WDS.sampleResult.sampleStart()    WDS.browser.get('https://google.com')    WDS.sampleResult.sampleEnd()    WDS.sampleResult.sampleStart()    WDS.browser.get("https://www.google.com/");    var searchBox =       WDS.browser.findElement(org.openqa.selenium.By.name("q"));    searchBox.click();    searchBox.sendKeys('Test');    searchBox.sendKeys(org.openqa.selenium.Keys.ENTER);   WDS.sampleResult.sampleEnd()   

8. Run and Validate

  • In the Thread Group section one can increase number of users/ramp up time
    And evaluate results in the Listener opted to view and analyze Results.

Setting up Junit Test Cases into JMeter

With Selenium we can write functional automation test cases, and we can put load on those scripts which are already written and validate the response time and other performance parameters

Prerequisites

  1. A Sample Selenium Script with Junit Jars Download Jars and Selenium Standalone Jars Download
  2. Latest Java version (Download Java )must be installed and the Java path must be set. (Java Path)
  3. JMeter must be installed here

SAMPLE JUNIT SCRIPT

mport org.junit.Test;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.WebDriver;
public class sample{
@Test
public void testing()
{
 System.setProperty("webdriver.gecko.driver", "C:\\Users\\Driver\\driver\\geckodriver.exe");
 WebDriver driver = new FirefoxDriver();
 driver.get("https://google.com");
 System.out.println(driver.getTitle());
 }
 }

1. Export Selenium Jars

  • Navigate to your Java project by clicking on Project Explorer in the left side of panel of Eclipse IDE, click on class file you wish to export, then right-click on class file and click on export in the dropdown option. Under java folder, click on Jar file and click on Next.
  • Select jar file download location and provide the file name of the jar.
    Now copy the jar and place it under junit folder where JMeter is installed
    ApacheJmeter
    Example : apache-jmeter-5.2.1\lib\junit\Sample.Jar

2. Open JMeter

  • Under Test Plan, add a thread group (Test Plan right-click->Add thread group)
  • Add Junit Sampler Request (Thread group->Add Sampler->Junit Sampler)
    Click checkbox Search for Junit4 annotation. Once you click the checkbox, you will observe the “Class Name” and “Test Method” text fields are auto-populated based on the script method

HEADLESS BROWSER CONCEPT : htmlUnitDriver

More thread leads to more browser sessions, which ultimately deteriorate the performance to quite an extent.

NO MORE MULTIPLE BROWSER SESSION

HtmlUnitDriver helps to run the test case without opening the web browser i.e., in Headless mode. Html Unit driver is a better choice as it increases the execution speed and CPU usage while maintaining Selenium API. Headless testing is simply running your Selenium tests using a headless browser. It operates as your typical browser would, but without a user interface, making it excellent for automated testing

How to Achieve this?

When we integrate Selenium- Junit scripts in JMeter, we need to choose the browser driver as htmlUnitDriver in the Selenium- Junit scripts.

Download the htmlunit-driver and in sample script change the

SAMPLE JUNIT SCRIPT using htmlunitDriver

import org.junit.Test;
 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 import org.openqa.selenium.WebDriver;
public class sample{
@Test
public void testing()
{
System.setProperty("webdriver.HtmlUnitDriver.driver", "C:\\Users\\Driver\\driver\\htmlunitdriver.exe");
WebDriver driver = new HtmlUnitDriver();
driver.get("https://google.com");
System.out.println(driver.getTitle());
}
}

Export this class file as jar file and label it with class name and replace this in location

Example: apache-jmeter-5.2.1\lib\junit\Sample.Jar

Restart JMeter and reopen the Junit Selenium .jmx file and run with headless mode with the updated ‘sample’ file which has headless browser – HtmlUnitDriver.

Summary

With an increasing focus on the user experience, performance testing is essential.

It is important to know the overall page load time during load testing to establish speed and scalability of software applications. As shown, the integration of JMeter with Selenium allows for headless testing and improved scalability of your tests.

Looking for a robust performance testing solution? Demo Kobiton Today!

Get a Kobiton Demo

Interested in Learning More?

Subscribe today to stay informed and get regular updates from Kobiton

Ready to accelerate delivery of
your mobile apps?

Request a Demo