【问题标题】:ChromeDriver exception reporting "target window already closed" in headless mode无头模式下ChromeDriver异常报告“目标窗口已关闭”
【发布时间】:2018-04-23 11:43:09
【问题描述】:

我正在使用 xUnit、SpecFlow、Selenium 和无头 Chrome 来运行自动化测试,但在尝试捕获最终浏览器窗口的屏幕截图时,我经常会在 localhost 上运行时遇到一系列崩溃。

有两个场景,每个场景都在一个单独的功能文件中,这意味着它们并行运行。似乎是第二个启动的场景总是失败。

这是我创建浏览器实例的方式:

    private static readonly string UserDataDir = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;

    var options = new ChromeOptions();
    options.AddArguments("--disable-gpu");
    options.AddArguments("--no-sandbox");
    options.AddArgument("--ignore-certificate-errors");
    options.AddArgument("--disable-web-security");
    options.AddArgument("--allow-insecure-localhost");
    options.AddArgument("--allow-running-insecure-content");
    options.AddArgument("--acceptInsecureCerts=true");
    options.AddArgument("--proxy-server='direct://'");
    options.AddArgument("--proxy-bypass-list=*");
    options.AddArgument("--disable-extensions");
    options.AddArgument($@"--user-data-dir={UserDataDir}\prof-{ProfileCounter++}");
    options.AddArgument("--incognito");
    options.AddArgument("--headless");

    var svc = ChromeDriverService.CreateDefaultService();
    svc.Port = Randomiser.Next(29700, 29900);

    Context.Driver = new ChromeDriver(svc, options, TimeSpan.FromMinutes(2));

    Context.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(ImplicitWaitSeconds);
    Context.Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(PageLoadSeconds);
    Context.Driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(AsynchronousJavaScriptSeconds);

这就是我关闭浏览器/驱动程序的方式:

[AfterScenario]
public void AfterScenario()
{
    Context.Driver.Quit();
    Context.Driver.Dispose();
    Context.Driver = null;
}

这是发生异常的地方:

    Context.Driver.GetScreenshot().SaveAsFile($@"{folder}{filename}.jpg", ScreenshotImageFormat.Jpeg);

以下是输出窗格中的完整例外:

------ Run test started ------
NUnit Adapter 3.10.0.21: Test execution started
Running all tests in C:\git\Testing\bin\Debug\Testing.dll
NUnit couldn't find any tests in C:\git\Testing\bin\Debug\Testing.dll
NUnit Adapter 3.10.0.21: Test execution complete
[xUnit.net 00:00:00.5180673]   Discovering: Testing
[xUnit.net 00:00:01.0205999]   Discovered:  Testing
[xUnit.net 00:00:01.0244487]   Starting:    Testing
[xUnit.net 00:00:14.7836328]     View the Daring Fireball talk show [FAIL]
[xUnit.net 00:00:14.7874843]       OpenQA.Selenium.NoSuchWindowException : no such window: target window already closed
from unknown error: web view not found
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 6.1.7601 SP1 x86_64)
[xUnit.net 00:00:14.7898949]       Stack Trace:
[xUnit.net 00:00:14.7911454]            at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
[xUnit.net 00:00:14.7916551]            at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[xUnit.net 00:00:14.7923841]            at OpenQA.Selenium.Remote.RemoteWebDriver.GetScreenshot()
[xUnit.net 00:00:14.7929467]         C:\git\Testing\StepDefinitions\Common\Implementation\PageNavigationStepsImplementation.cs(90,0): at Testing.StepDefinitions.Common.Implementation.PageNavigationStepsImplementation.TakeScreenshot(String filename)
[xUnit.net 00:00:14.7933710]         C:\git\Testing\StepDefinitions\FeatureProperties.cs(122,0): at Testing.StepDefinitions.FeatureProperties.AfterScenario()
[xUnit.net 00:00:14.7939053]            at lambda_method(Closure , IContextManager )
[xUnit.net 00:00:14.7944826]            at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
[xUnit.net 00:00:14.7949636]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType)
[xUnit.net 00:00:14.7956197]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
[xUnit.net 00:00:14.7960898]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
[xUnit.net 00:00:14.7966550]            at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioEnd()
[xUnit.net 00:00:14.7971662]            at TechTalk.SpecFlow.TestRunner.OnScenarioEnd()
[xUnit.net 00:00:14.7975471]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.RunScenario(SpecFlowDocument gherkinDocument, Scenario scenario)
[xUnit.net 00:00:14.7979477]            at SpecFlow.xUnitAdapter.SpecFlowPlugin.Runners.ScenarioTestCaseRunner.<>c__DisplayClass9_0.<RunTestAsync>b__2()
[xUnit.net 00:00:14.7990766]       Output:
[xUnit.net 00:00:14.7995180]         Given I am on the https://daringfireball.net/ page
[xUnit.net 00:00:14.7998653]         -> done: PageNavigationSteps.IAmOnThePage("https://daringfir...") (11.5s)
[xUnit.net 00:00:14.8002840]         When I click element THE TALK SHOW found by text
[xUnit.net 00:00:14.8006231]         -> error: no such window: target window already closed
from unknown error: web view not found
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 6.1.7601 SP1 x86_64)
[xUnit.net 00:00:14.8009870]         Then I arrive on the page titled Daring Fireball: The Talk Show
[xUnit.net 00:00:14.8013918]         -> skipped because of previous errors
[xUnit.net 00:00:18.8364913]   Finished:    Testing
========== Run test finished: 3 run (0:00:20.3) ==========

