Automation BDD

Automating Web Performance Data Collection with Behat and BrowserMob Proxy

BrowserMob Proxy is a utility which is used for capturing HTTP traffic and performance data from the browser. BrowserMob-Proxy adds in essential missing capabilities such as checking HTTP status codes and injecting headers for HTTP Basic Auth. Web Perfomance data can be manually captured by other tools like Firebug or Developers Tools. Using BrowserMob Proxy we can capture perfonace data in HAR format while running automated tests. There is lots of food to learn about BrowserMob on thier official website.

In this article, we will see how to integrate BrowserMob Proxy with Behat. In order to get started we need to install PHPBrowserMob package.

PHPBrowserMob

Download latest version of the BrowserMob Proxy from the site. You can launch BrowserMob proxy like this

You will see something like this:

This means, BrowserMob Proxy is running correcrtly.

Download and start selenium version 2.25.0. Note that, proxy won’t work for some version of selenium server. Better Download latest.

Now, We need to install Behat by following official doc Or just follow my previous post on Behat installtion with composer.

Now create a feature file for exporting the performance data in the HAR format.

You need to include ‘PHPWebDriver‘ and ‘PHPBrowserMobProxy
We can implement the feature using following code :

Now, run ‘behat’ and watch the test running in the Browser. You will find data stored in the ‘/tmp/BROWSERMOBHAR’ file. You can use HAR viewer to see performance data in fancy way !

There are number of possibilities you can use this data. Here is best article which describe use of HAR file.

  • Calculate Load time.
  • Set/Get HTTP Headers during your tests
  • Capture performance data with HAR files.
  • Simulate network traffic and latency
  • Rewrite HTTP requests and respones

SourcCode on GitHub:

Source Code is available on GitHub repo ‘Behat-BrowserMobProxy‘ . Just give it a try !

Final Thoughts:

Combination of Behat, WebDriver and BrowserMob Proxy can be used for capturing the network trafic while running automated tests. We can use HAR files as per our need eg. Calculate Page load times, Page performance analysis etc etc..