Selenium WebDriver - Using a Headless Browser in .Net

If you have ever run a large suite of automated tests using Selenium WebDriver, you will know that sometimes it can take ages to complete. This coupled with different versions of browsers can be a bit of a pain. This is where Headless Browsers come in.

Headless Browser

In the simplest of terms, a headless browser is a web browser without a graphical user interface. This means that the browser can access web pages but doesn't ever display them to a user and this makes it super useful because it is lightning fast, lightweight and in-memory. Instead of having to wait for the browser to load a new window and then open and close, you can send it a large chunk of URLs to check and it will be able to handle them fast and efficiently.

This comes in very useful when writing selenium tests that you want to get up and running quickly. At my workplace, we also have our Selenium automated tests incorporated into our CI builds. I spent a while looking for .net solutions out there that are easy to get started with and don't require much change to your existing code. There is a great Java version out there called HtmlUnit, but I couldn't find any decent .net ones....until I came across SimpleBrowser. Fortunately SimpleBrowser works using the IWebDriver interface, which means it quite easy to get started with.

I have blogged quite a bit previously about using Selenium WebDriver, so for more information on the basics, please check out this link. Let's begin by taking a look at a simple example - first, you will need to download the latest version of SimpleBrowser from Github. Next, add a reference to the SimpleBrowser libraries.

As you can see, no GUI was displayed to the user - it all ran in the background and was super fast and efficient. I ran this against a large suite of tests and noticed that it ran extremely quickly! SimpleBrowser supports cookie-based sessions, HTTP/HTTPS, CSS selectors, etc. However, there is also currently no support for JavaScript - this may or may not be seen as a problem depending on how your site is coded (You may have considered progressive enhancement).

I did come across this issue when testing:

Reference Binaries

Some older versions of the Webdriver.dll might not work, but fortunately there is a workaround for this - you can update your app.config to bind to any older versions - this seemed to work for me.

Either way, I think this is a great tool and will start putting it to good use! To download a copy of the project in this article, please click here.