If you need to collect performance data and http web traffic between your browser and web server. You can use following 2 methods to make the process automatically with Selenium WebDriver.
- Log http traffic data into har files using Firebug and netExport Firefox add-on.
- Use BrowserMob Proxy as proxy server to route web page request and log http traffic data into har files.
No mater which method you choose, you need to know
What is har file?
HAR is abbreviation of HTTP Archive. You can make http web traffic visualized with this format files created by http communication tracking tools. This file include detail information about the communication between web browser and web server. You can also make analysis with all these information.
The har file format is similar with json, you can use jackson or gson libraries to parse the http web communication data in the log har file.
Use Firebug and netExport to capture web traffic
- Set up java project with eclipse if you do not create one. Please refer following topic.
How to create java project with myeclipse
How to fix java Unsupported major.minor version 51.0 Exception
- Add selenium webdriver jar library into your java project if you do not do this. Please refer following topic.
How to add selenium server standalone jar file into your java projectI prefer selenium-server-standalone-2.53.0.jar and Firefox 45.0.1.
- Download and add Firebug add-on in Firefox 45.0.1.
After add Firebug.1) Input following command in windows start run menu.
%APPDATA%\Mozilla\Firefox\Profiles\2) Copy the Firebug xpi file from your Firefox profile folder like following.
3) Save the xpi file into C:\Workspace\dev2qa.com\Lib.
- Download netExport Firefox extension.
I prefer netExport-0.9b3.xpiSave the download file to C:\Workspace\dev2qa.com\Lib
Now the preparation is done, we will show you how to coding in java.
- Create the Firefox profile which will be used by Selenium WebDriver and add Firebug and netExport extension to the profile.
- Set Firebug and netExport preferences to enable Firebug extension to capture http web traffic and save data into har formated log file.
- Initial Firefox browser using Selenium WebDriver.
- Capture http web traffic to har log file under C:\\Workspace\\dev2qa.com\\NetworkTraffic.
Use BrowserMob Proxy to capture web traffic
It is very easy to collect http communication information from browsers use BrowserMob Proxy. It can be used as a standalone or embedded proxy server. The proxy server also save the communication data in HAR format. The code below will use BrowserMob as a standalone proxy server to capture http traffic.
- Download browsermob-proxy-2.1.4-bin.zip
- Unzip the downloaded zip file to C:\Workspace\dev2qa.com\Lib.
- Add browsermob-dist-2.1.4.jar from following directory to your java project libraries.
- Go to C:\Workspace\dev2qa.com\Lib\browsermob-proxy-2.1.4\bin in command console, and start BrowserMobProxy server by run browsermob-proxy.bat.
- Create a new class CaptureTrafficWithBrowserMobProxy.java
- Initiate proxy server
- Create firefox webdriver using the running proxy server
- Use firefox webdriver browse page and log the traffic in har format.
How to visualize the har file.
You can use chrome extension har file viewer to visualize the content in the generated har file.