How to Use BrowserMob Proxy for Load Testing in Selenium

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 which can monitor the web app and measure the performance data. Hence, in this blog post, we’ll use 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 the learning purpose. And as always we suggest going through the best Selenium tutorials 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 that, 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. You can get its most recent version from the below download link.

Create a Selenium Load Testing Demo Using BrowserMob Proxy

BrowserMob – Selenium for Load Testing.

Use BrowserMob Proxy for Load Testing in Selenium.

1- How to Setup 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.

package com.techbeamers.loadtesting;

import java.io.File;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.browsermob.proxy.ProxyServer;
import org.browsermob.core.har.Har;

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.

This tutorial on “Using BrowserMob Proxy in Selenium for Load Testing” was in continuation of our promise to bring 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

  1. Hamza Reply
    • Meenakshi Agarwal Reply
  2. Manoj Reply
    • Hamza Reply
      • Meenakshi Agarwal Reply
  3. Ankit Reply
    • Meenakshi Agarwal Reply
  4. Roman Reply
    • Meenakshi Agarwal Reply