问题
I am in the process of converting a bunch of selenium test cases over to SpecFlow. Everything is working well but what is driving me crazy is that I am using an after scenario to do a driver.quit. When that executes, I have roughly 4-6 browsers opening and closing. In looking at the task manager, it is killing all of the chromedriver.exe (a second one is opened somewhere) and chrome.exe processes successfully which is great. If I run the same test in selenium, I just have one browser and it closes. When SpecFlow runs now, it basically adds 10-20 seconds onto my runs as it opens and closes browsers.
If I change my browser to Firefox I get the same thing. One browser opens and at completion, several blank firefox browsers open and close.
Any ideas what could be causing this behavior? I have searched all over and no one seems to have ever experienced this but me.
Appreciate the help.
Here is teardown code. I have it in a basetestobject class and it is inherited by the specflow classes. I have tried removing the [TestCleanup] but the same result applies.
BaseTestObject.cs
public class BaseTestObject
{
private IWebDriver seleniumDriver;
private IDriver testDriver;
[TestInitialize]
public virtual void Setup()
{
TestDriver.goToUrl(ConfigProperties.StartUrl);
}
[AfterScenario]
public void ScenarioTearDown()
{
TestDriver.Quit();
}
[TestCleanup]
public void CleanUp()
{
TestDriver.Quit();
}
public IWebDriver SeleniumDriver
{
get
{
if (seleniumDriver == null)
{
seleniumDriver = GetDriver();
}
return seleniumDriver;
}
}
public IDriver TestDriver
{
get
{
if (testDriver == null)
{
testDriver = new UiDriver(SeleniumDriver);
}
return testDriver;
}
}
public CurrentPageObjectScope On
{
get
{
return new CurrentPageObjectScope(TestDriver);
}
}
public static String GetTimestamp()
{
return DateTime.Now.ToString("yyyyMMddhhmmssfff");
}
public static String GetTimestamp2()
{
return DateTime.Now.ToString("M/d/yyyy");
}
private IWebDriver GetDriver()
{
switch (ConfigProperties.Browser.ToLower())
{
case "firefox":
return new FirefoxDriver();
case "chrome":
ChromeOptions options = new ChromeOptions();
ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"../Chrome/");
service.SuppressInitialDiagnosticInformation = true;
service.HideCommandPromptWindow = true;
options.AddArguments("test-type");
options.AddArgument("--start-maximized");
return new ChromeDriver(service, options);
case "ie":
case "internetexplorer":
return new InternetExplorerDriver(@"../IE/");
default:
throw new NotImplementedException("Unknown browser string in Config properties " + ConfigProperties.Browser);
}
}
Edit 9.15.14
resolved by adding the below in my baseTestObject. Thanks for the help.
[AfterScenario]
public void CleanUp()
{
if (seleniumDriver != null)
{
SeleniumDriver.Dispose();
seleniumDriver = null;
}
}
回答1:
We ended up doing what's described in this post. It allows browser reuse (if you want) between scenarios and closes everything only "AfterFeature" (not after scenario run).
来源:https://stackoverflow.com/questions/25467957/specflow-teardown-opens-and-closes-multiple-blank-browsers