Tag Archives: firebug

Automating Web Performance Data Collection with Behat and BrowserMob Proxy

30 Oct

BrowserMob Proxy is a utility which is used for capuring 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.


$ sudo pear channel-discover element-34.github.com/pear
$ sudo pear install -f element-34/PHPBrowserMobProxy
$ sudo pear install -f element-34/Requests

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

$ cd browsermob-proxy-2.0-beta-6/
$ cd bin
$ sh browsermob-proxy -port 9090

You will see something like this:

$ sh browsermob-proxy -port 9090
INFO 10/30 21:29:50 o.b.p.Main           - Starting BrowserMob Proxy version 2.0-beta-6
INFO 10/30 21:29:51 o.e.j.u.log          - jetty-7.3.0.v20110203
INFO 10/30 21:29:51 o.e.j.u.log          - started o.e.j.s.ServletContextHandler{/,null}
INFO 10/30 21:29:52 o.e.j.u.log          - Started SelectChannelConnector@

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.

$ java -jar selenium-server-standalone-2.25.0.jar

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

$ sudo mkdir Behat-BrowserMob
$ sudo chmod -R 777 Behat-BrowserMob
$ cd Behat-BrowserMob/
$ behat --init

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

$ sudo vim BehatBrowserMob.feature
 Feature: BrowserMob Proxy with Behat
In order to check website performance
As a automated tester
I need to see network traffic captured in the HAR format

Scenario: Behat Bowsermob
Given I setup BrowserMobProxy
And I Navigate to "http://www.facebook.com/"
When I export HAR
Then I should see network traffic in the HAR file

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


use Behat\Behat\Context\ClosuredContextInterface,
use Behat\Gherkin\Node\PyStringNode,
use Behat\MinkExtension\Context\MinkContext;

require_once __DIR__ . '/PHPBrowserMobProxy/Client.php';
require_once __DIR__ . '/php-webdriver/PHPWebDriver/WebDriver.php';
require_once __DIR__ . '/php-webdriver/PHPWebDriver/WebDriverProxy.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
require_once 'PHPUnit/Autoload.php';

* Features context.
class FeatureContext extends BehatContext {

protected static $driver;
protected static $BrowserMob;
protected static $BrowserMobSession;
public $data;

* Initializes context.
* Every scenario gets it's own context object.
* @param array $parameters context parameters (set them up through behat.yml)
public function __construct(array $parameters) {


* @BeforeScenario
public function cleanup() {
$HARFile = "/tmp/BROWSERMOBHAR.php";
if (file_exists($HARFile)) {
echo "The file $HARFile exists, I am going to delete it \n";

* @Given /^I setup BrowserMobProxy$/
public function iSetupBrowsermobproxy() {
self::$driver = new PHPWebDriver_WebDriver();
self::$BrowserMob = new PHPBrowserMobProxy_Client("localhost:9090");
$additional_capabilities = array();
$webDriverProxy = new PHPWebDriver_WebDriverProxy();
$webDriverProxy->httpProxy = self::$BrowserMob->url;
$this->session = self::$driver->session('firefox', $additional_capabilities);

* @Given /^I Navigate to "([^"]*)"$/
public function iNavigateTo($url) {


* @When /^I export HAR$/
public function iExportHar() {
file_put_contents("/tmp/BROWSERMOBHAR.php", var_export(self::$BrowserMob->har, true));

* @Then /^I should see network traffic in the HAR file$/
public function iShouldSeeNetworkTrafficInTheHarFile() {


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..