Wednesday, February 28, 2018

Selenium Webdriver Scripts Using Jmeter

1.     Introduction

Modern day websites and the web applications are using and a lot of advanced features to make the client experience better and AJAX and JavaScript based logic plays a vital role in developing such features. Companies are also trying to make the websites prettier by using some cool graphics. However, this demands an increased level of processing at the browser end and using some rich graphic content that could make the application performing slowly if the enhanced features are not handled properly.

Most of the leading performance testing tools capture the response time on the protocol level which doesn’t include the client side rendering time. In addition, it requires advanced level of scripting skills in simulating AJAX calls using conventional load testing tool. Selenium webdriver scripts are a good choice to test the E2E performance of the web applications and to capture the response time inclusive of client-side rendering time.

To use Selenium Webdriver with Jmeter simply install the Webdriver set of plugins using the Plugins Manager which is available at https://jmeter-plugins.org/wiki/PluginsManager/

Simply follow the given steps:
-       Download the Plugins Manager JAR file
-       Put the file in the lib/ext directory
-       Restart JMeter
-       Click “Options” and then “Plugins Manager”



2.     Download Webdriver Plugins

Once you have “Plugins Manager” installed, you can simply go to available Plugins section and down download “Selenium/Webdriver Support” plugin. This will require a restart of Jmeter.

In the following diagram “Selenium/Webdriver Support” plugin is already downloaded and available under Available Plugins section.


After the Jmeter restart, you can find the “Webdriver Sampler” under the sampler section

 
 Webdriver set of plugins support Firefox browser out of the box. However, if you like to use Chrome or Internet Explorer then it will require some additional setup.

To use Chrome or Internet Explorer browser, simply download Chrome or IE driver using the below link and provide the path if the driver under the driver Config Element



Chrome driver config can be download from https://jmeter-plugins.org/wiki/ChromeDriverConfig/ and place the .exe file under the Bin folder of Jmeter or set the path of the EXE file under driver config.



We are ready to begin with the scripting now. All you need to add in your test plan is the Chrome Driver Config Element, Web Driver Sampler (Number of samplers depends on the testing needs), Cookie Manager, Cache Manager and View Results Tree.


3.     Webdriver Sampler Scripting

In this example, we are going to script a simple flow of opening the duckduckgo.com website, Searching Load testing Keyword and clicking on one the results link.
Here is the code you will require to do this:

//Common Code in all the Samplers

var pkg = JavaImporter(org.openqa.selenium); //Import Java Selenium Pacage
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver wait package classes
var wait = new support_ui.WebDriverWait(WDS.browser, 5000); //Wait 5 seconds until page loads

WDS.sampleResult.sampleStart(); //Sampler starting point
WDS.log.info("Sample started"); // This will log the information for us

// Transaction related code

WDS.sampleResult.sampleEnd();

//Transaction-1 Launch Website

WDS.browser.get('http://duckduckgo.com'); //opens website specified in 'http://duckduckgo.com'
WDS.log.info("navigated to duckduckgo.com");

//Transaction-2 Search Keyword

var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage')); //saves search field into searchField
searchField.click(); //clicks search field
searchField.sendKeys(['Load testing']); //types word "blazemeter" in field
WDS.log.info("Searched for Load testing");

var button = WDS.browser.findElement(pkg.By.id('search_button_homepage')); //Find Search button
button.click(); //Click Search Button
WDS.log.info("Clicked on the search button");

//Transaction-3 Open Link

var link = WDS.browser.findElement(pkg.By.id('r1-0')); // Clicks on the first link.
link.click(); //Click the search result's Link

var link = WDS.browser.findElement(pkg.By.id('firstHeading')); // wait for the page to be loaded

The script would look like this:



4.     Selenium IDE Chrome Extension Plugin

To use the selectors we can use the “Selenium IDE” Add-on, which is available at https://docs.seleniumhq.org/download/ . Selenium IDE is a Firefox add-on with a recording option for actions in the browser. To get similar selectors for other browsers, download and install the add-on.

In this example, I have downloaded the Selenium IDE extension for Chrome. Once it is successfully installed, you can see the icon in the browser



Open Duck Duck Go and Selenium IDE. Set the Selenium IDE’s base URL https://duckduckgo.com/ and start recording. Type “Load testing” and click Search open the link after that. If you open Selenium IDE, you see the captured actions and selectors.



All the captured actions can be manually converted to the webdriver format


Action and Command
Webdriver Code
Launch Website
Open
WDS.browser.get('http://duckduckgo.com');
Click at Search Box
Click at id= id=search_form_input_homepage
var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage'));
searchField.click();
Type Keyword
Type id=search_form_input_homepage
searchField.sendKeys(['Load testing']);
Click Search Button
Click at id= search_button_homepage
var button = WDS.browser.findElement(pkg.By.id('search_button_homepage'));
button.click();

No comments:

Post a Comment