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

时间:2014-04-02 07:26:34

标签: c# selenium-webdriver webdriver xunit.net

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

}

3 个答案:

答案 0 :(得分:5)

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

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

IUseFixture<T>

答案 1 :(得分:0)

经过一番调查后能够找到解决方案,并将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");
    }
}

答案 2 :(得分: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;
    }
}