Selenium is a suite of browser automation tools to help with automating web browsers across a number of platforms. It’s a really handy tool to help with automating web applications for testing purposes.
Most of the well known browsers support it and have taken steps to make Selenium a native component of their browsers. The core Selenium technology is used across other browser automation tools , Api’s and frameworks.
In this guide I’ll provide instructions on how to set up Selenium Web Driver using Microsoft Visual Studio and an example of how to write a quick test.
The setup process is pretty much the same, across most versions of Visual Studio i.e. Visual Studio 2013, Visual Studio 2015 and Visual Studio 2017.
For Visual Studio 2013, I recommend ensuring you have Update 5 isntalled.
I prefer to use nUnit for my Selenium Testing, you may want to install the nUnit VS Templates .
Selenium WebDriver browser automation testing
The WebDriver is a tool for automating the testing of web applications. It assists in the running of web browsers natively as a user would either locally or on a remote machine using the Selenium Server. It offers support for dynamic web pages and supports AJAX.
The Selenium WebDriver uses each browsers native support for automation and is programming language dependent. Microsoft C# bindings are distributed as part of a set of signed dlls and dependency dlls.
The first step would be to start Microsoft Visual Studio and create a new Visual C# Unit Test Project. Once the solution opens right click on the Project file in the Solution Explorer Window and Select Manage Nuget Packages…
Conduct a search using Online and the search term Selenium the options for both the Selenium WebDriver and Selenium WebDriver Support Classes should appear. Ensure you install both these packages to your project.
Both the Internet Explorer and Firefox driver are included in the Selenium, however we will need to download an additional web driver for Chrome . Once the web driver for chrome has been downloaded extract the executable file to a folder in your solution .
I created a folder called dependencies and added the executable there
Ensure the Build Action Content is selected Copy to Output Directory Copy Always has been selected. This will ensure that chromedriver.exe is always in the folder of the running assembly so it can be used.
Once all the set up is complete we can now begin writing a simple test check everything is working as expected. In my example I am going to test that a user is redirected to a login page if they select the Login link on the homepage of the threenine.co.uk.
Example of how to use Selenium for automated browser testing
We’ll create a simple ThreeNineLoginTest.cs unit test class and import the Selenium Namespaces required
We’ll now create a simple unit test
public class ThreeNineLoginTest
public void RedirectToLoginFromHomePage()
using (IWebDriver wdriver = new ChromeDriver(@"dependencies"))
If we run the test, we’ll see the chrome browser launch and the website should be loaded, the new chrome window should be maximized and then the Login link should be click and the user should be redirected to the login page. We’ll carry out a simple assert to determine if the Browser URL is inded the URL of the login page. The test should pass.
When in the Visual Studio IDE, I prefer to run all my tests using Resharper Productivity Tool for Visual Studio, but we can now also equally run these tests using the Selenium Server too.
Creating and Editing Test Cases Using Selenium IDE
You can develop Test Cases using the Selenium IDE, a Firefox plugin with a COntext Menu and Selenium command options that help with learning the Selenium Script Syntax.
Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run in.
Once you’ve installed Selenium IDE in Firefox, it’s available in the Firefox Tools menu. Selecting it will open a new, empty script-editing window as well as a menu for loading or creating test cases.
After installation of the Selenium IDE , it’s available in the Firefox Tools menu.
Selecting it will open a new, empty script-editing window as well as a menu for loading or creating test cases. From there, you can use one of three methods to build test cases:
- Record test cases: Record a test case based on website interaction. During recording, Selenium automatically inserts commands based on your actions, such as clicking a link , entering values , selecting options from a drop-down menu , or clicking check boxes or radio buttons.
- Adding verifications and asserts: check the properties of a web page using the assert and verify.
- Editing: Edit test cases and manually insert or edit commands and comments.
Executing Test Cases in Selenium IDE
- Running a test case: Execute the currently displayed test case.
Running a test suite: Execute all test cases in the test suite currently loaded.
- Stop and start: Use the “Pause” button to stop a test case currently running.
- Stop in the middle: This option enables you to set a specific breakpoint that stops a running test at a particular command. Setting a breakpoint is easy; just select a command, right-click, and then choose “Toggle Breakpoint” from the context menu.
- Start from the middle: Similar to stopping a test at a certain command, you can also set a test to begin running from a specific command. The process for setting this point is the same, but instead of choosing “Toggle Breakpoint,” you’ll select “Set/Clear Start Point” from the context menu.
- Running a single command: Double-click on any command to run it by itself, a useful tool for testing a command while you’re constructing it.
Selenium is a powerful tool to automate your mundane browser testing tasks. Mastering the basics and you’ll seen find more ways to use the tool to improve your development and testing processes.
There are many more advanced features and functionality, but the ease of use and simplicity of the tooling makes it very easy and appealing for new users to to pick up and master very quickly.
A useful resource for learning about the advanced capabilities of Selenium is teh Selenium Wiki on GitHub .
A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Latest posts by Gary Woodfine (see all)
- Happy 4th Blog Birthday – A blogging year in review - Dec 6, 2018
- Getting started with .NET Core and the Serverless Framework - Dec 3, 2018
- How to use the Abstract Factory design pattern in C# - Nov 18, 2018