使用[TestFixture]对多个驱动程序运行WebDriver NUnit测试

时间:2013-12-06 02:06:32

标签: c# selenium webdriver nunit

不确定我在这里做错了什么。我正在尝试学习webdriver和c#。 当我在nunit中运行此测试时,它会给我以下错误

TestPage.TestSetup(铬).TestPage: TestPage.TestSetup没有合适的构造函数

到目前为止,这是我的代码。

namespace TestPage 
{ 
    [TestFixture(Browser.Firefox)]
    [TestFixture(Browser.Chrome)]
    [TestFixture(Browser.IE)]
    public class TestSetup : SetUps 
    {
      /* [TestCase(Browser.Firefox)]   // This solves the issue but that creates a lot of duplicates.
        [TestCase(Browser.Chrome)]
        [TestCase(Browser.IE)]*/
        [Test]
        public void TestPage(Browser browser)
        {

            driver = GetWebDriverForBrowser(browser); 
            driver.Navigate().GoToUrl(baseURL);
            ...
        }

    }
}   


  namespace SetUps
    {
        public class SetUps
        {
            protected IWebDriver driver;
            protected StringBuilder verificationErrors;
            protected string baseURL;

            [SetUp]
            public void SetupTest()
            {
                baseURL = "www.ggogle.com";
                verificationErrors = new StringBuilder();
            }

            [TearDown]
            public void TeardownTest()
            {
                try
                {
                    driver.Quit();
                }
                catch (Exception)
                {
                    // Ignore errors if unable to close the browser
                }
                Assert.AreEqual("", verificationErrors.ToString());
            }
            public enum Browser
            {
                Chrome,
                Firefox,
                IE
            }
            public IWebDriver GetWebDriverForBrowser(Browser browser)
            {
                IWebDriver driver = null;

                switch (browser)
                {
                    case Browser.Chrome:
                        driver = new ChromeDriver(@"C:\repos\Testing\Tests");
                        break;

                    case Browser.Firefox:
                        driver = new FirefoxDriver();
                        break;

                    case Browser.IE:
                        driver = new InternetExplorerDriver(@"C:\repos\Testing\Tests");
                        break;
                }

                if (driver != null)
                {
                    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
                }
                return driver;
            }

        }
    }

Run WebDriver NUnit tests on multiple drivers

得到了这个想法

但它似乎对我不起作用。

1 个答案:

答案 0 :(得分:2)

namespace TestPage 
{ 
    [TestFixture(Browser.Firefox)]
    [TestFixture(Browser.Chrome)]
    [TestFixture(Browser.IE)]
    public class TestSetup : SetUps 
    {
    public TestSetup (Browser browser)
        {
            driver = GetWebDriverForBrowser(browser);   // This part solved the issue.
        }      

    [Test]
        public void TestPage()
        {

            driver.Navigate().GoToUrl(baseURL);
            ...
        }

    }
}   


  namespace SetUps
    {
        public class SetUps
        {
            protected IWebDriver driver;
            protected StringBuilder verificationErrors;
            protected string baseURL;

            [SetUp]
            public void SetupTest()
            {
                baseURL = "www.ggogle.com";
                verificationErrors = new StringBuilder();
            }

            [TearDown]
            public void TeardownTest()
            {
                try
                {
                    driver.Quit();
                }
                catch (Exception)
                {
                    // Ignore errors if unable to close the browser
                }
                Assert.AreEqual("", verificationErrors.ToString());
            }
            public enum Browser
            {
                Chrome,
                Firefox,
                IE
            }
            public IWebDriver GetWebDriverForBrowser(Browser browser)
            {
                IWebDriver driver = null;

                switch (browser)
                {
                    case Browser.Chrome:
                        driver = new ChromeDriver(@"C:\repos\Testing\Tests");
                        break;

                    case Browser.Firefox:
                        driver = new FirefoxDriver();
                        break;

                    case Browser.IE:
                        driver = new InternetExplorerDriver(@"C:\repos\Testing\Tests");
                        break;
                }

                if (driver != null)
                {
                    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
                }
                return driver;
            }

        }
    }