In the WebDriver Series, I share with you tips and tricks how to implement various test automation scenarios. In this article, I am going to explain how to intercept raw HTTP traffic using WebDriver C#. In the Java world, most of the people are using tools such as Browsermob (an open source HTTP lightweight proxy). I have seen projects in C# that use it as well. However, when I tried to integrate into my solutions, there were several problems. Moreover, it was quite slow in C# since you need to start the proxy from the command line. Previously, a popular library was Fiddler, but now it is paid. I wanted to find an open source one. I stopped on Titanium.Web.Proxy.Http. Here, I am going to show you how to integrate it into your WebDriver tests. Through it, you will be able to filter specific requests or redirect them. If you want, you can validate some of the requests' responses.

Why Do You Need to Capture HTTP Traffic?

Using the HTTP proxy, you can intercept an HTTP request originating from the browser before it is sent to the web server and/or capture HTTP responses coming from the web server just before they reach the browser and are rendered by it. This functionality opens up the framework to be used in a broader range of automation scenarios like:



Security Testing: In your event handlers you have access to all the basic HTTP Request/Response properties, and you can examine, inject, alter or do whatever you want with them.



Performance Testing: You can use the HTTP event handlers to look at file sizes you are sending or receiving and even build regression tests that make sure your files don't grow beyond a certain size.



Page Synchronization: You can now sync your test to specific requests and can detect Ajax requests over the wire.

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research while I was working on a similar feature for our solution.

Capture HTTP Traffic in WebDriver

First, you need to install the Titanium.Web.Proxy.Http NuGet package.

How to Configure Titanium?

We initialize the proxy once per test class. You can even do it once per tests' run. Setting the property TurstRootCertificate to true will trust locally the root certificate used the proxy. On the next line, we create an endpoint and telling the proxy to listen on port 18882. You can, of course, change it if needed. Then we set the proxy as the primary system proxy for HTTP and HTTPS traffic. I will explain later about the BeforeRequest and BeforeResponse events. On class cleanup, we stop the proxy server. Before each test, we start the browser and configure it to use the started proxy. We will use the _requestsHistory and _responsesHistory dictionaries to access the made requests and the corresponding responses. They need to be reinitialized for each test so in the TestCleanup method we clear them.

Proxy BeforeRequest Event Handlers

We can register an event handler for the BeforeRequest event, and there do some stuff with the requests' info. Once you have created the event handlers, you need to subscribe them for the BeforeRequest or BeforeResponse events. Without any handlers the proxy is useless!

Capture HTTP Requests Event Handler

Here is how we can capture the HTTP requests so that we can later make assertions against them.

Block Specific Requests Event Handler

We block all requests to analytics related URLs. Of course, you can create your own conditions. Sometimes, for testing certain functionalities on your website blocking some resources may speed up your tests.

Redirect Requests Event Handler

Above, I redirect the logo of Automate the Planet to a different image.

Modify Requests Event Handler

You can use similar code to alter specific requests.

Proxy BeforeResponse Event Handlers

We can register an event handler for the BeforeResponse event, and there do some stuff with the responses info.

Capture HTTP Responses Event Handler

Modify Responses Event Handler

HTTP Proxy in Tests

For the different assertions, we use the two dictionaries that I mentioned where we store all requests and responses. In the assertion methods, we filter these collections and check whether some conditions are true or false. The possibilities are limitless.

Assert That a Request is Made

Assert That no Error Codes Exist