更新

步骤代码:

[When(@"I click element (.*)")]
public void IClickElement(string id)
{
    try {
        Context.Driver.FindElementsByPartialLinkText(id).FirstOrDefault();
    } catch (Exception ex) {
        var mssg = ex.Message; // debugger stops here (see exception below)
    }
}

AfterScenario 代码:

[AfterScenario]
public void AfterScenario()
{
    Context.Driver.Quit();
    Context.Driver.Dispose();
    Context.Driver = null;
    Context.NgDriver = null;
}

虚假的、不规则的错误信息:

no such window: target window already closed
from unknown error: web view not found
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 6.1.7601 SP1 x86_64)

相关的堆栈跟踪:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElements(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementsByPartialLinkText(String partialLinkText)
   at Testing.StepDefinitions.Common.Implementation.GenericNavigationStepsImplementations.FindElement(Matcher match, String id, String attributeValue) in C:\git\Testing\Testing\StepDefinitions\Common\Implementation\GenericNavigationStepsImplementations.cs:line 73

AfterScenario 没有任何捕获,但随着屏幕截图尝试的删除,它不再抛出。在步骤定义的 catch 处放置一个停止点表示浏览器已关闭。

更新 2

将 Chrome 更新到 66。异常消息:

no such window: target window already closed
from unknown error: web view not found
  (Session info: headless chrome=66.0.3359.117)
  (Driver info: chromedriver=2.38.551601 (edb21f07fc70e9027c746edd3201443e011a61ed),platform=Windows NT 6.1.7601 SP1 x86_64)

堆栈跟踪:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElements(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementsByPartialLinkText(String partialLinkText)
   at Testing.StepDefinitions.Common.Implementation.GenericNavigationStepsImplementations.FindElement(Matcher match, String id, String attributeValue) in C:\git\Testing\Testing\StepDefinitions\Common\Implementation\GenericNavigationStepsImplementations.cs:line 73

同样的错误,同样的代码。

【问题讨论】:

  • 我之前有 Driver.Close();在 AfterScenario 中,现在已将其删除,因为我已经看到很多帖子暗示该方法应该被弃用。
  • 为什么截屏是在AfterScenario做的?可能窗口甚至没有启动您尝试捕获屏幕截图
  • AfterScenario 无论发生什么都会尝试截取屏幕截图 - 我只是没有检查以防止在浏览器关闭时发生这种情况(它永远不应该关闭。)“目标窗口已关闭”错误实际上是在调用 AfterScenario 之前发生的 - 它发生在导航页面的步骤之一期间。在这种情况下,它发生在“当我单击通过文本找到的 TALK SHOW 元素时”步骤中。
  • 您能否确保您的测试有尝试并捕获并打印异常,这将确保它不是异常传播,并且在调用场景之后也会失败。此外,如果没有尝试抓住它,我永远不会在 AfterScenario 中做任何事情。
  • 我在步骤中添加了一些错误捕获并从 AfterScenario 中删除了屏幕截图代码。

标签: c# selenium-chromedriver .net-4.6.1


【解决方案1】:

正如所讨论的,这个问题可能是由于多种原因

  1. svc.Port = Randomiser.Next(29700, 29900);。你永远不应该自己分配端口,它也可能与其他一些程序端口冲突,甚至可以随机生成相同的端口号,这会导致问题

  2. 您正在并行启动脚本。在极少数情况下,驱动程序启动时可能会出现竞争情况,并且驱动程序可能无法启动。所以我会在我的驱动程序初始化代码中添加工厂模式,如下所示

    public sealed class ChromeFactory 
    { 
        private static readonly object padlock = new object();
    
        ChromeFactory()
        {
        }
    
        public static WebDriver NewInstance
        {
            get
            {
                lock (padlock)
                {
                    return new ChromeDriver();
                }
            }
        }
    }
    

并像下面这样初始化驱动程序

Context.Driver = ChromeFactory.NewInstance
  1. 由于您有并行执行,我建议不要使用 #2 建议,而是设置一个 selenium 网格和浏览器限制计数并使用它。这将使您的生活更加简单

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多