在多个驱动程序上运行WebDriver NUnit测试 [英] Run WebDriver NUnit tests on multiple drivers

查看:54
本文介绍了在多个驱动程序上运行WebDriver NUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近开始使用WebDriver(支持Selenium 1),使用NUnit框架执行浏览器测试.由于我们想在各种浏览器中运行测试,因此我们为每个浏览器定义了驱动程序,并在安装夹具时将它们放在列表中:

We recently started to use WebDriver (in favor of Selenium 1) for performing browser tests, using the NUnit framework. Since we want to run the tests in a variety of browsers, we define drivers for each and put them in a list during fixture set up:

[TestFixtureSetUp]
public void SetupTest()
{
    // Load drivers
    Drivers = new List<IWebDriver>
            {
                new ChromeDriver(),
                ...
            };

在每个测试中,我们都会像这样遍历列表:

In every single test we iterate through the list like this:

[Test]
public void SomeTest()
{
    foreach (var driver in Drivers)
    {
        driver.Navigate().GoToUrl("...");
...

在所有测试方法中这样做都是不对的.测试方法不应与它们应使用的驱动程序有关.理想情况下,我们将具有以下内容:

It feels wrong to do this in all the test methods. The test methods should not be concerned with what driver they should work on. Ideally we would have something like this:

public void SomeTest(IWebDriver driver)
{
    driver.Navigate().GoToUrl("...");
...

解决这个问题的一种方法是使用TestCases:

One way we could solve this is by using TestCases:

[TestCase(new ChromeDriver())]
[TestCase(new FireFoxDriver())]
...

但这是很多重复,并将正确初始化驱动程序的问题转移到每个测试的属性中.并不是真正的收获.

But this is a lot of duplication and shifts the problem of correct initialization of the drivers into the attributes of every single tests. Not really a gain.

是否可以通过某种方式告知NUnit框架执行整套测试,并在每次运行中为单独的测试注入不同的参数?还是对此有其他好的解决方案?

Is there any way the NUnit framework can be told to execute the whole suite of tests and inject a different parameter to the individual tests in every run? Or is there any other good solution to this?

推荐答案

您应该可以使用

You should be able to use the TestCaseSourceAttribute. First create a commonly accessible class that provides the collection of web drivers:

public static class WebDriverFactory
{
    public static IWebDriver[] Drivers =
    {
        new ChromeDriver(),
        new FirefoxDriver(),
        ...
    };
}

接下来,像这样实现依赖于Web驱动程序的单元测试:

Next, implement your web driver dependent unit tests like this:

[Test, TestCaseSource(typeof(WebDriverFactory), "Drivers")]
public void SomeTest(IWebDriver driver)
{
    driver.Navigate().GoToUrl("...");
    ...
}

(可选)为减少实现每个单元测试时的键入,还定义一个新的 Attribute 类,该类继承自 TestCaseSourceAttribute ,并且仅实现默认构造函数:

Optionally, to reduce typing when implementing each unit test, also define a new Attribute class that inherits from TestCaseSourceAttribute and that only implements a default constructor:

public class WebDriverSourceAttribute : TestCaseSourceAttribute
{
    public WebDriverSourceAttribute() : base(typeof(WebDriverFactory), "Drivers")
    {            
    }
}

使用继承的 WedDriverSource 属性,现在可以将单元测试简化为:

Using the inherited WedDriverSource attribute, the unit tests can now be simplified to:

[Test, WebDriverSource]
public void SomeTest(IWebDriver driver)
{
    driver.Navigate().GoToUrl("...");
    ...
}

这篇关于在多个驱动程序上运行WebDriver NUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