簡體   English   中英

使用[TestFixture]對多個驅動程序運行WebDriver NUnit測試

[英]Run WebDriver NUnit tests on multiple drivers with [TestFixture]

不知道我在這里做錯了什么。 我正在嘗試學習webdriver和c#。 當我在nunit中運行此測試時,它會給我以下錯誤

TestPage.TestSetup(Chrome).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測試中獲得了這個想法

但它似乎對我不起作用。

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

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM