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
- What is HAR
- HAR Spec
- BrowserMobProxy
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:
1 |
gem 'browsermob-cli' |
And then execute:
1 |
$ bundle install |
Or install it yourself as:
1 |
$ gem install browsermob-cli |
Gem Search and Download
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.
1 |
$ browsermob-cli setup |
Now, you should have browsermob-proxy binary downloaded in the /tmp directory itself.
Generate HAR file for any URL
We can generate HAR data for any url e.g (BBC website) by running
1 |
$ browsermob-cli show_har http://www.bbc.co.uk/blogs |
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 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
1 |
$ browsermob-cli show_har_click http://www.bbc.co.uk/blogs --locator ".rsp-img" |
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
1 2 3 |
$ browsermob-cli view_har_in_YAML $ browsermob-cli view_har_in_browser |
The Har File in the browser
Source-Code
The source code is available on Github ‘browsermob-cli‘.
https://github.com/Shashikant86/browsermob-cli
Video Demo
[embedyt] http://www.youtube.com/watch?v=X_xQ7ja_GtE[/embedyt]
Thank you for reading ! Feedback welcome.