Please post your Web Driver questions in official Web Driver forum

Tuesday, October 30, 2012

Capture network traffic using WebDriver

We often come across testing requirements when we need to analyze the network traffic to find -
    • HTTP status of page
    • Analyze header information to find if right information is passed
    • Validating parameters related to ajax requests etc
Selenium 1 has had a way to capture n/w traffic but the feature does not always work as expected. At times Selenium 1 does not capture all n/w traffic, And given that Selenium 1 APIs are almost dead it is
worth using solutions available for Selenium 2 (WebDriver). I have come across two solution of capturing n/w traffic with WebDriver -
  • Browser Mob Proxy (BMP) - which acts as proxy server b/w WebDriver and your application, captures all the transactions b/w browser and application and creates a HAR file. Sounds interesting? But I came across a few issues using BMP - 


    1. Test execution goes sluggish because of using proxy server
    2. Test steps go abrupt at times, for example you may have to click same button twice/thrice to perform operation
    3. BMP does not support capturing n/w traffic against application running on localhost (at least not at the time of writing this post.) 
If you get to work with BMP then bigger advantage is its capability of capturing network traffic across all browsers. But if your test require you to use only Firefox then there is more elegant solution available, that is to use 
Firebug is as popular tool among selenium users as it is among developers. NetExport is add on to firebug which captures network traffic in form of HAR files. HAR file is json based file, refer HAR specification for more. NetExport can export all the n/w traffic in HAR format as detailed in image here












Manual operations of exporting HAR file can be automated using WebDriver.
WebDriver can easily configure firefox plug-in and step through the application to get the required HAR file generated. Refer this excellent example of configuring Firefox driver with firebug and netexport add ons. Once you get the HAR file then HarLib can be used to fetch the required data from HAR file.
Fork me on GitHub