To use Selenium Webdriver with JMeter, install “Webdriver” plugins. The WebDriver sampler is useful if you want to test for performance AJAX or GWT based web applications.
Download WebDriver plugins from http://jmeter-plugins.org/downloads/all/
Unzip the files and copy in the lib folder under JMeter home directory.
To test if WebDriver plugins installed open Jmeter and test if there is jp@gc – Firefox Driver Config.
*If not, check your JMeter’s lib folder. Add the Webdriver sampler and Firefox or chrome driver config element. Insert the following code to launch Bing search and search for a text. Also add tree listener to view the result.var pkg = JavaImporter(org.openqa.selenium) var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait) var wait = new support_ui.WebDriverWait(WDS.browser, 20000) WDS.sampleResult.sampleStart() WDS.browser.get('http://www.bing.com/') var searchField = WDS.browser.findElement(pkg.By.id('sb_form_q')) searchField.click() searchField.sendKeys(['testing']) var button = WDS.browser.findElement(pkg.By.id('sb_form_go')) button.click() WDS.sampleResult.sampleEnd()Explanation of the code
- The code starts with the import Java packages “org.openqa.selenium” and “org.openqa.selenium.support.ui.WebDriverWait” which will allow you to use the WebDriver classes.
- WDS.sampleResult.sampleStart() and WDS.sampleResult.sampleEnd() captures sampler’s time and will track it. You can remove them, the script will still work but you will not get load time
- WDS.browser.get(‘http://www.bing.com/’) – opens the browser with Bing search website
- var searchField = WDS.browser.findElement(pkg.By.id(‘sb_form_q’)) – saves the search text box in searchField varialble
- searchField.click() – Clicks the search field to make it active
- searchField.sendKeys([‘testing’]) – Enters text to search
- var button = WDS.browser.findElement(pkg.By.id(‘sb_form_go’)) – Saves search button in a variable
- button.click() – clicks on the search button.
You are now ready to go. Do let us know your experience in using Selenium with JMeter
Thanks for the detailed guide, it’s easy to read and understand, much appreciated. I would also recommend to take a look at The WebDriver Sampler: Your Top 10 Questions Answered one, it highlights the most commonly asked questions and provides solutions (including sample code which can be used as a reference)