How to run Selenium tests in multiple browsers one after another from C# NUnit?


We can run Selenium tests in multiple browsers one after another from C# NUnit. This is done with the help of the Test Fixture concept. This is an attribute that identifies a class, step up and tear down methods.

There are some rules to be followed for a class to have a fixture −

  • It should not be of type abstract.

  • There should be a default constructor for a non−parameterized fixture.

  • The parameterized fixture should have a constructor.

  • It can be publicly exported.

Example

using NUnit.Framework;
using OpenQA.Selenium
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
namespace NUnitTestProject1{
   //Test Fixture declaration
   [TestFixture(typeof(FirefoxDriver))]
   [TestFixture(typeof(ChromeDriver))]
   public class MultipleBrowser<TWebDriver> where
   TWebDriver : IWebDriver, new(){
      private IWebDriver driver;
      [SetUp]
      public void CreateDriver(){
         this.driver = new TWebDriver();
      }
      [Test]
      public void Test1(){
         //launching URL.
         driver.Navigate()
         .GoToUrl("https://www.tutorialspoint.com/index.htm");
      }
      [TearDown]
      public void close_Browser(){
         driver.Quit();
      }
   }
}

Updated on: 30-Jan-2021

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements