Using Selenium with JMeter's WebDriver Sampler
How to Use Selenium with JMeter's Webdriver Sampler
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.
Install the Webdriver using the JMeter Plugins Manager.
Write your WebDriver script as usual, then add "Thread Group" to your "Test Plan."
Config Element -> HTTP Cookie Manager, Config Element -> jp@gc - Firefox Driver Config, Sampler -> jp@gc - Web Driver Sampler, Listener -> View Results Tree.
The result is as follows:
You do not need to configure two config elements – you may omit that step. Open the "Web Driver Sampler" and add this code:
var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage'))
var button = WDS.browser.findElement(pkg.By.id('search_button_homepage'))
var link = WDS.browser.findElement(pkg.By.ByCssSelector('#r1-0 > div.links_main > h2 > a.large > b'))
WDS.log.info(WDS.name + ' has logged an entry');
(Don't worry if the entire code doesn’t seem clear yet. We'll revisit it below).
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 Firefox window that will open the website. Search for “BlazeMeter.” 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.
Our 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:
- WDS.browser.get('http://duckduckgo.com') - Opens the website http://duckduckgo.com
- var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage')) - Saves the search field into searchField variable.
- searchField.click() - Clicks the search field.
- searchField.sendKeys(['blazemeter']) - Types “blazemeter” in field
- var link = WDS.browser.findElement(pkg.By.ByCssSelector('#r1-0 > div.links_main > h2 > a.large > b')) - Saves selector as variable but uses CSS.
- WDS.log.info(WDS.name + ' has logged an entry') - Logs a message.
How to Use Selectors
To simplify the use of selectors, install the Selenium IDE add-on. Selenium IDE is a Firefox add-on with a recording option for actions in the browser. To get similar selectors, download and install the add-on. (Be sure to download the .xpi file.)
Open Duck Duck Go and Selenium IDE. Set the Selenium IDE’s base URL https://duckduckgo.com/. Type “blazemeter” and click Search. If you open Selenium IDE, you see the captured actions and selectors.
All captured data can be manually converted to the WebDriver format(see below).
Start the Test Run on BlazeMeter
To launch the WebDriver test in the cloud with more concurrent users on BlazeMeter, use Firefox, which is the only currently supported browser for use with WebDriver. Create a new test and upload your JMX file to run it.
Before uploading your JMeter script, it’s best to remove/disable View Results Tree, which can slow test performance. After a few minutes, reports will generate.
We launched the test with 40 concurrent users, as noted in the response time (*see the Monitoring tab).
Although we launched the test with only 40 users, the CPU is fully utilized from the outset. This is precisely why each sampler starts one browser. Be sure to take this into account when writing tests.
When using the WebDriver plugin, in order to perform better load testing, assemble the Selenium tests with JMeter tests. The number of WebDriver samplers should be fewer than the number of JMeter samplers. If you need any values from websites through Ajax, you can use WebDriver with the Once Only Controller to avoid continual/duplicate browser launches.
You can also use Taurus with its native Selenium executions, as a way to leverage existing functional tests written in Selenium.
Read more about Continuous Testing in our White Paper "Continuous Testing in Practice".
You might also find these useful:
Interested in writing for our Blog? Send us a pitch!