LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

Running Selenium scripts with JMeter

I have Selenium automation scripts ready with functional flow, now I want to integrate those scripts with JMeter for load-testing. Is that possible?If so how to integrate both? My first aim is to run the automation script using selenium than run those scripts in jmeter for load or performance testing.

Up Vote Down Vote 5 Votes
Posted 5 months ago

Answers


How to run selenium script in JMeter

Content source - QA Automation Blog

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.

Pre-requisite:

  1. JMeter must be installed on your system if not then refer here.
  2. Install the Webdriver Set plugin using the JMeter Plugins Manager.
  3. 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 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.sampleResult.getLatency();
WDS.log.info("Sample started");

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

//Enter user name
wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.name('userName')));
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
wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.name('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
wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath('//input[@name=\'login\']')));
var btnLogin = WDS.browser.findElement(pkg.By.xpath('//input[@name=\'login\']')); //saves login field into btnLogin
btnLogin.click();


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


WDS.sampleResult.sampleEnd();
Up Vote Down Vote 2 Votes
Posted 5 months ago