Run Selenium tests in multiple browsers with a base class

前端 未结 1 1722
甜味超标
甜味超标 2021-01-14 02:07

Let\'s say I Have code:

namespace SeleniumTests 
{
    [TestFixture(typeof(FirefoxDriver))]
    [TestFixture(typeof(InternetExplorerDriver))]
    public clas         


        
1条回答
  •  执笔经年
    2021-01-14 02:27

    This doesn't use generics like your example but instead kind of works like dependency injection.

    You need a method to create the instance of the webdriver.

    public class DriverFactory
    {
        public IWebDriver Driver { get; set; }
    
        public enum DriverType
        {
            IE,
            Firefox,
            Chrome
        }
    
        public IWebDriver GetDriver(DriverType typeOfDriver)
        {
            if (typeOfDriver == DriverType.IE) return new InternetExplorerDriver();
            if (typeOfDriver == DriverType.Chrome) return new ChromeDriver();
            return new FirefoxDriver(); // return firefox by default
        }
    }
    

    Then call from your setup:

    [Setup]
    public void CreateDriver()
    {
        var driverFactory = new DriverFactory();
        this.driver = driverFactory.GetDriver(DriverType.Chrome);
    }
    

    You can inherit the class:

    public class TestWithMultipleBrowsers : DriverFactory
    

    0 讨论(0)
提交回复
热议问题