简体   繁体   English

Selenium C#等待超时异常的最长时间

[英]Selenium C# Maximum time to wait for Timeout Exception

What is the maximum explicit timeout that Selenium C# waits before it throws timeout exception? Selenium C#抛出超时异常之前等待的最大显式超时是多少?

Sometimes the application which we are testing becomes very slow and takes up to 4 mins to load .I want to add a wait time, so that it will wait a maximum upto 5 mins. 有时我们正在测试的应用程序变得非常慢,最多需要4分钟才能加载。我想添加一个等待时间,这样它最多可以等待5分钟。

I have tried with this code 我已经尝试过此代码

WebDriverWait wait1 = new WebDriverWait(WebDriver, TimeSpan.FromMinutes(5));

wait1.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("returnjQuery.active==0"));

But it throws timeout exception around 2 mins. 但是它会在2分钟左右引发超时异常。

Webdriver has ways for implict and exlict wait but that wont be useful when page is taking too long to load. Webdriver有隐式和显式等待的方式,但是当页面加载时间太长时,它将不会有用。 Also, when an exception or error is occured in the flow, we end up waiting unnecessarily for “specified” time though page has already loaded and nothing is going to change in the remaining time period. 另外,当流程中发生异常或错误时,尽管页面已经加载,但在剩余的时间段内没有任何变化,我们最终不必要地等待了“指定的”时间。

One of the limitation of Webdriver API is no support for WaitForPageLoad out of the box. Webdriver API的局限性之一是不支持开箱即用的WaitForPageLoad。 But we can implement that using WebDriverWait class and readyState property of DOM. 但是我们可以使用WebDriverWait类和DOM的readyState属性来实现。

WebDriverWait can wait for element. WebDriverWait可以等待元素。 I afraid that WebDriverWait won't work on JavaScriptExecutor directly. 我担心WebDriverWait无法直接在JavaScriptExecutor you need to handle something like below 您需要处理以下内容

You can wait till document to be in ready state. 您可以等到文档处于就绪状态。

 string state = string.Empty;
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();

The full code be like below 完整的代码如下

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;
    }
}

Source :- 资源 :-

https://automationoverflow.wordpress.com/2013/07/27/waiting-for-page-load-to-complete/ https://automationoverflow.wordpress.com/2013/07/27/waiting-for-page-load-to-complete/

Refer below for 请参阅以下内容

How to make Selenium WebDriver wait for page to load when new page is loaded via JS event 通过JS事件加载新页面时,如何使Selenium WebDriver等待页面加载

Hope it will help you :) 希望它能对您有所帮助:)

Answering straight if you are using ExplicitWait ie WebDriverWait while the page gets loaded through: 如果通过页面加载时正在使用ExplicitWaitWebDriverWait则直接回答:

WebDriverWait wait1 = new WebDriverWait(WebDriver, TimeSpan.FromMinutes(5));

wait1.Until(x => (bool)((IJavaScriptExecutor)x).ExecuteScript("returnjQuery.active==0"));

IMO, it's a overhead. IMO,这是开销。

It is worth to mention that once your script starts loading an url, by default the browser client returns document.readyState === "complete" . 值得一提的是,一旦脚本开始加载url,默认情况下浏览器客户端就会返回document.readyState === "complete" Then only your next line of code gets executed. 然后,仅执行下一行代码。

Page Loading: 页面加载:

Now let me get a bit specific now. 现在让我具体一点。 Using Selenium, by default 3 (three) types of timeouts are implemented as follows: 使用Selenium,默认情况下,实现了三种(三种)超时类型,如下所示:

  1. session script timeout : Specifies a time to wait for scripts to run. session script timeout :指定等待脚本运行的时间。 If equal to null then session script timeout will be indefinite. 如果等于null,则会话脚本超时将是不确定的。 Otherwise it is 30,000 milliseconds. 否则为30,000毫秒。
  2. session page load timeout : Specifies a time to wait for the page loading to complete. session page load timeout :指定等待页面加载完成的时间。 Unless stated otherwise it is 300,000 milliseconds. 除非另有说明,否则为300,000毫秒。 [ document.readyState === "complete" ] [ document.readyState === "complete" ]
  3. session implicit wait timeout : Specifies a time to wait in milliseconds for the element location strategy when retreiving elements and when waiting for an element to become interactable when performing element interaction . session implicit wait timeout :指定当取回元素时以及在执行元素交互时等待元素变得可交互时等待元素定位策略的时间(以毫秒为单位)。 Unless stated otherwise it is zero milliseconds. 除非另有说明,否则为零毫秒。

Element Loading: 元素加载:

In case after an interaction with an element (elementA, which calls a jQuery ) you need to wait for a jQuery to be completed for another element to be interactable (elementB), the function you mentioned fits the bill. 如果与元素(称为jQuery elementA)进行交互之后,您需要等待jQuery完成才能使另一个元素可交互(elementB),则您提到的功能很合适。

Conclusion: 结论:

As you are looking for a solution to timeout after 5 mins while loading the url, it is already implemented by default through session page load timeout with a value 300,000 milliseconds or 5 minutes. 当您寻找加载URL时5分钟后超时的解决方案时,默认情况下已通过session page load timeout (值300,000 milliseconds5分钟)实现了该解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM