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

前端 未结 5 2019
逝去的感伤
逝去的感伤 2021-02-14 17:41

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

5条回答
  •  醉话见心
    2021-02-14 18:11

    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 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();
            }
        }
    }
    

提交回复
热议问题