簡體   English   中英

Selenium C#等待超時異常的最長時間

[英]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等待頁面加載

希望它能對您有所幫助:)

如果通過頁面加載時正在使用ExplicitWaitWebDriverWait則直接回答:

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

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

IMO,這是開銷。

值得一提的是,一旦腳本開始加載url,默認情況下瀏覽器客戶端就會返回document.readyState === "complete" 然后,僅執行下一行代碼。

頁面加載:

現在讓我具體一點。 使用Selenium,默認情況下,實現了三種(三種)超時類型,如下所示:

  1. session script timeout :指定等待腳本運行的時間。 如果等於null,則會話腳本超時將是不確定的。 否則為30,000毫秒。
  2. session page load timeout :指定等待頁面加載完成的時間。 除非另有說明,否則為300,000毫秒。 [ document.readyState === "complete" ]
  3. session implicit wait timeout :指定當取回元素時以及在執行元素交互時等待元素變得可交互時等待元素定位策略的時間(以毫秒為單位)。 除非另有說明,否則為零毫秒。

元素加載:

如果與元素(稱為jQuery elementA)進行交互之后,您需要等待jQuery完成才能使另一個元素可交互(elementB),則您提到的功能很合適。

結論:

當您尋找加載URL時5分鍾后超時的解決方案時,默認情況下已通過session page load timeout (值300,000 milliseconds5分鍾)實現了該解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM