[英]Selenium C# Maximum time to wait for Timeout Exception
Selenium C#抛出超时异常之前等待的最大显式超时是多少?
有时我们正在测试的应用程序变得非常慢,最多需要4分钟才能加载。我想添加一个等待时间,这样它最多可以等待5分钟。
我已经尝试过此代码
WebDriverWait wait1 = new WebDriverWait(WebDriver, TimeSpan.FromMinutes(5));
wait1.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("returnjQuery.active==0"));
但是它会在2分钟左右引发超时异常。
Webdriver有隐式和显式等待的方式,但是当页面加载时间太长时,它将不会有用。 另外,当流程中发生异常或错误时,尽管页面已经加载,但在剩余的时间段内没有任何变化,我们最终不必要地等待了“指定的”时间。
Webdriver API的局限性之一是不支持开箱即用的WaitForPageLoad。 但是我们可以使用WebDriverWait类和DOM的readyState属性来实现。
WebDriverWait
可以等待元素。 我担心WebDriverWait
无法直接在JavaScriptExecutor
。 您需要处理以下内容
您可以等到文档处于就绪状态。
string state = string.Empty;
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
完整的代码如下
public void WaitForPageLoad(int maxWaitTimeInSeconds) {
string state = string.Empty;
try {
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(maxWaitTimeInSeconds));
//Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns ture
wait.Until(d = > {
try {
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
} catch (InvalidOperationException) {
//Ignore
} catch (NoSuchWindowException) {
//when popup is closed, switch to last windows
_driver.SwitchTo().Window(_driver.WindowHandles.Last());
}
//In IE7 there are chances we may get state as loaded instead of complete
return (state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase));
});
} catch (TimeoutException) {
//sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
throw;
} catch (NullReferenceException) {
//sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
throw;
} catch (WebDriverException) {
if (_driver.WindowHandles.Count == 1) {
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
}
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
if (!(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)))
throw;
}
}
资源 :-
https://automationoverflow.wordpress.com/2013/07/27/waiting-for-page-load-to-complete/
请参阅以下内容
通过JS事件加载新页面时,如何使Selenium WebDriver等待页面加载
希望它能对您有所帮助:)
如果通过页面加载时正在使用ExplicitWait
即WebDriverWait
则直接回答:
WebDriverWait wait1 = new WebDriverWait(WebDriver, TimeSpan.FromMinutes(5));
wait1.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("returnjQuery.active==0"));
IMO,这是开销。
值得一提的是,一旦脚本开始加载url,默认情况下浏览器客户端就会返回document.readyState === "complete"
。 然后,仅执行下一行代码。
现在让我具体一点。 使用Selenium,默认情况下,实现了三种(三种)超时类型,如下所示:
session script timeout
:指定等待脚本运行的时间。 如果等于null,则会话脚本超时将是不确定的。 否则为30,000毫秒。 session page load timeout
:指定等待页面加载完成的时间。 除非另有说明,否则为300,000毫秒。 [ document.readyState === "complete"
] session implicit wait timeout
:指定当取回元素时以及在执行元素交互时等待元素变得可交互时等待元素定位策略的时间(以毫秒为单位)。 除非另有说明,否则为零毫秒。 如果与元素(称为jQuery
elementA)进行交互之后,您需要等待jQuery
完成才能使另一个元素可交互(elementB),则您提到的功能很合适。
当您寻找加载URL时5分钟后超时的解决方案时,默认情况下已通过session page load timeout
(值300,000 milliseconds
或5
分钟)实现了该解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.