Multi browser testing using Visual Studio

Multi browser testing using Visual Studio can be achieved by using Selenium and NUnit plugins. Below are step by step details of how it can be achieved.

Step 1

In order to achieve multi browser testing using Visual Studio, we need to install few extensions and plugins. First is NUnit adapter ( used in test execution and NUnit Test framework) and Selenium Web Drivers.

Install Nunit adapter for Visual Studio from Tools-> Extensions and updates..

Step1 extensions and updatesStep1 extensions and updates 2

Similarly install NUnit framework extension.

Step 2

Create an empty Test project in Visual Studio as shown below.

Step2 create project

Select the newly created project and click on “NuGet package manager” and search for Selenium. It will list selenium WebDriver for different browsers. Select them and install. In my example I have selected for browser Edge, Chrome, and Firefox.

Step2 select package managerStep2 select selenium webdrivers 2Step2_installedAfter Installation of Web drivers, they will be automatically added to project references.

Alternative way to add references is by manually downloading the respective Web Drivers and adding references in the project.

Step 3

Now we will start on writing code for launching the browsers. Add a new item of type class file and add following code.

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;

namespace Sample1
{
    class Sample1_Edge
    {
        private IWebDriver driver;
        [SetUp]
        public void Setup()
        {
            driver = new EdgeDriver();
        }

        [Test]
        public void SampeBrowserLaunch1()
        {
            driver.Navigate().GoToUrl("http://digitalab.org");
        }
        [TearDown]
        public void TearDown()
        {
            driver.Quit();
            driver.Dispose();
        }
    }
}

Note the 3 important sections. Setup, Test and TearDown. Test will repeat for multiple tests.

For other browsers, you need to change the following code.

driver = new EdgeDriver();

to

driver = new FirefoxDriver();
driver = new ChromeDriver();

Step4

Build the code and run the test using Test Explorer.

Step4 Test execution

 

In the next post we will structure this and use xml file to control multiple browser settings.

In this example of Multi browser testing using Visual Studio, we have used VS 2015. But this will also work with version 2013. It should also work with VS 2012 but I have not tested it.

Leave a Reply