Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test multiple browsers with selenium and a single NUnit suite and keep it DRY?

I'm looking for a way to reuse one NUnit test suite without duplicating the entire suite for each browser. It seems like I would need a new fixture for each browser. Can I send some sort of environment variable or configuration setting from the NUnit gui to switch the browser? see below:

[TestFixture]
public class User
{
    private ISelenium selenium;
    private StringBuilder verificationErrors;

    [SetUp]
    public void SetupTest()
    {
        // TheBrowser = How do I populate this variable from the NUnit gui? 
        selenium = new DefaultSelenium("localhost", 4444, **TheBrowser**, "http://localhost:52251/");
        selenium.Start();
        verificationErrors = new StringBuilder();
    }

    [TearDown]
    public void TeardownTest()
    {
      ...
    }

    [Test]
    public void SearchUser()
    {
       ... 
    }

}
like image 763
Tim Santeford Avatar asked Jan 27 '11 22:01

Tim Santeford


People also ask

How can we run same test cases on multiple browsers at once?

Step1: If we are using Selenium WebDriver, we can automate test cases using Internet Explorer, FireFox, Chrome, Safari browsers. Step 2: To execute test cases with different browsers in the same machine at the same time we can integrate TestNG framework with Selenium WebDriver.

Which component from selenium suite is used to run multiple tests simultaneously in different browsers and OS?

Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in parallel.

How do I run Selenium on multiple browsers?

Create an XML which will help us in parameterizing the browser name and don't forget to mention parallel="tests" in order to execute in all the browsers simultaneously. Execute the script by performing right-click on the XML file and select 'Run As' >> 'TestNG' Suite as shown below.


1 Answers

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

Building the following will create two "GoogleTest" NUnit tests, one for Firefox and one for 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();
        }
    }
}
like image 122
alanning Avatar answered Dec 06 '22 10:12

alanning