硒测试开始后,Chromedriver 不断失去连接
Chromedriver keeps losing connection right after the start of the selenium test
我对单页 Web 应用程序进行了 selenium 测试。当我开始测试时,它开始 chrome,导航到所需的 URL 并最大化浏览器。但仅此而已。在这些步骤之后,它只显示所需 URL 的页面,直到测试因超时而失败:
The HTTP request to the remote WebDriver server for URL http://localhost:59833/session/9b0d7ae349643a68d86bf8ce2ef419ee/element/18f73f99-48e6-4fa9-81a8-d77d3d6ff80c/click timed out after 60 seconds.
----> System.Net.WebException : The request was aborted: The operation has timed out.
Call stack:
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.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at MyTest.Pages.Login..ctor() in D:\Path\To\Test\Project\Pages\Login.cs:line 30
at TearDown.SetUp()
在我看来,只有特定的操作才会产生这个问题。例如。如果我将光标放在页面主体上并单击那里,我的测试将 运行 直到某个测试用例然后断开连接,即使 运行 之前的所有测试用例都通过了完全相同的重新加载代码边.
当我使用 "--headless"
选项初始化 chrome 驱动程序时,会发生完全相同的行为。
几周前我已经遇到了完全相同的问题,但是 genuis "fix" 似乎是开始测试并且不要触摸任何东西(不要移动鼠标,不要按任何键键盘)直到完成。这不再有效并且显然不是最终解决方案,因为测试应该在构建服务器上自动 运行。
我不知道还能做什么。这是我已经尝试过的:
- 建议WebDriver点击页面上的"body"元素:
Driver.FindElement(By.TagName("body")).Click();
- 执行一些 JavaScript 以聚焦浏览器
((IJavaScriptExecutor)driver).ExecuteScript("window.focus();");
- 使用不同的参数初始化 WebDriver,例如
"--no-sandbox"
、"--disable-gpu"
、"ignore-certificate-errors"
、"--headless"
有没有人遇到同样的问题或知道如何解决这个问题?
如果需要我的更多代码,请告诉我。
编辑
以下代码出现问题:
SharedResources.Resources.DefaultTimeout = General.TIMEOUT_IN_MILLISECONDS;
SharedResources.Resources.ScreenShotTaken += OnScreenshotTaken;
// login to the web app to invoke the web api which takes a second to initialize on first use. This is where the problem actually occurs
Login().DoLoginByClickLoginButton(SharedResources.Resources.AdminUser.Username, SharedResources.Resources.AdminUser.Password);
Thread.Sleep(10000);
Login
的构造函数如下所示:
public Login()
{
Driver.Navigate().GoToUrl(General.MAIN_URL);
Driver.Navigate().Refresh();
Driver.Manage().Window.Maximize();
Toolbox.AwaitElementVisible(By.Id(BUTTON_LOGIN_ID), General.TIMEOUT_IN_MILLISECONDS);
Driver.FindElement(By.TagName("body")).Click(); // This line is the one mentioned in the call stack
}
我如何设置 WEBDRIVER
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--no-sandbox");
chromeOptions.AddArgument("--window-size=1920,1200");
chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Accept;
this.WebDriver = new ChromeDriver(ChromeDriverService.CreateDefaultService(Environment.GetEnvironmentVariable("WebDrivers")), chromeOptions);
如果有人遇到同样的问题,我找到了将 ChromeDriver 降级到 2.40.0 的解决方案(比我之前使用的版本 79.0.3945.3600 低了近 20 个版本)
当然这不是最好的解决方案,但我现在至少可以 运行 我的测试没有任何问题。
我对单页 Web 应用程序进行了 selenium 测试。当我开始测试时,它开始 chrome,导航到所需的 URL 并最大化浏览器。但仅此而已。在这些步骤之后,它只显示所需 URL 的页面,直到测试因超时而失败:
The HTTP request to the remote WebDriver server for URL http://localhost:59833/session/9b0d7ae349643a68d86bf8ce2ef419ee/element/18f73f99-48e6-4fa9-81a8-d77d3d6ff80c/click timed out after 60 seconds.
----> System.Net.WebException : The request was aborted: The operation has timed out.
Call stack:
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.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
at MyTest.Pages.Login..ctor() in D:\Path\To\Test\Project\Pages\Login.cs:line 30
at TearDown.SetUp()
在我看来,只有特定的操作才会产生这个问题。例如。如果我将光标放在页面主体上并单击那里,我的测试将 运行 直到某个测试用例然后断开连接,即使 运行 之前的所有测试用例都通过了完全相同的重新加载代码边.
当我使用 "--headless"
选项初始化 chrome 驱动程序时,会发生完全相同的行为。
几周前我已经遇到了完全相同的问题,但是 genuis "fix" 似乎是开始测试并且不要触摸任何东西(不要移动鼠标,不要按任何键键盘)直到完成。这不再有效并且显然不是最终解决方案,因为测试应该在构建服务器上自动 运行。
我不知道还能做什么。这是我已经尝试过的:
- 建议WebDriver点击页面上的"body"元素:
Driver.FindElement(By.TagName("body")).Click();
- 执行一些 JavaScript 以聚焦浏览器
((IJavaScriptExecutor)driver).ExecuteScript("window.focus();");
- 使用不同的参数初始化 WebDriver,例如
"--no-sandbox"
、"--disable-gpu"
、"ignore-certificate-errors"
、"--headless"
有没有人遇到同样的问题或知道如何解决这个问题?
如果需要我的更多代码,请告诉我。
编辑
以下代码出现问题:
SharedResources.Resources.DefaultTimeout = General.TIMEOUT_IN_MILLISECONDS;
SharedResources.Resources.ScreenShotTaken += OnScreenshotTaken;
// login to the web app to invoke the web api which takes a second to initialize on first use. This is where the problem actually occurs
Login().DoLoginByClickLoginButton(SharedResources.Resources.AdminUser.Username, SharedResources.Resources.AdminUser.Password);
Thread.Sleep(10000);
Login
的构造函数如下所示:
public Login()
{
Driver.Navigate().GoToUrl(General.MAIN_URL);
Driver.Navigate().Refresh();
Driver.Manage().Window.Maximize();
Toolbox.AwaitElementVisible(By.Id(BUTTON_LOGIN_ID), General.TIMEOUT_IN_MILLISECONDS);
Driver.FindElement(By.TagName("body")).Click(); // This line is the one mentioned in the call stack
}
我如何设置 WEBDRIVER
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--no-sandbox");
chromeOptions.AddArgument("--window-size=1920,1200");
chromeOptions.UnhandledPromptBehavior = UnhandledPromptBehavior.Accept;
this.WebDriver = new ChromeDriver(ChromeDriverService.CreateDefaultService(Environment.GetEnvironmentVariable("WebDrivers")), chromeOptions);
如果有人遇到同样的问题,我找到了将 ChromeDriver 降级到 2.40.0 的解决方案(比我之前使用的版本 79.0.3945.3600 低了近 20 个版本)
当然这不是最好的解决方案,但我现在至少可以 运行 我的测试没有任何问题。