{{announcement.body}}
{{announcement.title}}

MSTest Tutorial: Running First Selenium Automation Script

DZone 's Guide to

MSTest Tutorial: Running First Selenium Automation Script

Parameterized tests allows tester to run the same test a few times using different values. Find out how you can do this in MSTest/

· Performance Zone ·
Free Resource

MSTest is the default test automation framework which comes bundled with Visual Studio. It started as a command-line tool for executing tests and was referred to as the Visual Studio Unit Testing Framework; however, the name MSTest is more synonymous with the developers.

In the previous MSTest tutorial, I had a look at setting up the MSTest framework in Visual Studio. In this MSTest tutorial for Selenium automation, I’ll help you run your first MSTest script for automated web testing with Selenium C#.

Requirements For Running Your First MSTest Script

In this MSTest tutorial, I'll use the Chrome browser to verify the functionalities in the code. So, you’ll need to install the Chrome WebDriver on the machine where you’ve installed the MSTest framework and Selenium WebDriver.

Different web browsers have their corresponding Selenium WebDrivers. These WebDrivers are crucial to interact with the web elements on a page for automated web testing with Selenium C#. You can download the Selenium WebDriver for browsers such as Opera, Firefox, Chrome, Internet Explorer, Microsoft Edge, etc. using the following links.

Since I’ll be using Chrome, I’ll download the Selenium WebDriver for Chrome i.e. ChromeDriver. Though the downloaded ChromeDriver can be placed in any directory, it is a good practice to install the ChromeDriver in the location where the Chrome browser (i.e. Chrome.exe) is present. Learn more about running an automated test with Chromedriver.

chrome.exe

The advantage of following this practice is that you do not need to specify the path where the ChromeDriver is present while invoking the WebDriver.

How To Run MSTest Scripts With Selenium?

For demonstrating the usage of Selenium WebDriver with MSTest, I’ll use the following Selenium test automation scenario for this MSTest tutorial:

1. Navigate to the URL https://lambdatest.github.io/sample-todo-app/

2. Select the first two checkboxes

3. Send ‘Adding item to the list’ to the textbox with id = sampletodotext

4. Click the Add Button and verify whether the text has been added or not

Implementation Of The MSTest Script

C#
 




x
58


 
1
#Run first MStest script for Selenium test automation 
2
using Microsoft.VisualStudio.TestTools.UnitTesting;
3
using OpenQA.Selenium;
4
using OpenQA.Selenium.Chrome;
5
using System;
6
 
7
namespace MS_Test_Cross_Browser
8
{
9
    [TestClass]
10
    public class MS_Test_Cross_Browser_Test
11
    {
12
        String test_url = "https://lambdatest.github.io/sample-todo-app/";
13
        String itemName = "Yey, Let's add it to list";
14
 
15
        [TestMethod]
16
        public void NavigateToDoApp()
17
        {
18
            IWebDriver driver;
19
 
20
            // Local Selenium WebDriver
21
            driver = new ChromeDriver();
22
            driver.Manage().Window.Maximize();
23
 
24
            driver.Navigate().GoToUrl(test_url);
25
 
26
            driver.Manage().Window.Maximize();
27
 
28
            // Click on First Check box
29
            IWebElement firstCheckBox = driver.FindElement(By.Name("li1"));
30
            firstCheckBox.Click();
31
 
32
            // Click on Second Check box
33
            IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
34
            secondCheckBox.Click();
35
 
36
            // Enter Item name
37
            IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
38
            textfield.SendKeys(itemName);
39
 
40
            // Click on Add button
41
            IWebElement addButton = driver.FindElement(By.Id("addbutton"));
42
            addButton.Click();
43
 
44
            // Verified Added Item name
45
            IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
46
            String getText = itemtext.Text;
47
            Assert.IsTrue(itemName.Contains(getText));
48
 
49
            /* Perform wait to check the output in this MSTest tutorial for Selenium */
50
            //System.Threading.Thread.Sleep(4000);
51
 
52
            Console.WriteLine("LT_ToDo_Test Passed");
53
 
54
            driver.Quit();
55
        }
56
    }
57
}
58
 
          



Code WalkThrough

Step 1 — All the necessary packages are included at the beginning of the code. In this MSTest tutorial for Selenium; the following packages are included for automated web testing with Selenium C#:

  • MSTest framework (Microsoft.VisualStudio.TestTools.UnitTesting)
  • Selenium (OpenQA.Selenium)
  • Chrome WebDriver (OpenQA.Selenium.Chrome)
C#
 




xxxxxxxxxx
1


 
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2
using OpenQA.Selenium;
3
using OpenQA.Selenium.Chrome;
4
using System;
5
……………………………………………………………………………..
6
……………………………………………………………………………..
7
 
          



Step 2 — The [TestClass] attribute in MSTest represents the class containing the unit tests. In this example, MS_Test_Cross_Browser_Test() is the class containing all the tests.

C#
 




