If you’ve read one of our earlier posts on exploring Selenium for load testing, then you would admit it as a load-testing solution. But being a UI testing component you can only use it to make requests to a web application. In fact, you’ll need something else that can monitor the web app and measure the performance data. Hence, in this blog post, we’ll use the BrowserMob proxy to create the Selenium load-testing demo. It’s a free tool and can run with Selenium for load testing the web apps.
Also, in this tutorial, we’ll create a Selenium load-testing demo project in Eclipse for learning purposes. And as always we suggest going through the best Selenium tutorial to get quick help. In fact, reading them will ensure that you are ready with a simple project before getting to the next section. So, you can quickly test the Selenium load testing code snippet given in the later part of the post.
A few words about the BrowserMob proxy, it helps in capturing the performance data for a web app using the Selenium Webdriver code sample. It records the performance readings in the HTML archive i.e. HAR format. You can even utilize it for tuning the browser and the incoming traffic. It can let you block/unblock content and allows editing the HTTP requests/responses packets.
How to Use BrowserMob Proxy and Selenium for Load Testing
1- How to Setup the BrowserMob Proxy with Selenium?
In this Selenium load testing demo, first of all, we’ll create a proxy server instance and run it on any port, say 8081. Then, we’ll add a proxy attribute to set the Webdriver capabilities. Please see the below code snippet.
// Start the BrowserMob Proxy. ProxyServer svr = new ProxyServer(9090); svr.start(); // Get the Selenium proxy object. Proxy proxy = svr.seleniumProxy(); // Set desired capability for using proxy Server. DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, proxy); // Start the Browser with desired features. WebDriver driver = new FirefoxDriver(capabilities);
Consequently, we’ll load the Firefox browser and run a small Selenium Webdriver test.
After running the test, we’ll call the BrowserMob API to read the perf data and export it in HAR format.
// Collect the performance data from the BrowserMob proxy server. // Get the HAR data. Har har = server.getHar(); // Write the HAR Data in a File File harFile = new File("C:\\<PATH>\\sample.har"); har.writeTo(harFile); // Stop the BrowserMob Proxy Server server.stop(); // Close the browser driver.quit();
2- Selenium Load Testing Demo – Full Coding Snippet.
Below is the complete code for running a load use case. However, there is a package shortcut used in the code which you can replace with the following.
Shortcut | Replace the shortcut with the following if needed. |
---|---|
import org.openqa.selenium.*; import org.browsermob.*; | You may choose to import a specific class as per your use case: For example, in the case of org.openqa.selenium , you may use:- Keys; Similarly, for org.browsermob , you may use these:- proxy.ProxyServer; |
Let’s check out the code below.
package com.techbeamers.loadtesting; import java.io.File; import org.openqa.selenium.*; import org.browsermob.*; public class SeleniumLoadTesting { public static void main(String[] args) throws Exception { // Start the BrowserMob Proxy. ProxyServer server = new ProxyServer(8081); server.start(); // Get the Selenium proxy object. Proxy proxy = server.seleniumProxy(); // Set desired capability for using Proxy Server DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, proxy); // Start the Browser up. WebDriver driver = new FirefoxDriver(capabilities); // Create a new HAR with the label "StockMarketData" server.newHar("StockMarketData"); // Open the Google homepage. driver.get("http://www.google.com"); // Find the search edit box on the Google page. WebElement searchBox = driver.findElement(By.name("q")); // Type in Selenium. searchBox.sendKeys("Selenium"); // Find the search button. WebElement button = driver.findElement(By.name("btnG")); // Click the button. button.click(); Thread.sleep(5000); // Collect the performance data from the BrowserMob proxy server. // Get the HAR data. Har ar = server.getHar(); // Write the HAR Data in a File File arFile = new File("C:\\<PATH>\\sample.har"); ar.writeTo(arFile); // Stop the BrowserMob Proxy Server server.stop(); // Close the browser driver.quit(); } }
Final Word – BrowserMob Proxy and Selenium for Load Testing
This tutorial on “BrowserMob and Selenium” was in continuation of our promise to make each topic of your interest as simple as you could perceive it. Hence, we tried to add all micro-level details about using BrowserMob proxy with Selenium.
We wish this post could have made you more informed than you were before reading it.
All the Best,
TechBeamers
9 Comments
I had this exception error :
Exception in thread “main” java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
at org.browsermob.core.har.Har.writeTo(Har.java:34)
at GetMyHAR.main(GetMyHAR.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.map.ObjectMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Hi – It’s a common issue due to the missing dependencies of following Jackson packages.
>jackson-core-asl-*.*.*.jar
>jackson-mapper-asl-*.*.*.jar
And the *.*.* means that you need to find a version of Jackson libs compatible to browser-mob.
Hi,
With the above setup i was able to capture content in HAR format when i run my tests locally but when tried to run tests on a VM or on cloud, the browser opens up but does not load anything because there will be no internet connection for that session. Reason being, i am opening up a port locally and since tests are running in cloud, it will try to access this port on cloud which will be connected to a different network. How do we resolve this ?
I get the solution by adding the jackson.jar package Now I have other issue it’s about geting an empty har file like this :
{“log”:{“version”:”1.1″,”creator”:{“name”:”BrowserMob Proxy”,”version”:”2.0″},”pages”:[{“id”:”StockMarketData”,”startedDateTime”:”2017-07-19T14:43:48.890+0000″,”title”:””,”pageTimings”:{}}],”entries”:[]}}
What’s wrong with that ?
Hi – You may need to initialize the http, ssl, ftp ports of the browser after starting the browser mob proxy. Please try after providing these settings.
Thanks for the tutorial.
I am unable to capture the header and cookies in har file through browsermob.
can you please let me know (example) how can I add the header in har file also when I am trying to parse .har file then it’s showing me error.
Thanks in advance.
Ankit Jain
You can extract the headers using the REST API.
By setting “captureHeaders=true” as an URL parameter while starting the HAR recording.
Example:
http://locahost:<port>/proxy/<proxy_port>/har?initialPageRef=ProxyTest&captureHeaders=true
Thank you for the tutorial. The only issue I am running into so far is that the download does not contain a single JAR that could be added to the project in Eclipse as External JARs: the download contains only the JAVA files. How should they be included? Thanks.
Hello – I’ve just checked the BrowserMob download link and was able to download the “browsermob-proxy-2.0-beta-6-bin.zip”. And upon extracting this zip, I could find the “browsermob-proxy-2.0-beta-6.jar” file under the “lib” folder. It is the file which you need to add as an external jar in your Selenium load testing project. Hope, it would answer your query.