有什么方法可以使用 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");
}
}
最佳答案
虽然我不了解 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");
}
}
关于c# - Xunit 为每个新测试创建新的测试类实例(使用 WebDriver 和 C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22804317/