Approach 1:
JMeter is an open source load testing tool, with many capabilities. One of the many interesting things which can be done is to integrate a WebDriver test into a JMeter test suite. JMeter can be used to record browser actions using proxy, but since JMeter doesn’t handle JavaScript, it cannot be used on its own to simulate realistic user interaction with a JavaScript/Ajax heavy web application.
Selenium can be used to record user-interaction with a web application where lots of interactions happen using Ajax.
It’s simple to integrate Selenium with JMeter.
Steps to Setup and configure Selenium WebDriver with Eclipse and Java:
1. Download and install Java.
2. Download and install Eclipse
3. Download WebDriver Jar Files.
4. Start Eclipse and configure it with selenium (WebDriver)
a. Select Workspace on eclipse start up (Webdriverwork)
b. Create new project ( File > New > Project > Java Project -> testproject)
c. Create new package (mytestpackage)
d. Create New Class (mytestclass)
e. Add external jar file to java build path
i. Right click on project 'testproject' > Select Properties > Select Java build path > Navigate to Libraries tab
ii. Click on add external JARs button > select both .jar files from D:\selenium-2.46.0.
iii. Click on add external JARs button > select all .jar files from D:\selenium-2.46.0\libs
That's all about configuration of WebDriver with eclipse. Now you are ready to write your test in eclipse and run it in WebDriver.
Code for Browser Mode
import org.junit.Test;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class mytestclass {
@Test
public void testing(){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.jmeter4u.com/");
System.out.println(driver.getTitle());
}
}
Code for Headless Mode
import org.junit.Test;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Mytestclass {
@Test
public void testing(){
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.jmeter4u.com/");
System.out.println(driver.getTitle());
}
}
5. Run your script from eclipse “Run” menu.
6. Export resources into JAR file.
7. Click on File -> Export the export dialog will appear.
8. Choose Java- >Jar file -> click next
On Jmeter to run selenium test script:
1. Copy the junit test class jar file to lib/junit folder in JMeter installation folder.
2. Launch JMeter and create a Test Plan | Thread Group | Junit Request Sampler.
3. The Junit Request window should display the Junit Test Class (com.bs.webtest.Mytestclass) as well as the test method (testing) automatically picked up by JMeter.
4. Save the test plan.
5. Run test plan.
Finally, jmeter script looks like below one:
Approach 2:
Web Driver Sampler automates the execution and collection of Performance metrics on the Browser (client-side). To use Selenium WebDriver with JMeter, simply install "WebDriver Set" plugins. The WebDriver sampler is super useful if you want to test for performance AJAX, GWT based web applications and simulated user actions.
This adds to the overall perceived performance of website/webapp, but this metric is not available in JMeter.
It is meant to compliment them by measuring the end user load time.
Steps to Setup for configuring WebDriver Plugin into Jmeter:
1. Download WebDriver plugins.
2. Unzip JMeterPlugins-WebDriver-1.2.1.zip
3. Unzipped files must be located in the lib/ folder.
4. Launch JMeter and create a Test Plan | Thread Group | Add "Config Element" -> "HTTP Cookie Manager" | Add "Config Element" -> "jp@gc - Firefox Driver Config" | Sampler -> "jp@gc - Web Driver Sampler" | "Listener" -> "View Results Tree".
Script for jp@gc - Web Driver Sampler
// Start capturing the sampler timing
WDS.sampleResult.sampleStart()
// Perform the Sampler task
WDS.browser.get("http://www.jmeter4u.com/");
// Verify the results
WDS.sampleResult.sampleEnd()
if(WDS.browser.getTitle() != 'Jmeter Training | Jmeter Experts | Jmeter Workshop | Jmeter Consultant') {
WDS.sampleResult.setSuccessful(false)
WDS.sampleResult.setResponseMessage('Expected title to be Jmeter Training | Jmeter Experts | Jmeter Workshop | Jmeter Consultant')
Finally, jmeter script looks like below one: