Xunit 为每个新测试(使用 WebDriver 和 C#)创建 Test 类的新实例

本文关键字:实例 创建 Test 新实例 使用 新测试 Xunit 测试 WebDriver | 更新日期: 2023-09-27 17:56:10

有没有办法使用Xunit使用Webdriver(Selenium)在同一浏览器中运行多个测试,目前xunit为每个新测试推出新的浏览器,下面是示例代码

public class Class1
{
    private FirefoxDriver driver;
    public Class1()
    {
         driver = new FirefoxDriver();
    }
    [Fact]
    public void Test()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElementById("gbqfq").SendKeys("Testing");
    }
    [Fact]
    public void Test2()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElementById("gbqfq").SendKeys("Testing again");
    }
}

Xunit 为每个新测试(使用 WebDriver 和 C#)创建 Test 类的新实例

虽然我不知道 Selenium,但我知道 xUnit.net 为每个测试方法创建一个测试类的新实例,所以这可能解释了为什么你会看到你报告的行为:driver字段为每个测试方法重新初始化,因为每次都会调用构造函数。

为了重用单个FirefoxDriver实例,您可以使用xUnit.net的IUseFixture<T>接口:

public class Class1 : IUseFixture<FirefoxDriver>
{
    private FirefoxDriver driver;
    public void SetFixture(FirefoxDriver data)
    {
        driver = data;
    }
    [Fact]
    public void Test()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElementById("gbqfq").SendKeys("Testing");
    }
    [Fact]
    public void Test2()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElementById("gbqfq").SendKeys("Testing again");
    }    
}

经过一些调查,能够在这里找到解决方案,并将FirefoxDriver更新为IWebDriver:

   public class SampleFixture : IDisposable
   {
    private IWebDriver driver;
    public SampleFixture()
    {
        driver = new FirefoxDriver();
        Console.WriteLine("SampleFixture constructor called");
    }
    public IWebDriver InitiateDriver()
    {
        return driver;
    }
    public void Dispose()
    {
       // driver.Close();
        driver.Quit();
        Console.WriteLine("Disposing Fixture");
    }
}
public class Class1 : IUseFixture<SampleFixture>
{
    private IWebDriver driver;
    public void SetFixture(SampleFixture data)
    {
        driver = data.InitiateDriver();
    }
    [Fact]
    public void Test()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
    }
    [Fact]
    public void Test2()
    {
        driver.Navigate().GoToUrl("http://google.com");
        driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
    }
}

>IUseFixture不再存在,似乎被IClassFixture取代。但我不能直接注入@Mark Seemann发布的FirefoxDriver

public class DashboardCategoryBoxes : IClassFixture<FirefoxDriver> {
    IWebDriver driver;
    public DashboardCategoryBoxes(FirefoxDriver driver) {
        //this.driver = wrapper.Driver;
        this.driver = driver;
    }
}

这会引发错误

 System.AggregateException : One or more errors occurred. (Class fixture type 'OpenQA.Selenium.Firefox.FirefoxDriver' may only define a single public constructor.) (The following constructor parameters did not have matching fixture data: FirefoxDriver driver)
    ---- Class fixture type 'OpenQA.Selenium.Firefox.FirefoxDriver' may only define a single public constructor.
    ---- The following constructor parameters did not have matching fixture data: FirefoxDriver driver

作为一种解决方法,我们可以创建一些没有构造函数的包装类

public class FirefoxWrapper {
    FirefoxDriver driver = new FirefoxDriver();
    public FirefoxWrapper Driver {
        get { return driver; }
    }
}

并从那里获取驱动程序

public class DashboardCategoryBoxes : IClassFixture<FirefoxWrapper> {
    IWebDriver driver;
    public DashboardCategoryBoxes(FirefoxWrapper wrapper) {
        driver = wrapper.Driver;
    }
}