本文介绍了在多个浏览器运行Selenium测试,一个基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有代码:

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



我想阻止

I want block

    [SetUp]
    public void CreateDriver () {
        this.driver = new TWebDriver();
    }



移动到基类。但我并不在此情况下知道如何从基类继承。如何处理< TWebDriver>其中,TWebDriver:IWebDriver,新的()

推荐答案

这不使用泛型喜欢你的榜样?而是一种工作方式类似依赖注入。

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

您需要一种方法来创建的webdriver的实例。

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

您可以继承类:

public class TestWithMultipleBrowsers : DriverFactory

这篇关于在多个浏览器运行Selenium测试,一个基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:04