вторник, 12 ноября 2013 г.

Get all requst and response during Selenium tests

I want to share my experience how to get log all requests that make tested application.
Why I needed it: truly say, We have not so stable QA server... sometimes, deployment team forgot to start all builds, and some service is missing; sometimes response from service returns very slow, so test failes.
All this cases could not be checked using UI. We will have simple popup, or even nothing at all. And then testers should spend their expensive time on check what could goes wrong
That's why I started to search solution of how we can get status of responses.
But also I have few specific cases when I need to change some parameters in request or response, to make my system run with mock services or fake data. Manual testers usually use Fiddler in this cases. So I searched how to use Fiddler for automation
Fiddler has own FiddlerCore library which allows to view and modify HTTP/HTTPS traffic

  1. First thing that you should do is to download FiddlerCore and add library to your project
  2. Then  you need to start Fiddler proxy
  3. private int StartFiddlerProxy(int desiredPort)
    {
    // We explicitly do *NOT* want to register this running Fiddler
    // instance as the system proxy. This lets us keep isolation.

    Console.WriteLine("[Fiddler Logs]: Starting Fiddler proxy");

    FiddlerApplication.Startup(desiredPort, true, true);
    Console.WriteLine("[Fiddler Logs]: Fiddler proxy listening on port {0}", FiddlerApplication.oProxy.ListenPort);
    return FiddlerApplication.oProxy.ListenPort;
    }
  4. Now, before creating driver just run: var proxyPort = StartFiddlerProxy(0);
    profile.SetProxyPreferences(DriverProxy(proxyPort));
    driver = new FirefoxDriver(profile);
  5. Ok, we have started Fiddler Proxy. Then we need to subscribe to the events.;
    If you need simple check that your application get all resources and responses, you need subscribe to the FiddlerApplication.AfterSessionComplete event: ;
    FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete; ;
    What we have inside of FiddlerApplication_AfterSessionComplete? Let's take a look:;
    private void FiddlerApplication_AfterSessionComplete(Session oSession);
    {
    if (CheckUrl(oSession.fullUrl));
    {
    var response = oSession.responseCode;;
    //if it was error return ;
    if (response >= 400);
    {
    //We need to decode response;
    oSession.utilDecodeResponse();
    Console.WriteLine("[Fiddler Logs]: Responce return {0} with headers {1} from {2}", response,
    oSession.oResponse.headers, oSession.PathAndQuery);
    }
    //else return status of response from fullUrl
    else
    Console.WriteLine("[Fiddler Logs]: Responce return {0} during navigation to {1}", response, oSession.PathAndQuery);
    }
    }

    Simple, isn't it? :)
  6. What if you need to check what requests app send? Then you need FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest
  7. Also, I want to note that HTTPS is realy tricky :) And I had a big problem with checking what is the traffic of all the :443 port urls
    After short googling I found that I need to subscribe to the FiddlerApplication.OnValidateServerCertificate event: FiddlerApplication.OnValidateServerCertificate += Application_CheckCertificates;
    private void Application_CheckCertificates(object sender, ValidateServerCertificateEventArgs e)
    {
    if (SslPolicyErrors.None == e.CertificatePolicyErrors);
    {
    return;
    }
    e.ValidityState = CertificateValidity.ForceValid;
    }

    As you can see I push validation of all request. If you need, you can create some rules that will validate only needed
  8. And of course, do not forget to finish Fiddler Proxy session:
    private static void StopFiddlerProxy()
    {
    Console.WriteLine("[Fiddler Logs]: Shutting down Fiddler proxy");
    FiddlerApplication.Shutdown();
    }


    Add call of StopFiddlerProxy in your [AfterScenario] or [AfterTest] methods
Now, I'm trying to replace response from the server using FiddlerApplication.BeforeResponse
Will write soon how to deal with it! :)

вторник, 5 ноября 2013 г.

пятница, 18 января 2013 г.

WebDriver IE - Document Mode

It's not a big surprise that IE is a big problem in automation.
Our project support only IE8 and higher. And all was good. But suddenly! All page layout in my test became ugly and incorrect... My test Browser was  IE9. Manual verification didn't reproduce that behavior.
I open "Developer tools" during test run and Document Mode was set to "Internet Explorer 7" ! But how it could be?
I have search but answer why page in IE9 open in "IE7" mode was not found....

Ok. I will not tell how much different settings combination I have tried.

If you want to open you page in native page document mode you should:

just remove site from the list of "Websites to be displayed in Compatibility Mode"

That's all :)