C# Selenium MSTest QuickStart Guide
In this article we’ll be using MSTest and Selenium to write tests for web applications. This will be a starter project we can build on for various projects and in future articles.
Requirements
Here are the requirements before we get started:
- Visual Studio (A Microsoft Integrated Development Environment or IDE)
- Nuget (A package manager for Visual Studio)
- Selenium C# language bindings (But we’ll use NuGet to grab these)
- Any browsers installed that you want to test Supported Platforms
Selenium Prep
If you haven’t read through my quick Overview of Selenium you should do that now. Selenium will need a few things configured before it’ll do its magic!
Create Project
To kick things off we’ll need a new project.
File > New > Project
Select the Unit Test Project template (Templates > Visual C# > Test), give it a name and configure some options. Press Ok to create the project. Visual Studio will create the project and open up your first UnitTest class.
Import/Install Selenium
Now we’ll need to grab the Selenium DLLs and give our project access to them.
Tools > NuGet Package Manager > Manage NuGet Packages for Solution
Select Browse, search for ‘Selenium’ and install both Selenium.WebDriver and Selenium.Support for your new project.
Write Test
Now the fun begins; we can write the first Selenium test! We’ll write our test against The Internet1.
Here’s some code to put inside the TestMethod1()
//var driver = new OpenQA.Selenium.Firefox.FirefoxDriver
//var driver = new OpenQA.Selenium.Edge.EdgeDriver
//var driver = new OpenQA.Selenium.IE.InternetExplorerDriver
var driver = new OpenQA.Selenium.Chrome.ChromeDriver
{
Url = "http://the-internet.herokuapp.com/"
};
Assert.IsTrue(driver.Title == "The Internet");
driver.Dispose();
- Get a web driver
- Set the URL property (tells the driver to go to that URL)
- Assert on the title of the driver
- Dispose of the driver
Run Test
Now that we have a functional test we can run it. First, if the Test Explorer isn’t displayed we need to add it.
Test > Windows > Test Explorer
Our test isn’t showing up yet. We need to build the solution for it to recognize that we’ve written a test it can run. Right click on the solution in the Solution Explorer and Build or Rebuild the solution. If the build is successful we should see our test show up in the Test Explorer. Now we can right click on our test and tell it to run. If all went according to plan we should see a Chrome window pop up, navigate to Google’s home page and then close.
This is a basic, and brittle, example of how Selenium works. If our assertion returns false the test will report a failure but the browser window will still be alive. This test is brittle in that it can’t run any code after the Assert if the Assert returns false. We’ll cover a much better testing approach in a later post to avoid such things! This is not an example of best practices by any means. This is to get you a working example of Selenium. Stay tuned for more posts on how to use Selenium and best practices for automating tests!
Thanks for reading! Please share using any of the buttons below and stay tuned for more. Don’t hesitate to reach out in the comments below or on any of the links in the author profile.
Footnotes
-
Credit to Dave Haeffner. ↩