ASP.NET UI Testing With Selenium in 2017: 1. Introduction to WebDriver

In this blog series I am going to work through creating automated UI tests for an ASP.NET app using C# and Selenium. I was motivated to write this because I struggled to find relevant and accurate information online, which is I think is just a result of how long Selenium has been around and improved over time. My goal is to highlight and explain the key pieces required starting with Selenium WebDriver in 2017.

What is WebDriver? (WebDriver vs. RemoteControl)

WebDriver is the tool we are going to use to automate interacting with the UI. It is pretty powerful and can be used for any browser automation task, not just UI testing. It relies on each browser’s native support for automation and requires a driver for each browser. The official WebDriver documentation is actually pretty good, even if it seems like it might be out of date (ignore the disclaimer at the top).

RemoteControl (RC) was the original version of Selenium, but was replaced by WebDriver (the wikipedia article provides some history). RC injected custom JavaScript to perform the automation, required running Selenium Server and did not support dynamic pages as well as WebDriver does. That should be the extent of what you need to know about RC and you are safe to ignore any info or guides that refer to it.

Which nugets to use?

This was a point of confusion for me initially that nowhere explained. There are multiple official nuget packages, what do they all do and which ones do I need?

The ones you will require are Selenium.WebDriver and Selenium.Support. WebDriver is the WebDriver API (pretty straightforward) and Support provides better ways of interacting with the API for C#.

You can ignore the Selenium.RC nuget and also Selenium.WebDriverBackedSelenium as it provides WebDriver functionality but with the RC API. It exists to transition code using RC to WebDriver, without it to be rewritten to work with the WebDriver API.

Using WebDriver

To go along with this blog series I have created a SeleniumExamples repo on GitHub which will have code that I refer to. At the time of this post there is a very basic example of using WebDriver with the driver for Chrome to go to the Google homepage in the Open_Google_Chrome method of the _1_Bare_Minimum_Web_Driver_Example class.

To setup the project I added xUnit and Selenium.WebDriver through nuget.

xUnit is my personal preference to run tests and any other test framework, e.g. NUnit, would also work. If you are using xunit, don’t forget the Visual Studio runner.

Selenium.WebDriver gives us access to the WebDriver API. The project is not doing anything advanced at this stage, so I didn’t need Selenium.Support.

Since I decided to use Chrome, I needed to add the Chrome driver. There is a 3rd party nuget package that adds it, but I decided to download it and add it into a Dependencies directory in the project. I wanted to do this a bit out of habit (at One Model we follow this pattern for other required executables) and to make sure I could control where the driver gets added so that I can make them all live in the same folder (when I add others).

I also decided to set the project to copy the driver into the output (bin) directory on compile. I did this in case I want to deploy the test project elsewhere in the future to run tests, and this way all that is required is copying the entire bin directory. This might end up being unnecessary, but did not hurt to do now.

To create an instance of ChromeDriver requires that chromedriver.exe is somewhere in the computer’s PATH, or that you pass the full directory of where it is located as an argument. I prefer that the repo can be cloned and run without any system configuration, so I took the approach of providing the directory as an argument.

From there the code to open the browser and navigate to Chrome is pretty straight forward.


Wrap Up

That’s it for this post. In the next post I add an example web application for testing on and have it start automatically before the UI tests run.

ASP.NET UI Testing With Selenium in 2017: 1. Introduction to WebDriver