【问题标题】:Chromedriver localhost with random port "The operation has timed out"带有随机端口的 Chromedriver localhost“操作已超时”
【发布时间】:2020-06-23 16:36:30
【问题描述】:

对 selenium chromedriver 有疑问。我在 AzureDevops CI 上运行了一些测试。一些随机数量的测试失败,但有异常:

    OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:50080/session timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out.

端口号每次都是随机的。

下面是启动chrome的代码:

var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless");
driver = new ChromeDriver(chromeOptions);

没什么特别的,但由于某种原因,测试仍然失败。在我的本地电脑上运行稳定,在 CI 运行期间出现问题

这是完整的堆栈跟踪:

at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
   at ClickBrainAutomation.Common.ConditionsHook.SetUp() in C:\agent_pool\agent_0\_work\610\s\ClickBrainAutomation\Common\ConditionsHook.cs:line 82
   at TechTalk.SpecFlow.Bindings.SynchronousBindingDelegateInvoker.<>c__DisplayClass2_0.<<InvokeBindingDelegateAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at TechTalk.SpecFlow.Bindings.AsyncHelpers.<>c__DisplayClass1_0`1.<<RunSync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at TechTalk.SpecFlow.Bindings.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop()
   at TechTalk.SpecFlow.Bindings.AsyncHelpers.RunSync[T](Func`1 task)
   at TechTalk.SpecFlow.Bindings.SynchronousBindingDelegateInvoker.InvokeBindingDelegateAsync(Delegate bindingDelegate, Object[] invokeArgs)
   at TechTalk.SpecFlow.Bindings.SynchronousBindingDelegateInvoker.InvokeDelegateSynchronously(Delegate bindingDelegate, Object[] invokeArgs)
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioStart()
   at TechTalk.SpecFlow.TestRunner.OnScenarioStart()
   at ClickBrainAutomation.Features.AdFeature.ScenarioStart()
   at ClickBrainAutomation.Features.AdFeature.AdStatusesLifecycle() in C:\agent_pool\agent_0\_work\610\s\ClickBrainAutomation\Features\AdsFeature.feature:line 145
--WebException
   at System.Net.HttpWebRequest.GetResponse()
   at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
--TearDown
   at ClickBrainAutomation.Common.ConditionsHook.AfterScenario() in C:\agent_pool\agent_0\_work\610\s\ClickBrainAutomation\Common\ConditionsHook.cs:line 95
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioEnd()
   at TechTalk.SpecFlow.TestRunner.OnScenarioEnd()
   at ClickBrainAutomation.Features.AdFeature.TestTearDown()

【问题讨论】:

    标签: selenium azure-devops selenium-chromedriver


    【解决方案1】:

    如果您使用 Visual Studio 2017 在本地 PC 上运行测试。您应该在托管代理 vs2017-win2016 上运行 azure devops 管道。如果您在本地计算机上使用 Visual Studio 2019。然后应该使用托管代理 windows-2019

    如果您使用的 Selenium.WebDriver.Chrome 包是 2.43 版本。您可以尝试降级到 2.40.0 版本。

    您也可以尝试传递ChromeDriver.exe二进制文件的目录路径,当new ChromeDriver("full path to the directory containing ChromeDriver.exe", option)。见下文:

    ChromeOptions option = new ChromeOptions();
    option.AddArgument("no-sandbox");
    var path = Environment.GetEnvironmentVariable("ChromeWebDriver");
    driver = new ChromeDriver(path, option, TimeSpan.FromSeconds(130));
    

    更多信息请查看this thread

    如果上述解决方法不起作用。您可以在本地电脑上create a self-hosted agent 运行您的 UI 测试。 (注意:必须将自托管 Windows 代理配置为作为交互式进程运行并启用自动登录。请参阅here

    【讨论】:

    • 我猜(无沙盒)选项是解决方案,我知道为什么,但是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多