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

前端 未结 8 1651
清酒与你
清酒与你 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 01:59

    This helped me to solve similar problem How do I run a set of nUnit tests with two different setups?

    Just set different browsers in setup method : ]

    0 讨论(0)
  • 2020-11-29 02:02

    This is basically just an expansion of alanning's answer (Oct 21 '11 at 20:20). My case was similar, just that I did not want to run with the parameterless constructor (and thus use the default path to the driver executables). I had a separate folder containing the drivers I wanted to test against, and this seems to work out nicely:

    [TestFixture(typeof(ChromeDriver))]
    [TestFixture(typeof(InternetExplorerDriver))]
    public class BrowserTests<TWebDriver> where TWebDriver : IWebDriver, new()
    {
        private IWebDriver _webDriver;
    
        [SetUp]
        public void SetUp()
        {
            string driversPath = Environment.CurrentDirectory + @"\..\..\..\WebDrivers\";
    
            _webDriver = Activator.CreateInstance(typeof (TWebDriver), new object[] { driversPath }) as IWebDriver;
        }
    
        [TearDown]
        public void TearDown()
        {
            _webDriver.Dispose(); // Actively dispose it, doesn't seem to do so itself
        }
    
        [Test]
        public void Tests()
        {
            //TestCode
        }
    }
    

    }

    0 讨论(0)
  • 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<IWebDriver>();
                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<string> failedBrowsers = new List<string>();
                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'

    0 讨论(0)
  • 2020-11-29 02:03

    I was wondering about the same issue and finally i got solution

    So when you install plugin you then are able to controll in which browser should scenario be tested.

    Feature example:

    @Browser:IE
    @Browser:Chrome
    Scenario Outline: Add Two Numbers
        Given I navigated to /
        And I have entered <SummandOne> into summandOne calculator
        And I have entered <SummandTwo> into summandTwo calculator
        When I press add
        Then the result should be <Result> on the screen
        Scenarios:
            | SummandOne | SummandTwo | Result |
            | 50 | 70 | 120 |
            | 1 | 10 | 11 |
    

    Implementation

    [Given(@"I have entered '(.*)' into the commentbox")]
    public void GivenIHaveEnteredIntoTheCommentbox(string p0)
    {
                Browser.Current.FindElement(By.Id("comments")).SendKeys(p0);
    }
    

    More info

    0 讨论(0)
  • 2020-11-29 02:04

    There must be a better way, but you could use T4 templates to generate duplicate test classes for each browser - essentially automating a copy-and-paste of the tests for each browser.

    0 讨论(0)
  • 2020-11-29 02:08

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

    Running the following example will execute the GoogleTest twice, once in Firefox and once in 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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题