Automation BDD

BrowserMob-CLI : Ruby library to generate HAR from command line

Introducing BrowserMob-CLI

Recently, I wrote BrowserMob-CLI ruby gem which allows us to generate HAR files from command line.

This will preview HAR files in the Browser as well as parse it in YAML. It works well for FirefoxDriver.  ChromeDriver/GhostDriver will be coming soon. 

If you are new to HAR(HTTP Archive) & BrowserMob Proxy then please read these couple of links

HAR file can be used for

  • 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 response

BrowserMob-Proxy

It’s a CLI wrapper around 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 Performance data can be manually captured by other tools like Firebug or Developers Tools. Using BrowserMob Proxy we can capture performance data in HAR format while running automated tests. There is lots of food to learn about BrowserMob on  official website.

Why BrowserMob-CLI

Basically running BrowserMob proxy involves downloading binary which we can’t checkin into source control system. It takes time to learn how exactly browser-mob proxy works. BrowserMob-CLI can do following things for you.

  • Programatically Download Browsermob proxy  and save it in /tmp
  • Generate and Preview Har file from command line by passing URL
  • Generate and Preview Har file from command line by passing URL and adding user actions
  • List out request urls 

Installation

Before we start, please make sure you got following things in place

  • JAVA_HOME Environmental variable set in your .zshrc or .bashrc
  • wget
  • Ruby > 1.9.3
  • Bundler
  • Install Har gem

Add this line to your application’s Gemfile:

And then execute:

Or install it yourself as:

Gem Search and Download

browsermob-cli-install

Usage

Download BrowserMob Proxy

Assuming that you don’t have BrowserMob Proxy downloaded, then first thing we probaly need to download it and save it to your /tmp directory (Assuming /tmp has write access). Just run following command.

Now, you should have browsermob-proxy binary downloaded in the /tmp directory itself.

browsermob-proxy-download

Generate HAR file for any URL

We can generate HAR data for any url e.g (BBC website) by running

After running this command, you should see following

  • Firefox open up with BBC Blogs homepage within BrowserMob proxy
  • All request URLS printed on console
  • HAR file saved to /tmp/traffic.har
  • HAR data parsed in YAML format and saved to /tmp/traffic.yml
  • WEBrick server starts and open up HAR data to preview

generate_requests

Generate HAR file for any URL when user clicks on the page

We can generate HAR data for any url e.g (BBC website) and passing CSS Selector where you want to click

After running this command, you should see following

  • Firefox open up with BBC blog homepage within BrowserMob proxy and clicked on image
  • All request URLS printed on console
  • HAR file saved to /tmp/traffic.har
  • HAR data parsed in YAML format and saved to /tmp/traffic.yml
  • WEBrick server starts and open up HAR data to preview

View YAML File Or Preview in Browser

You can view generated YAML file with HAR data

The Har File in the browser

har_viewer

Source-Code

The source code is available on Github ‘browsermob-cli‘.

Video Demo

Thank you for reading ! Feedback welcome.