Selenium C# Tutorial: MSTest QuickStart Guide

Updated: Jan 18

In my previous, we talked about how to use NUnit with Selenium WebDriver, but due to your increasing questions and requests related to MSTest, I decided to create this Quick Guide that will provide all the answers to your questions.


What is the MSTest framework?

MSTest is the default test automation framework that comes bundled with Visual Studio. It was started as a primary command-line tool for executing tests and is also known as "Visual Studio Unit Testing Framework" However, the name MSTest is more synonymous with the engineering community.


Like the NUnit test framework, the MSTest framework provides all the necessary tools to create and run tests that will verify and validate your source code. The framework has a variety of attributes and annotations (e.g. [ClassInitialize], [TestMethod], [TestCleanup]) used to recognize tests created in the solution.



Requirements For Running Your First MSTest Script

in this MSTest article and code examples, I'll use the Chrome browser. So, you'll need to download and install the Chrome WebDriver on the computer where you've installed visual studio. The selenium framework will use a chrome driver to interact with the elements on a page for automated web testing with Selenium C#.


Note: You must download the driver to be similar to your chrome version. Otherwise, it will fail to run when initializing the driver in your code.


To download the Selenium WebDriver for chrome, please use the following link:

Now, open the ZIP and copy the "hromedriver.exe" into the folder of the chrome browser (you will be able to invoke the WebDriver without the need to specify its path).

And if you choose not to follow this practice, please add the setProperty() method from the system class. Using it will allow us to set the different properties for the desired browser to be bused in your test project. The setProperty method has two attributes: "propertyName" (name of the browser-specific driver) and "value" (path of that browser driver).

System.setProperty("webdriver.chrome.driver","C:\\ChromeDriver\\chromedriver.exe");


Creating the project and Deploying Selenium WebDriver

To download selenium Webdriver in your project, please follow the following steps:


Step 1: Open Visual Studio and create a new "MSTest Test Project"

Step 2: Verify that all Nuggets are installed

Step 3: Add Selenium WebDriver Nuggets:

Now, you have created all the infrastructure for using Selenium Web Driver with MSTests. you are all set to develop and execute your first selenium test:

//MSTest framework (Microsoft.VisualStudio.TestTools.UnitTesting)
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Selenium (OpenQA.Selenium)
using OpenQA.Selenium;
//Chrome WebDriver (OpenQA.Selenium.Chrome)
using OpenQA.Selenium.Chrome;
using System;

namespace TestProject1._0
{
    [TestClass]// class containing the unit tests. 
    public class UnitTest1
    {
        IWebDriver driver = new ChromeDriver();


        [TestMethod]//method that contains the test method
        public void TestMethod1()
        {
            driver.Url = "https://www.agilequalitymadeeasy.com/blog";
            Assert.AreEqual(driver.Url, "https://www.agilequalitymadeeasy.com/blog");
        }
         

        [TestMethod]//method that contains the test method
        public void TestMethod2()
        {
            driver.Url = "https://www.agilequalitymadeeasy.com/blog";
            Assert.AreEqual(driver.Title, "Blog | AgileQualityMadeEasy");
        }
    }
}

Now, to run your tests, you'll need to start a new build by pressing "Ctrl+Shift+B" and opening the Test Explorer (Tools -> Test Explorer)



Annotations in MSTest

Similar to what we have in NUnit, the primary role of annotations in MSTest is to inform the underlying framework on how the source code should be interpreted. This starts when the build is compiled, and a DLL is generated. In this article, we will cover essential used MStest framework annotations:


[ClassInitialize] and [ClassCleanup]

ClassInitialize Method will be called only once before executing any of the test methods present in that class, and ClassCleanup will be called only once after executing the test methods currently in that class.

 [ClassInitialize]publicstaticvoidTestFixtureSetup(TestContextcontext) {// Executes once for the test class}

 [ClassCleanup]publicstaticvoidTestFixtureTearDown() {// Runs once after all tests in this class are executed.

Example:

[TestClass]
    public class UnitTest1
    {
        static IWebDriver driver; 
        static int Testcounter = 1;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            // initialize the web driver
            driver = new ChromeDriver();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            // close the web driver
            driver.Quit();
        }

[TestInitialize] and [TestCleanup]

The TestInitialize attribute is needed when we want to run a function before executing a test. For example, we want to run the same test ten times and set a code that will be used before running each time. Similar to the TestInitialize attribute, the TestCleanup will run after every test execution. If we need to perform some cleanup operation, we can write code within this function.


Let's see an example of how it works; in the following example, I will increase the counter per test so that you can see that it's run twice per test method.

[TestClass]
    public class UnitTest1
    {
        IWebDriver driver = new ChromeDriver();
        static int Testcounter = 1;

        [TestInitialize()]
        public void Initialize()
        {
            Console.WriteLine("Test started number {0}", Testcounter);
            Testcounter++;
        }

        [TestCleanup()]
        public void Cleanup()
        {
            Console.WriteLine("Test number {0} is compleated", Testcounter);
        }

        [TestMethod]        
        public void TestMethod1()
        {
            
        }

        [TestMethod]
        public void TestMethod2()
        {
            
        }
    }

Result:

[TestClass]

This indicates that the class has test methods.

[TestClass]// class containing the unit tests. 
    public class UnitTest1
    {
        IWebDriver driver = new ChromeDriver();

        [TestMethod]        
        public void TestMethod1()
        {
           
        }

        [TestMethod]
        public void TestMethod2()
        {
    
        }

        [TestMethod]
        public void TestMethod3()
        {
          
        }
    }

[TestMethod]

A Test case in the test class.

public class UnitTest1
    {
        IWebDriver driver = new ChromeDriver();

        [TestMethod]        
        public void TestMethod1()
        {
           
        }

        [TestMethod]
        public void TestMethod2()
        {
    
        }

        [TestMethod]
        public void TestMethod3()
        {
          
        }
    }

[Ignore]

Ignores a specific test method.

[TestMethod]
[Ignore]
public void TestMethod1()
{
driver.Url = "https://www.agilequalitymadeeasy.com/blog";
Assert.AreEqual(driver.Url, "https://www.agilequalitymadeeasy.com/blog");}

[TestCategory]

The TestCategory attribute is handy when the number of your tests starts to get overwhelming. It lets you create a group of tests using any arbitrary system you want to run a specific group of tests in class, a project, or a solution.


in the example below, you can see that we have two ways to add this attribute:

[TestMethod, TestCategory("Category 1")]
public void Method1()

[TestMethod]
[TestCategory("Category 1")]
public void Method2()




363 views0 comments