Integrate BrowserMob Proxy with Selenium to Test Web Page Performance

While automating our Selenium tests, we might need to test our web page’s performance. While Selenium does not allow this out of the box, there are some tools we can use for this purpose. BrowserMob Proxy is one such tool. And the great thing about it is, you can easily pair it up with your Selenium tests. In this article, we are going to discuss the same. We will see how we can use BrowserMob Proxy with Selenium to test web page performance.

Apart from capturing performance data, BrowserMob can also manipulate browser behavior. And in case you want to simulate network traffic and latency, it lets you do that as well. All these great features comes at zero extra cost as it is an open source tool.

BrowserMob exports performance data in HAR format. We can run it in Embedded Mode or as a Standalone proxy server. But for our example here, we will run it in Embedded Mode. The reason for this is simple. We may not want to start BrowserMob manually every time our Selenium tests starts. So to avoid this manual intervention, we will start it using Embedded Mode.

Starting BrowserMob in Embedded Mode is simple. It’s just a matter of adding few lines of code. But in return, we get lot of benefits out of this. It provides a great deal of flexibility in terms of how we want to use it. For example, it can capture performance data for each of our tests.

OK, enough with the talks. We will see the practical part now.

Scenario: We will capture performance data of Google’s Privacy Policy web page. For that, we have already written a Selenium test method in Java. Now, we’ll add necessary code for BrowserMob proxy integration.

Using BrowserMob Proxy with Selenium

  • Add below dependency in your Maven file. If you are not using Maven, you can download BrowserMob’s JAR and reference it in your project.
  • Since we will start BrowserMob proxy along with Selenium, we need to change our setUp method as below.
  • At the end of our test, we will stop BrowserMob proxy with below code.
  • BrowserMob stores all the content in HAR file. So at the start of our test, we will need to create a new HAR file. We can do this with below code.
  • Also at the end of test, we need to store the HAR file. For this, just add below code at the end of your test.
  • After adding these lines in your test, your test method should look like below.
  • That’s it! You can now run your test. Once done, you should be able to see HAR file in your specified directory.

Viewing performance results

  • Now that you have HAR file, you can use different tools to view its content. Check out below screenshot from HTTP Archive Viewer, a Google Chrome plugin to view HAR files.
Integrate BrowserMob Proxy with Selenium

Test result in HTTP Archive Viewer

As you can see from above tutorial, using BrowserMob Proxy with Selenium is simple. Give it a try in your project and share your feedback with us in comments.

Comments
  1. apps4yourpc
  2. abiya
  3. Glenn
  4. NESRINE ZOUARI
  5. Hoang

Leave a Reply

Your email address will not be published.