Tuesday, September 2, 2014

Monitoring network activity via BrowserMob Proxy + HarStorage

Sometimes it's hard to understand a root cause of an issue while testing web applications, because bugs may not be as obvious to appear on UI. In such case it's important to pay attention to browser's network tab that can give us a lot of useful information about captured requests, e.g. internal server / JS errors.

We can capture network traffic automatically via BrowserMob Proxy that provides us useful REST API. After downloading and unzipping, you can start browsermob-proxy.bat to raise proxy server. Default port is 8080. You can change it by executing batch with -port argument.


Now we can use this proxy for listening traffic on a Selenium port. Generally you should only add an appropriate driver capabilities:


Where seleniumProxy configured to use BrowserMob proxy ip and allocated port:


To access BrowserMob proxy REST API, we can implement a simple REST client using Jersey. For example, to get a HAR file as String, we can send the following request:


You can find a link on a full source code below. And now let's focus on a second part of this article.

BrowserMob proxy allows us to retrieve HAR file and put it into stream. So we can easily save output json into file. It makes perfect sense when we need to run or debug just several tests. But what if we want to capture traffic for hundreds or thousands of tests? It would be much harder to find needed file. Besides that, we would also need to use some external tools to analyze generated HARs.

To make HAR files analysis more flexible, we can use HAR Storage. Just follow installation guide to raise the server for storing generated HAR statistics. By default it uses 5000 port.


To save generated by BrowserMob proxy HAR file into storage, we can use appropriate REST API:


If you run some tests and open HAR storage UI via browser, you can see the following user-friendly statistics:


You can find full source code with BrowserMob proxy and HAR Storage REST clients + simple Selenium test example on GitHub. Note that provided pom.xml already contains plugin for building a jar, if you want to add it into your project. Just exclude selenium / testng libraries and run clean install goals.

No comments:

Post a Comment