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

前端 未结 5 1317
眼角桃花
眼角桃花 2021-02-14 17:51

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:14

    I recommend an important variation on Lucifer's solution. Have the attribute supply a factory, not a driver, to Fact/Theory methods. Why? Well, when you see all those browser windows pop-up (n x m, where n = number of tests, m = browsers per test), you'll decide you only want them created when the test is actually going to run.

    So with updates from prior solution, including some name changes...

    using OpenQA.Selenium;
    using SeleniumPageObjectsPatternExample.Attributes;
    using SeleniumPageObjectsPatternExample.PageObjects;
    using Xunit;
    using Xunit.Extensions;
    
    namespace SeleniumHelpers
    {    
        public class HomepageTests 
        {
            [Theory]
            [WebDriver(DriverType.Firefox)]
            [WebDriver(DriverType.GoogleChrome)]
            public void HomepageLinksToBlogPage(WebDriverFactory factory)
            {
                // arrange 
                IWebDriver webDriver = factory.Create(); // Browser starts here.
                var expected = "some expected value";
    
                // act
                var homepage = new HomePage(webDriver, true);
    
                // assert
                Assert.True(homepage.BlogLink.Displayed);
                Assert.Equal(expected, homepage.Header.Text);
    
                // Optional cleanup (better wrapped in try/finally for entire method).
                webDriver.Quit();
                webDriver.Dispose();
            }
        }
    }
    

    Updated DataAttribute... (Note, I corrected the signature of the GetData() method to work with the latest xUnit.)

    using System.Reflection;
    using OpenQA.Selenium;
    using Xunit.Extensions;
    
    namespace SeleniumHelpers
    {    
        public class WebDriverAttribute : DataAttribute
        {
            private WebDriverFactory WebDriverFactory { get; set; }
    
            public WebDriverAttribute(Type browser)
            {
                this.WebDriver = WebDriverFactory.Create(browser);
            }
    
            public override IEnumerable GetData(MethodInfo methodUnderTest)
            {
                return new[] { new[] { this.WebDriverFactory } };
            }
        }
    }
    

    The new WebDriverFactory

    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    
    namespace SeleniumHelpers
    {    
        public enum DriverType
        {
            Firefox,
            GoogleChrome
        }
    
        public class WebDriverFactory
        {
            private DriverType _driverType;
            public WebDriverFactory(DriverType driverType)
            {
                _driverType = driverType;
            }
    
            public static IWebDriver Create(Type browser)
            {
                IWebDriver webDriver;
    
                switch (browser)
                {
                    case Type.Firefox:
                        webDriver = new FirefoxDriver();
                        break;
                    case Type.GoogleChrome:
                        webDriver = new ChromeDriver();
                        break;
                    default:
                        webDriver = new ChromeDriver();
                        break;
                }
    
                webDriver.Manage().Window.Maximize();
    
                return webDriver;
            }
        }
    }
    

提交回复
热议问题