RyanSchlomer.com

Sr QA Consultant

Selenium Automation–Starting a New Project in Visual Studio

Posted by:

|

On:

|

,
Selenium Automation

I’ve been adding some bits and pieces of automation to the first few posts, but now I’m going to take a step back and go through the steps to set up a new project for Selenium Automation. It’s not that hard to get a Visual Studio project up and running to start automating web pages with Selenium.

Creating a Visual Studio Project for Selenium Automation

In Visual Studio, create a new project. You can create a Web Driver Test for Edge (.Net Core) project or an MSTest Test project. Either is fine to get started. Next, you can import the following NuGet packages. Depending on the project you created, some of them might be imported already:

  1. OpenQA.Selenium: This package provides the core Selenium WebDriver classes.
  2. OpenQA.Selenium.Support.UI: This package contains additional utilities like WebDriverWait.
  3. SeleniumExtras.WaitHelpers: This package contains extra waiting conditions.

If you created a Web Driver Test for Edge project, your C# file should look like this:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;

namespace EdgeDriverTest4
{
    [TestClass]
    public class EdgeDriverTest
    {
        // In order to run the below test(s), 
        // please follow the instructions from https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium
        // to install Microsoft Edge WebDriver.

        private EdgeDriver _driver;

        [TestInitialize]
        public void EdgeDriverInitialize()
        {
            // Initialize edge driver 
            var options = new EdgeOptions
            {
                PageLoadStrategy = PageLoadStrategy.Normal
            };
            _driver = new EdgeDriver(options);
        }

        [TestMethod]
        public void VerifyPageTitle()
        {
            // Replace with your own test logic
            _driver.Url = "https://www.bing.com";
            Assert.AreEqual("Bing", _driver.Title);
        }

        [TestCleanup]
        public void EdgeDriverCleanup()
        {
            _driver.Quit();
        }
    }
}

Using Other Browsers

You obviously don’t have to test against Edge. To test against Chrome or Firefox, you just initialize those drivers instead:

            IWebDriver Cdriver = new ChromeDriver();
            IWebDriver Fdriver = new FirefoxDriver();

I’ll get into testing against multiple browsers at the same time later.

Microsoft Edge WebDriver

Before you can run the Selenium test above, you need to follow the instructions on lines 10, 11, and 12:

    // In order to run the below test(s), 
    // please follow the instructions from https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium
    // to install Microsoft Edge WebDriver.

Download the appropriate msedgedriver.exe file from Microsoft. Now, update the code so that the Edge driver knows where to find the file. You can see I added the driverPath, modified the first method, and made a few other changes:

public class EdgeDriverTest
    {
        // In order to run the below test(s), 
        // please follow the instructions from https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium
        // to install Microsoft Edge WebDriver.

        private static EdgeDriver _driver;
        private static string driverPath = @"C:\Test";

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {
            EdgeDriverService service = EdgeDriverService.CreateDefaultService(driverPath);
            var edgeOptions = new EdgeOptions
            {
                PageLoadStrategy = PageLoadStrategy.Normal
            };
            _driver = new EdgeDriver(service, edgeOptions);
        }   

        [TestMethod]
        public void VerifyPageTitle()
        {
            // Replace with your own test logic
            _driver.Url = "https://www.bing.com";
            Assert.AreEqual("Bing", _driver.Title);
        }

        [TestCleanup]
        public void EdgeDriverCleanup()
        {
            _driver.Quit();
        }
    }

Running the test should bring up an Edge browser, navigate to bing.com, and close the browser. It’s probably best to just comment out the _driver.Quit() line so that you can see where the test ends.

Adding in TestManager Class

Let’s add in the TestManager class from a few posts back. Create a file and add the code to the project. Don’t forget that you will need to set up a SQLite database in order to access the database. I’ll add a post on that in the future as well. For now, you can install the NuGet package for SQLite (System.Data.SQLite is what I am using) and/or comment out the SQLite references to get it to compile.

In the code below, I created a TestManager instance and called the TM.AreEqual() method instead of the Assert.AreEqual(). This logs the test result and keeps running if the test fails.

using EdgeDriverTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;

namespace EdgeDriverTest
{
    [TestClass]
    public class EdgeDriverTest
    {
        // In order to run the below test(s), 
        // please follow the instructions from https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium
        // to install Microsoft Edge WebDriver.

        private static EdgeDriver _driver;
        private static string driverPath = @"C:\Test";

        private static TestManager TM;

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {
            EdgeDriverService service = EdgeDriverService.CreateDefaultService(driverPath);
            var edgeOptions = new EdgeOptions
            {
                PageLoadStrategy = PageLoadStrategy.Normal
            };
            _driver = new EdgeDriver(service, edgeOptions);

            TM = TestManager.Instance;

        }
       

        [TestMethod]
        public void VerifyPageTitle()
        {
            // Replace with your own test logic
            _driver.Url = "https://www.bing.com";
            //Assert.AreEqual("Bing", _driver.Title);
            TM.AreEqual("Bing", _driver.Title);
        }

        [TestCleanup]
        public void EdgeDriverCleanup()
        {
            //_driver.Quit();
        }
    }
}

As you can see, starting a new Visual Studio project for Selenium automation is pretty straightforward. Change the URL to the web page you want to test and have fun!