xxxxxxxxxx
1


 
1
namespace MS_Test_Cross_Browser
2
{
3
    [TestClass]
4
    public class MS_Test_Cross_Browser_Test
5
    {
6
 
          



Step 3 — The [TestMethod] attribute in MSTest indicates a method that contains the test method. In this example, the test method is NavigateToDoApp() and the same is under the [TestMethod] attribute.

C#
 




xxxxxxxxxx
1


 
1
[TestMethod]
2
public void NavigateToDoApp()
3
{
4
 
          



Step 4 — The IWebDriver interface is used for enabling browser interactions. Hence, we create an instance of the IWebDriver interface in the code. A new instance of Chrome browser is initiated using the command driver = new ChromeDriver().

The URL is set to https://lambdatest.github.io/sample-todo-app/ and the browser window is maximized using the command driver.Manage().Window.Maximize() for this MSTest tutorial for automated web testing with Selenium C#.

C#
 




xxxxxxxxxx
1
12


1
public void NavigateToDoApp()
2
{
3
    IWebDriver driver;
4
 
5
    // Local Selenium WebDriver
6
    driver = new ChromeDriver();
7
    driver.Manage().Window.Maximize();
8
 
9
    driver.Navigate().GoToUrl(test_url);
10
 
11
    driver.Manage().Window.Maximize();
12
 
          



Step 4 — Once the Chrome browser is initiated and the target URL is loaded, we locate the required elements on the web page for automated web testing with Selenium C#. The details of the required Web Elements are obtained using the Inspect Tool in the Chrome Browser. As seen in the screenshot below for cross-browser testing, the element with Name “li1” is located using the tool. 

LambdaTest sample app

C#
 




xxxxxxxxxx
1
25


 
1
[TestMethod]
2
public void NavigateToDoApp()
3
{
4
    IWebDriver driver;
5
 
6
    ...............................................................
7
    ...............................................................
8
    ...............................................................
9
 
10
    // Click on First Check box
11
    IWebElement firstCheckBox = driver.FindElement(By.Name("li1"));
12
    firstCheckBox.Click();
13
 
14
    // Click on Second Check box
15
    IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
16
    secondCheckBox.Click();
17
 
18
    // Enter Item name
19
    IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
20
    textfield.SendKeys(itemName);
21
    ...............................................................
22
    ...............................................................
23
    ...............................................................
24
}
25
 
          



The same approach is followed for locating the other web elements having Name - li2, li3. The other elements are located using the Id of those elements.

The FindElement command in Selenium is used along with the appropriate web locators to identify the web element. Once the web element is identified (or located), appropriate action i.e. Click, SendKeys, etc. is performed on the web element.

C#
 




xxxxxxxxxx
1


1
// Click on Second Check box
2
IWebElement secondCheckBox = driver.FindElement(By.Name("li2"));
3
secondCheckBox.Click();
4
 
5
// Enter Item name
6
IWebElement textfield = driver.FindElement(By.Id("sampletodotext"));
7
textfield.SendKeys(itemName);
8
 
          



Step 5 — A new item (Yey, Let's add it to list) is added to the list. An assert is raised if the text in the newly added element does not match with the expected text.

The Quit WebDriver command (driver.Quit) is called once the test is complete so that the WebDriver session is closed properly and resources used by the WebDriver are freed in this MSTest tutorial for Selenium automation.

C#
 




xxxxxxxxxx
1
16


1
[TestMethod]
2
public void NavigateToDoApp()
3
{
4
    ..........................................................................  ..........................................................................  ..........................................................................  // Verified Added Item name
5
    IWebElement itemtext = driver.FindElement(By.XPath("/html/body/div/div/div/ul/li[6]/span"));
6
    String getText = itemtext.Text;
7
    Assert.IsTrue(itemName.Contains(getText));
8
 
9
    /* Perform wait to check the output */
10
    //System.Threading.Thread.Sleep(4000);
11
 
12
    Console.WriteLine("LT_ToDo_Test Passed");
13
 
14
    driver.Quit();
15
}
16
 
          



Executing The MSTest Script

To execute the newly created test (NavigateToDoApp), go to Test and press Run All Tests.

run all tests

The execution progress can be seen in the Test Explorer window which can be seen by going to menu-item View -> Test Explorer.

test explorer

Shown below is an execution snapshot from Visual Studio. The Green tick mark indicates that the test execution completed successfully.

test explorer

Shown below is the automated browser testing execution in action in the Chrome web browser:

chrome web browser

All In All

In this MSTest tutorial for Selenium, I took a deep-dive into automated web testing with Selenium C# using the MSTest framework to run your first MSTest Script for Selenium test automation. Interactions with the elements on the web-page were performed by using the relevant Selenium APIs. The code implementation used in this article can be scaled to execute complex Selenium test automation scenarios using Selenium C# with the MSTest framework.

This concludes this MSTest tutorial for Selenium to run your first MSTest script for Selenium test automation. I hope you found this article informative! Do share this article with your peers and colleagues looking to learn about Selenium test automation using MSTests. A retweet and share on LinkedIn are always welcome with open arms. That’s all for now. Happy Testing!

Topics:
automation testing, c sharp, c#, mstest, performance, testing tutorial, tutorial

Published at DZone with permission of Himanshu Sheth . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}