Out of Box

How to run selenium script in JMeter

JMeter was originally built to provide an open source solution for load and performance testing. but JMeter can also be used for functional testing.

Selenium scripts can be executed in jmeter by selenium web driver.

To use Selenium Webdriver with JMeter, simply install “Webdriver Set” plugins. The WebDriver sampler is useful for testing the performance of AJAX, GWT-based Web applications, and simulated user actions.

Why needed?

With the advancement of technology, HTML5, JS and CSS improvements, more and more logic and behaviour have been pushed down to the client. Things that add to the overall browser execution time may include:

  1. Client-side Javascript execution – eg. AJAX, JS templates
  2. CSS transforms – eg. 3D matrix transforms, animations
  3. 3rd party plugins – eg. Facebook like, Double click ads, site analytics, etc

All these things add to the overall browser execution time,

This adds to the overall perceived performance of website/webapp, but this metric is not available in JMeter. JMeter is not a real browser so we cant measure the user experience at client side ,like page rendering/load time.

Performance mix is the practice to test application user experience while performing a load test. Web Driver Sampler automates the execution and collection of Performance metrics on the Browser (client-side). JMeter Load Test will put enough load on your system while the JMeter WebDriver plan will allow you to get the user experienced response times including page rendering.


  1. JMeter must be installed on your system if not then refer here.
  2. Install the Webdriver Set plugin using the JMeter Plugins Manager.

Write your WebDriver script as usual, then add “Thread Group” to your “Test Plan.”

Add below JMeter elements to test plan.

Config Element -> HTTP Cookie Manager
Config Element -> jp@gc – Chrome Driver Config
Sampler -> jp@gc – Web Driver Sampler
Listener -> View Results Tree.

The result is as follows:

Add chrome driver path to the “Path to Chrome Driver” .

Add below code to WebDriver and modify as per your business scenario :

var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui,org.openqa.selenium.support.events.EventFiringWebDriver);
var wait = new pkg.WebDriverWait(WDS.browser, 150)// WebDriver wait

WDS.sampleResult.sampleStart(); //captures sampler's start time
WDS.log.info("Sample started");

// Launch website specified in 'http://newtours.demoaut.com/'
WDS.log.info("Sample ended - navigated to http://newtours.demoaut.com/");

//Enter user name
var txtUserName = WDS.browser.findElement(pkg.By.name('userName')); //saves username field into txtUserName
txtUserName.sendKeys(['mercury']); //enter 'mercury' in user name
WDS.log.info("Enter UserName");

//Enter password
var txtPassword = WDS.browser.findElement(pkg.By.name('password')); //saves password field into txtPassword
txtPassword.sendKeys(['mercury']); //enter 'mercury' in password
WDS.log.info("Enter password");

//Click Sign in button
var btnLogin = WDS.browser.findElement(pkg.By.xpath('//input[@name=\'login\']')); //saves login field into btnLogin

//Click sign off link
var lnkSignOff = WDS.browser.findElement(pkg.By.xpath('//a[@href=\'mercurysignoff.php\']')); //saves sign off field into lnkSignOff


Now, try to start your test. Whatever you do, DO NOT change the “Thread Group” values. They must all be set to 1.

You should see the new Chrome window that will open the website. login to mercury website . After the test has started, open View Results Tree to confirm there are no errors. If the Response Code is “200” and the Response Message is “OK,” the test was run successful. If not, check the WebDriver script for errors. 

Code Review

Code starts with the import Java packages “org.openqa.selenium” and “org.openqa.selenium.support.ui.WebDriverWait” that allow you to use the WebDriver classes.

Here is a handy list of WebDriver’s packages.

If you want to use any of the packages, import them with JavaImporter:

var action = JavaImporter(org.openqa.selenium.PACKAGENAME.CLASSNAME)

WDS.sampleResult.sampleStart() and WDS.sampleResult.sampleEnd() captures the sampler’s time and tracks it. You can remove them. The script will still work, but you can’t get load time:

var pkg = JavaImporter(org.openqa.selenium, org.openqa.selenium.support.ui,org.openqa.selenium.support.events.EventFiringWebDriver); – importing supporting packages
var wait = new pkg.WebDriverWait(WDS.browser, 150) – For web Driver wait

WDS.browser.get(‘http://newtours.demoaut.com/’); – Opens the website

wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.name(‘userName’))); – Wait for userName field to be located

var txtUserName = WDS.browser.findElement(pkg.By.name(‘userName’));
Saves the username field into txtUserName variable.

txtUserName.sendKeys([‘mercury’]); – Enter ‘mercury’ value in userName field

Similarly for password

wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath(‘//input[@name=\’login\’]’))); – wait for sign in button to be located

var btnLogin = WDS.browser.findElement(pkg.By.xpath(‘//input[@name=\’login\’]’));
Saves the sign in button into btnLogin variable.

btnLogin.click(); – Click on sign in button

Some useful code :

Different locators :


WDS.browser.findElement(pkg.By.id(‘elementId’) );

WDS.browser.findElement(pkg.By.xpath(‘elementXpath’) );

WDS.browser.findElement(pkg.By.ByCssSelector(‘elementXpath’) );


Wait Condition:



java.lang.Thread.sleep(2000); // static wait

Other Stuff:

WDS.browser.getCurrentUrl(); – To get the current url

WDS.browser.getWindowHandle(); – To fetch single handle

WDS.browser.getWindowHandles(); – To fetch multiple handles

WDS.browser.switchTo().window(handle); – Switch to another window

WDS.browser.executeScript(‘javascript’) – To execute java script code

var element = WDS.browser.findElement(org.openqa.selenium.By.id(‘Title’))
var select = new org.openqa.selenium.support.ui.Select(element)
– To select a value from drop down




Categories: Out of Box, Selenium

Tagged as:

4 replies »

Leave a Reply