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

问题描述 投票:8回答:3

有没有办法使用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");
    }

}
c# selenium-webdriver webdriver xunit.net
3个回答
5
投票
虽然我不了解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"); } }


1
投票
经过一番调查后能够在这里找到解决方案,并将Firefox升级为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"); } }


0
投票
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; } }

© www.soinside.com 2019 - 2024. All rights reserved.