C# Selenium Specflow钩子在测试运行后关闭驱动程序
我目前有一个非常简单的SeleniumSpecFlow框架,可以打开Chrome或Firefox浏览器。我想在启动后添加一个C# Selenium Specflow钩子在测试运行后关闭驱动程序,c#,selenium,selenium-chromedriver,hook,specflow,C#,Selenium,Selenium Chromedriver,Hook,Specflow,我目前有一个非常简单的SeleniumSpecFlow框架,可以打开Chrome或Firefox浏览器。我想在启动后添加一个钩子来退出并处理驱动程序,以便浏览器正确关闭 LoginPage.cs: [Binding] public class LoginPage { private IWebDriver Driver { get; set; } [Given("I open a (.*) browser with a (.*) resolution (.*)")] pu
钩子来退出并处理驱动程序,以便浏览器正确关闭
LoginPage.cs:
[Binding]
public class LoginPage
{
private IWebDriver Driver { get; set; }
[Given("I open a (.*) browser with a (.*) resolution (.*)")]
public IWebDriver CreateBrowserInstance(Browser browser, BrowserResolution resolution, bool headless)
{
switch(browser)
{
case Browser.Chrome:
Driver = StaticWebDriverFactory.GetChromeWebDriver(resolution, headless);
return Driver;
case Browser.Firefox:
Driver = StaticWebDriverFactory.GetFirefoxWebDriver(resolution, headless);
return Driver;
default:
throw new PlatformNotSupportedException($"{browser} is not currently supported.");
}
}
}
Hooks.cs
[Binding]
public class Hooks
{
private IWebDriver Driver { get; }
[AfterTestRun]
public void AfterTestRun()
{
Driver.Quit();
Driver.Dispose();
}
}
当我没有Hooks文件时,测试将按预期打开浏览器。不管我们如何添加它并运行测试,测试都会被跳过。我不确定钩子哪里出了问题
编辑:
[Binding]
public class LoginPage
{
private IWebDriver Driver { get; set; }
[Given("I open a (.*) browser with a (.*) resolution (.*)")]
public IWebDriver CreateBrowserInstance(Browser browser, BrowserResolution resolution, bool headless)
{
switch(browser)
{
case Browser.Chrome:
Driver = StaticWebDriverFactory.GetChromeWebDriver(resolution, headless);
return Driver;
case Browser.Firefox:
Driver = StaticWebDriverFactory.GetFirefoxWebDriver(resolution, headless);
return Driver;
default:
throw new PlatformNotSupportedException($"{browser} is not currently supported.");
}
}
}
我试图将[AfterScenario]
放入LoginPage.cs
文件中,结果一切正常
我不确定为什么我不能将我的测试挂钩放在一个单独的文件中,因为它们将被所有测试/页面使用。我是不是看错了
我不确定为什么我不能将我的测试挂钩作为一个单独的文件保存在一个单独的文件中
所有测试/页面都将使用它们。我看错了吗
路
您可以将测试挂钩放在单独的文件中[这是最佳实践],也可以多次使用。测试运行程序将执行所有hooks方法
前/后功能和前/后测试运行事件的绑定方法必须是静态的
现在,在您的示例中,调试并检查Driver
属性是否具有要在后测试运行方法中关闭的webdriver实例
通过查看代码,您的Hooks类中的Driver
属性似乎没有webdriver的当前实例。在LoginPage类中,您需要在构造函数中调用Hooks。我看不到LoginPage类的任何构造函数。