Selenium C# Tutorial: Cross Browser Testing

Updated: Jan 18

Cross Browser Testing is testing a web application in multiple browsers (e.g., Chrome, Firefox, etc.) to ensure that the functional requirements work as expected. The option to perform these tests manually is out of the question as it is exceptionally tedious. And time-consuming. Using Selenium WebDriver, we can quickly write a single test and run it on different browsers simultaneously and in a reasonable time frame.


We know Selenium WebDriver supports different browsers like:

  • Chrome

  • Firefox

  • IE

  • Safari

So, it is essential to design, configure and implement our code to use those browsers in our framework. One way is to use constant strings for each type of browser and call them, but as you may guess, it will be tough to maintain and debug after the initial implementation.



So what is the right way to achieve this?

Using C#, we can write a strongly-typed code, which we can do using Enum. Create browser types with Enum and call them with their own class. let's see it in action:


Prerequisites:

  1. Add all nuggets of selenium.

  2. Add all nuggets for browser under tests (Chrome, IE, and Firefox).

  3. Create a new MS Test project.

Now, create two new classes:


Class 1: Browser

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;

namespace MultipleBrowsers
{
    public enum BrowserType
    {
        InternetExplorer,
        FireFox,
        Chrome
    }

    public class Browser
    {
        public BrowserType Type { get; set; }

        private readonly IWebDriver _driver;

        public Browser(IWebDriver driver)
        {
            _driver = driver;
        }

        public void GoToUrl(string url)
        {
            DriverContext.Driver.Url = url;        
        }        
    }
}


Class 2: DriverContext

using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Text;

namespace MultipleBrowsers
{
    public static class DriverContext
    {

        private static IWebDriver _driver;

        public static IWebDriver Driver
        {
            get { return _driver; }
            set { _driver = value; }
        }

        public static Browser Browser { get; set; }

        enum BrowserType
        {
            InternetExplorer,
            FireFox,
            Chrome
        }
    }
}


Unit Tests (Main Class):

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

namespace MultipleBrowsers
{
[TestClass]
public class UnitTest1
{
string url = "https://www.agilequalitymadeeasy.com/";

public void OpenBrowser(BrowserType browserType)
{
switch (browserType)
{
case BrowserType.InternetExplorer:
DriverContext.Driver = new SafariDriver();
DriverContext.Browser = new Browser(DriverContext.Driver);
break;
case BrowserType.FireFox:
DriverContext.Driver = new FirefoxDriver();
DriverContext.Browser = new Browser(DriverContext.Driver);
break;
case BrowserType.Chrome:
DriverContext.Driver = new ChromeDriver();
DriverContext.Browser = new Browser(DriverContext.Driver);
break;
}
}
   
[TestMethod]
public void FireFoxTest()
{
OpenBrowser(BrowserType.FireFox);
DriverContext.Browser.GoToUrl(url);
}

[TestMethod]
public void ChromeTest()
{
OpenBrowser(BrowserType.Chrome);
DriverContext.Browser.GoToUrl(url);
}
}
}

Output:




475 views0 comments