Skip to main content

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.

Comments

  1. Hi, I tried this and it is working fine for the normal sites like facebook and so but when i am running this for my company site walmart.com then its not generating .har file.

    Can you please help me with this? I am stuck here.

    ReplyDelete
    Replies
    1. Is there some ajax driven operation for which you are gathering data? If so then ajax has been pain to capture n/w traffic. If it is a normal page then should have worked. In either case, I would ask you to add more details.

      Delete
  2. Hi Tarun,

    After adding some more preferences like
    profile.setPreference(domain + "currentVersion", "2.0");
    profile.setPreference(domain + "allPagesActivation", "on");
    profile.setPreference(domain + "defaultPanelName", "net");
    profile.setPreference(domain + "net.enableSites", true);

    profile.setPreference(domain + "addonBarOpened", true);
    profile.setPreference(domain + "console.enableSites", true);
    profile.setPreference(domain + "script.enableSites", true);
    profile.setPreference(domain + "previousPlacement", 1);
    profile.setPreference(domain + "allPagesActivation", "on");
    profile.setPreference(domain + "onByDefault", true);

    Now i am able to get har file of www.walmart.com but still for websites like www-e4.walmart.com(which is internal test env, replica of production site ), not able generate har file.

    Architecture and design wise both are same, so could it be some security related issue.

    ReplyDelete
    Replies
    1. Does not seem any thing explicitly wrong. Could you post this question here - https://groups.google.com/forum/#!forum/firebug
      This is official forum for firebug.

      Delete

Post a Comment

No spam only genuine comments :)

Popular posts from this blog

Appium and android mobile app automation

Next appium and Android mobile app automation video tutoria l is live. If you are new to appium then please check - appium-tutorial This video tutorial covers - Start vysor (Just for this session and not mobile automation :)) Start appium and start appium inspector Desired Capabilities platformName - Android deviceName - L2N0219828001013 (as seen on "adb devices") Saved Capability Sets Start Session Scan app elements using appium inspector Get appPackage and appActivity using "APK info" app Install "APK info" app and open app whose appPackage and appActivity are required i.e. calculator Check top section of app icon com.android.calculator2 is app package com.android.calculator2.Calculator is app activity testng.xml file settings for running Android app tests Test details com.seleniumtests.tests.mobile.AndroidAppTest and CalculatorScreen class View beautiful STF test report  

Return only first or last element from webelements collection

Note: If you are new to java and selenium then start with selenium java training videos .     We often come across situation when there are multiple elements on a page and we probably like to exercise only a few of them using selenium webdriver. May be just first and last element. For example on a search result page we may like to click on only first and last link and not all. This is when Iterables API comes handy. (By the way I am assuming that you have already completed watching selenium training videos :)). Once we have collection of web element then we can use Iterables to get only first or last element as following - Consider that we fetch collection of element as - List< WebElement > webElements = getDriver().findElements(By. id ( "htmlID" ));   Now we can get the first web element from this collection as -  WebElement firstElement = Iterables. getFirst (webElements,  getDriver().findElement(By. id ( "defaultElement" )));   Herein second

Using chrome console to test xPath and css selectors

Note: If you are new to java and selenium then start with selenium java training videos .       Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application - Hit escape key and console would open another pane to write element locators - And now you can start writing xPath or css selectors in chrome console and test them - The syntax for writing css id - $$(“ ”) And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed - If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application - If you want to clean console of previously wri