[英]How to close Chrome driver and clear from memory - selenium C# and SpecFlow - parallel testing setup
Currently when running tests, Chrome driver's stack up and are never fully closed.目前,在运行测试时,Chrome 驱动程序会堆积起来并且永远不会完全关闭。 Have tried using:
曾尝试使用:
driver.Close();
driver.Quit();
driver.Dispose();
Relating to this discussion与本次讨论有关
But the chrome drivers still seem to exist after tests have run / finished.但是在测试运行/完成后,chrome 驱动程序似乎仍然存在。
After scenario hook -场景挂钩后 -
[AfterScenario]
public void AfterScenario()
{
var driver = _objectContainer.Resolve<IWebDriver>();
driver?.Quit();
driver?.Dispose();
}
After feature hook -功能挂钩后 -
[AfterFeature]
public static void AfterFeature(FeatureContext featureContext)
{
var driverService = featureContext.Get<ChromeDriverService>();
driverService.Dispose();
}
Running 4 tests in parallel currently so want to avoid closing all browsers by accident.当前并行运行 4 个测试,因此希望避免意外关闭所有浏览器。
Any help appreciated.任何帮助表示赞赏。 New to QA testing automation and just learning my way around things.
QA 测试自动化的新手,只是在学习解决问题的方法。 Have confirmed the
AfterScenario
is being called when a scenario finishes but they still seem to be lurking and slowing down my machine locally when they stack up.已经确认
AfterScenario
在场景结束时被调用,但是当它们堆积起来时,它们似乎仍然潜伏并在本地减慢我的机器的速度。 As mentioned I have tried driver.Close()
but this hasn't helped.如前所述,我尝试过
driver.Close()
但这并没有帮助。
Let me know if you need any more information (long time lurker, first time poster).如果您需要更多信息,请告诉我(长期潜伏者,第一次发帖者)。
Since you already have a ChromeDriverService available, kill the process explicitly in your [AfterScenario]
hook:由于您已经有一个 ChromeDriverService 可用,请在
[AfterScenario]
挂钩中明确[AfterScenario]
该进程:
[AfterScenario]
public void AfterScenario()
{
// Add a FeatureContext parameter to the class constructor if need be
var driverService = featureContext.Get<ChromeDriverService>();
var driver = _objectContainer.Resolve<IWebDriver>();
try
{
if (driverService.ProcessId != default)
{
Trace.WriteLine("Attempting to kill web driver service process #" + driverService.ProcessId);
var process = Process.GetByProcessId(driverService.ProcessId);
process.Kill();
Trace.WriteLine(" > Done killing process #" + driverService.ProcessId);
}
}
catch (Exception ex)
{
Trace.WriteLine("Exception thrown when killing web driver service: " + ex);
}
try
{
driverService.Dispose();
Trace.WriteLine("Web driver service disposed of.");
}
catch (Exception ex)
{
Trace.WriteLine("Exception thrown when disposing of web driver service: " + ex);
}
driver?.Close();
driver?.Quit();
driver?.Dispose();
}
Since Chrome 85, I've noticed that these ChromeDriver.exe processes are not cleaning up as easily as they were before.从 Chrome 85 开始,我注意到这些 ChromeDriver.exe 进程不像以前那样容易清理。 You would think the
Dispose()
method would kill the process, but lately I've had to add some extra strength cleanup and error handling.您可能会认为
Dispose()
方法会终止进程,但最近我不得不添加一些额外的强度清理和错误处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.