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

前端 未结 8 1686
清酒与你
清酒与你 2020-11-29 01:39

I\'m looking for the recommended/nicest way to make Selenium tests execute in several browsers one after another. The website I\'m testing isn\'t big, so I don\'t need a par

8条回答
  •  春和景丽
    2020-11-29 02:02

    I use a list of IWeb driver to perform tests on all browsers, line by line:

    [ClassInitialize]
            public static void ClassInitialize(TestContext context) {
                drivers = new List();
                firefoxDriver = new FirefoxDriver();
                chromeDriver = new ChromeDriver(path);
                ieDriver = new InternetExplorerDriver(path);
                drivers.Add(firefoxDriver);
                drivers.Add(chromeDriver);
                drivers.Add(ieDriver);
                baseURL = "http://localhost:4444/";
            }
    
        [ClassCleanup]
        public static void ClassCleanup() {
            drivers.ForEach(x => x.Quit());
        }
    
    ..and then am able to write tests like this:
    
    [TestMethod]
            public void LinkClick() {
                WaitForElementByLinkText("Link");
                drivers.ForEach(x => x.FindElement(By.LinkText("Link")).Click());
                AssertIsAllTrue(x => x.PageSource.Contains("test link")); 
            }
    

    ..where I am writing my own methods WaitForElementByLinkText and AssertIsAllTrue to perform the operation for each driver, and where anything fails, to output a message helping me to identify which browser(s) may have failed:

     public void WaitForElementByLinkText(string linkText) {
                List failedBrowsers = new List();
                foreach (IWebDriver driver in drivers) {
                    try {
                        WebDriverWait wait = new WebDriverWait(clock, driver, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(250));
                        wait.Until((d) => { return d.FindElement(By.LinkText(linkText)).Displayed; });
                    } catch (TimeoutException) {
                        failedBrowsers.Add(driver.GetType().Name + " Link text: " + linkText);
                    }
                }
                Assert.IsTrue(failedBrowsers.Count == 0, "Failed browsers: " + string.Join(", ", failedBrowsers));
            }
    

    The IEDriver is painfully slow but this will have 3 of the main browsers running tests 'side by side'

提交回复
热议问题