C# Selenium WebDriver偶尔抛出超时异常
在我们的项目上使用selenium进行ui测试。我们正在运行最新版本2.30.0。 我们使用FirefoxWebDriver并运行Firefox19.0 一般来说,当我在VisualStudio中运行ui测试时,ui测试可以在本地甚至服务器端工作。我们的ui测试几乎在构建服务器上执行。它在我通过VisualStudio手动测试的同一台服务器上使用相同的部署 但在buildserver上执行ui测试时,我们偶尔会遇到以下问题:C# Selenium WebDriver偶尔抛出超时异常,c#,.net,testing,selenium,webdriver,C#,.net,Testing,Selenium,Webdriver,在我们的项目上使用selenium进行ui测试。我们正在运行最新版本2.30.0。 我们使用FirefoxWebDriver并运行Firefox19.0 一般来说,当我在VisualStudio中运行ui测试时,ui测试可以在本地甚至服务器端工作。我们的ui测试几乎在构建服务器上执行。它在我通过VisualStudio手动测试的同一台服务器上使用相同的部署 但在buildserver上执行ui测试时,我们偶尔会遇到以下问题: Test(s) failed. OpenQA.Selenium.Web
Test(s) failed. OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7056/hub/session/bed1d0e7-efdc-46b6-ba07-34903519c44d/element/%7B8717bb19-96c7-44d3-b0ee-d4b989ae652d%7D/click timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
--WebException
at System.Net.HttpWebRequest.GetResponse()
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
基本上,测试点击了一个上传按钮,在这个按钮上,输入字段之前填充了一个文件。由于文件非常小,因此只需几秒钟即可完成。
然而,有时会达到60秒的时限
有没有办法隔离潜在的问题?或者以前有人遇到过同样的问题?
任何提示,不胜感激。谢谢。就我而言,页面根本没有完全加载。某些facebook插件似乎加载时间过长。我尝试捕获异常并操纵未完成的dom,但这没有给我任何结果:(
John我们在项目中遇到了类似的问题。该问题与Selenium或我们的应用程序无关。它正在超时,因为该项目的生成服务器配置应该在5分钟内超时。但我们的所有测试都没有在5分钟内完成,因此生成因随机超时问题而失败 我们的firefox-19也有问题,测试通常会随机失败。不知何故,firefox-10只适用于我们的selenium测试。请尝试以下代码:
DesiredCapabilities caps = DesiredCapabilities.Firefox();
//set the timeout to 120 seconds
IWebDriver driver = new RemoteWebDriver(new Uri("<app_url>"), caps, TimeSpan.FromSeconds(120));
DesiredCapabilities=DesiredCapabilities.Firefox();
//将超时设置为120秒
IWebDriver-driver=新的RemoteWebDriver(新Uri(“”),大写,TimeSpan.FromSeconds(120));
我遇到了同样的错误:.NET WebDriver:2.37,FF:25.0.1。我注意到Firefox一直处于锁定状态,直到我退出测试应用程序,所以我构建了Firefox的调试版本,发现锁定发生在它写入stderr时。这为我提供了更改WebDriver代码的线索,使其不再重定向standard out和error解决了我的问题。似乎WebDriver以某种方式阻止了std错误。来自MSDN:
同步读取操作在调用方之间引入了依赖关系
从StandardError流读取和子进程写入
这些依赖关系可能导致死锁情况
更多信息
对于任何想做同样调整的人,我做了:-
RedirectStandardError=true
,请更改为RedirectStandardError=false
ii.无论您在哪里找到RedirectStandardOutput=true
,请更改为RedirectStandardOutput=false
(对于非Windows,Executable.cs中也有一个)
iii.在ConsoleOutput中,将“return this.stream.ReadToEnd()”更改为“return”“”"No longer redirecting console output for Firefox in .NET bindings."
这是我在这里使用的解决方法。因此,这一切都是有意义的。在四种不同的场景中发生在我身上:
新的FirefoxDriver(“FfBinaryPath”,FfProfileInstance,TimeSpan.FromMinutes(3));
public static IWebElement WaitForElementVisible(By selector, uint timeout = Config.DefaultTimeoutSec)
{
IWebDriver driver = Browser.Instance.Driver;
if (timeout > 0)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
wait.Until(ExpectedConditions.ElementIsVisible(selector));
return driver.FindElement(selector);
}
else
{
// Search for element without timeout
return driver.FindElement(selector);
}
}
public InternetExplorerDriver(
string internetExplorerDriverServerDirectory,
InternetExplorerOptions options,
TimeSpan commandTimeout
)
InternetExplorerOptions options = new InternetExplorerOptions();
IWebDriver driver = new InternetExplorerDriver("C:/Users/jeff/AppData/Local/Microsoft/WindowsApps", options, TimeSpan.FromSeconds(120));
public void VerifyPageIsLoaded()
{
var pageLoaded = false;
for (var i = 0; i < DefaultTimeout.Timeout.Seconds; i++)
{
Thread.Sleep(1000);
if (WebDriver.ExecuteJavaScript<string>("return document.readyState").Equals("complete"))
//jQuery.active might cause problems on some browser or browserstack so I commented it out
//&& WebDriver.ExecuteJavaScript<bool>("return jQuery.active == 0").Equals(true))
{
pageLoaded = true;
break;
}
Thread.Sleep(1000);
}
if (!pageLoaded)
{
throw new Exception("Page was not with complete state)!");
}
}