Thursday, May 9, 2013

Automated Website Testing with FiddlerCore

There are various UI automation tools to automate your website and validate the UI. However, it’s also important to capture and validate the network traffic. This can be done using FiddlerCore. You can also perform various types of testing like low bandwidth testing, mobile device testing and localization testing using FiddlerCore.

You can download FiddlerCore from http://fiddler2.com/fiddlercore

To start FiddlerApplication, call FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default);

Enable HTTPS Decryption

You can enable HTTPS Decryption while starting Fiddler Application

 FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.DecryptSSL);

 You also need to create and import Root cert for Fiddler

 if (!CertMaker.rootCertExists())
{
            if (!CertMaker.createRootCert())
            {
                        throw new Exception("Unable to create cert for FiddlerCore.");
            }
}

X509Certificate2 oRootCert = CertMaker.GetRootCertificate();
X509Store certStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadWrite);

try
{
            certStore.Add(oRootCert);
}
finally
{
            certStore.Close();
}


Capture Network Traffic

You can capture the network traffic in a list and then perform various validations like checking for response time, response string, etc. for the captured sessions.
List<Session> _sessions = new List<Session>();

FiddlerApplication.BeforeRequest += delegate(Session oSession)

            {

                Monitor.Enter(_sessions);

                _sessions.Add(oSession);

                Monitor.Exit(_sessions);

            };


Simulate Modem Speeds


You can add a delay of specific milliseconds per KB uploaded as well as for per KB downloaded. This simulates a slow network and can be used to test scenarios where specific requests shouldn’t be made on a slow network. For example, Yahoo mail downloads an image on good network but doesn’t on a slow network.

High Bandwidth

 Low Bandwidth
 
FiddlerApplication.BeforeRequest += delegate(Session oSession)

            {

                if (!_applyrule) return;

                oSession["request-trickle-delay"] = sendDelayinMillisecPerKb;

                oSession["response-trickle-delay"] = receiveDelayinMillisecPerKb;

            };

Mobile Device Testing


Most websites rely on the User Agent string to determine if a request is coming from a mobile device. Hence to test the behavior of a website on a mobile device, corresponding User Agent is set using the Fiddler proxy. For example, when you send the user agent for WP 7.5 i.e. Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator) , Gmail looks different than that on IE 9

Internet Explorer 9

 
Windows Phone 7.5

 
FiddlerApplication.BeforeRequest += delegate(Session oSession)

            {

                if (_applyrule)

                {

                    oSession.oRequest["User-Agent"] = userAgent;

                }

            };

Localization Testing


Localization testing can be performed by requesting the content in a specific language. This is done by modifying the Accept-Language header using the Fiddler proxy. For example, Hotmail looks the opposite for English and Hebrew Language.

English (en-US)
 

Hebrew (he)

 
FiddlerApplication.BeforeRequest += delegate(Session oSession)

            {

                if (_applyrule)

                {

                    oSession.oRequest["Accept-Language"] = languageAbbreviation;

                }

            };

 

13 comments:

  1. I have a website and I would like to monitor the traffic on the website using fiddler. Assume that the site is being used by my vendor(single user), can I use FiddlerCore to save the Fiddler sessions on my server as a log? Basically i would like the Fiddler trace of all the activities done on my website by that vendor.

    ReplyDelete
  2. Yes, you can do this by running FiddlerCore on the client machine from where the user is performing the activities.

    ReplyDelete
  3. Hi, I am looking at service virtualization options for an application that is outside our domain..can I use FiddlerCore to send auto customized responses?

    ReplyDelete
  4. I am not sure about your complete scenario. You can try installing fiddler and check if you can capture the traffic in fiddler. If yes, you should be able to use FiddlerCore to modify the request/response

    ReplyDelete
  5. Does this sample code, export raw data of all session ?
    Same what we manually do in Fiddler -> File -> Export sessions -> All sessions ?

    ReplyDelete
  6. You can export the session using
    Monitor.Enter(_sessions);
    var dictOptions = new Dictionary { { "Filename", filename } };
    SazFormat saz = new SazFormat();
    saz.ExportSessions("SAZ", _sessions.ToArray(), dictOptions, null);
    Monitor.Exit(_sessions);
    You can find a detailed sample at http://www.telerik.com/fiddler/fiddlercore

    ReplyDelete
  7. I want to capture https traffic using fiddler core and selenium.I have tried as below
    CONFIG.bCaptureCONNECT = true;
    CONFIG.IgnoreServerCertErrors = true;
    CONFIG.bMITM_HTTPS = true;

    and flags as FiddlerCoreStartupFlags flags = FiddlerCoreStartupFlags.DecryptSSL & FiddlerCoreStartupFlags.AllowRemoteClients & FiddlerCoreStartupFlags.CaptureFTP & FiddlerCoreStartupFlags.ChainToUpstreamGateway & FiddlerCoreStartupFlags.MonitorAllConnections & FiddlerCoreStartupFlags.CaptureLocalhostTraffic;

    Installed Certificate successfully but session showing only http traffic unable to capture https and i have tried your coded above same result could you suggest me a solution.

    ReplyDelete
  8. Try FiddlerApplication.Startup(Constants.FiddlerPort, FiddlerCoreStartupFlags.Default);

    ReplyDelete
  9. Hi, thanks for your article. during my test (selenium-c#) I need to change one of the requests, the application loads with a certain JS file which I want to change to a local file. manually it's possible. How can I automate this scenario?
    thanks in advance!

    ReplyDelete
    Replies
    1. I haven't tried it but you can try these options:
      1. Set the IP of the server hosting the JS file to your local IP
      2. Update the response using your local file

      Delete
  10. Hello Anuj,

    I have to capture the data from Inspectors>>Webforms. is that possible using Fiddler Core.
    Thanks in advance for the blog, i get little confidence that i can do something.

    ReplyDelete
  11. New to Fiddler, could you please provide me with step by step c# code to get a list of request and response from Web page and then extract the info out of the response. Thanks in advance , chetan

    ReplyDelete