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
Will write soon how to deal with it! :)
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
- First thing that you should do is to download FiddlerCore and add library to your project
- Then you need to start Fiddler proxy
- Now, before creating driver just run:
var proxyPort = StartFiddlerProxy(0);
profile.SetProxyPreferences(DriverProxy(proxyPort));
driver = new FirefoxDriver(profile); - 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);
Simple, isn't it? :)
{
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);
}
}
- What if you need to check what requests app send?
Then you need
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest
- 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 - 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
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;
}
FiddlerApplication.BeforeResponse
Will write soon how to deal with it! :)