I currently have a very simple Selenium Specflow framework which open a Chrome or Firefox browser. I want to add an AfterTestRun
hook to quit and dispose of the driver so that the browser closes correctly.
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();
}
}
When I don't have the Hooks file, the test will open the browser as expected. However we I add it in and run the test, the test is skipped. I am not sure where I am going wrong with the hooks.
Edit:
I tried to put the [AfterScenario]
in the LoginPage.cs
file and it worked as expected.
I am not sure why I cannot have my test hooks in a separate file as they will be used by all tests/pages. Am I looking at this the wrong way?