简体   繁体   English

如何关闭 Chrome 驱动程序并从内存中清除 - selenium C# 和 SpecFlow - 并行测试设置

[英]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).如果您需要更多信息,请告诉我(长期潜伏者,第一次发帖者)。

  • Chromeversion: 85.0.4183.8700 Chrome 版本:85.0.4183.8700
  • Specflow - 3.0.225 Specflow - 3.0.225
  • Selenium webdriver - 3.141硒网络驱动程序 - 3.141

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.

相关问题 如何从 Chrome 驱动程序中分离 Chrome 浏览器(Selenium Web 驱动程序 C#) - How to Detach Chrome Browser from Chrome Driver (Selenium Web Driver C#) 如何使用Selenium Web Driver和C#清除浏览器cookie - How to clear browser cookies using Selenium Web Driver and C# 如何在Specflow / Selenium / C#/ NUnit上实现并行执行? - How do I implement parallel execution on Specflow/Selenium/C#/NUnit? 使用Gallio和Powershell和Chrome Driver在MbUnit框架和Selenium中进行C#并行测试失败-所有测试均失败 - Failed Parallel tests in C# with MbUnit framework and Selenium using Gallio and powershell with the Chrome Driver - all tests fail 如何使用打印选项 C# selenium chrome 驱动程序 - How to use print option C# selenium chrome driver 如何在 c# selenium chrome 驱动程序中验证(用户/密码)代理 - How to authenticate (user/password) proxy in c# selenium chrome driver 里面怎么调用link<td> 硒 c# 铬驱动程序 - How call link inside <td> selenium c# chrome driver 使用 MS Edge 驱动程序进行 C# Selenium 测试 - C# Selenium testing with MS Edge driver 如何使用 C# 中的 Selenium chrome 驱动程序从网页打印 URL 列表以进行控制台? - How do I print a list of URLs to console from a webpage using Selenium chrome driver in C#? C#硒中的Chrome驱动程序问题 - Chrome Driver issues in C# selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM