Run Selenium tests in multiple browsers one after another from C# NUnit Run Selenium tests in multiple browsers one after another from C# NUnit selenium selenium

Run Selenium tests in multiple browsers one after another from C# NUnit


NUnit 2.5+ now supports Generic Test Fixtures which make testing in multiple browsers very straightforward.http://www.nunit.org/index.php?p=testFixture&r=2.5

Running the following example will execute the GoogleTest twice, once in Firefox and once in IE.

using NUnit.Framework;using OpenQA.Selenium;using OpenQA.Selenium.Firefox;using OpenQA.Selenium.IE;using System.Threading;namespace SeleniumTests {    [TestFixture(typeof(FirefoxDriver))]    [TestFixture(typeof(InternetExplorerDriver))]    public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()    {        private IWebDriver driver;        [SetUp]        public void CreateDriver () {            this.driver = new TWebDriver();        }        [Test]        public void GoogleTest() {            driver.Navigate().GoToUrl("http://www.google.com/");            IWebElement query = driver.FindElement(By.Name("q"));            query.SendKeys("Bread" + Keys.Enter);            Thread.Sleep(2000);            Assert.AreEqual("bread - Google Search", driver.Title);            driver.Quit();        }    }}


This is a recurring question and is solved a couple ways:

  1. Factory method produces your ISelenium object - You have a helper class with a static getSelenium method. That method reads in some external config, which has a property that defines the browser you want as a string. In your getSelenium you then configure the browser accordingly. here's a handy post on using config files with NUnit http://blog.coryfoy.com/2005/08/nunit-app-config-files-its-all-about-the-nunit-file/

  2. Others have success with injecting the browser via an IoC container. I really like this because TestNG works really well with Guice in Java land, but I'm not sure how easy it is to mix NUnit and Ninject, MEF, etc...


This is basically just an expansion of alanning's answer (Oct 21 '11 at 20:20). My case was similar, just that I did not want to run with the parameterless constructor (and thus use the default path to the driver executables). I had a separate folder containing the drivers I wanted to test against, and this seems to work out nicely:

[TestFixture(typeof(ChromeDriver))][TestFixture(typeof(InternetExplorerDriver))]public class BrowserTests<TWebDriver> where TWebDriver : IWebDriver, new(){    private IWebDriver _webDriver;    [SetUp]    public void SetUp()    {        string driversPath = Environment.CurrentDirectory + @"\..\..\..\WebDrivers\";        _webDriver = Activator.CreateInstance(typeof (TWebDriver), new object[] { driversPath }) as IWebDriver;    }    [TearDown]    public void TearDown()    {        _webDriver.Dispose(); // Actively dispose it, doesn't seem to do so itself    }    [Test]    public void Tests()    {        //TestCode    }}

}