How I can check whether a page is loaded completely or not in web driver?

前端 未结 9 1847
走了就别回头了
走了就别回头了 2020-12-01 01:33

I am writing some Java Webdriver code to automate my application. How can I correctly check whether the page has been loaded or not? The application has some Ajax calls, too

9条回答
  •  执念已碎
    2020-12-01 02:09

    You can set a JavaScript variable in your WepPage that gets set once it's been loaded. You could put it anywhere, but if you're using jQuery, $(document).onReady isn't a bad place to start. If not, then you can put it in a

    And in your test (C# example):

    // The timespan determines how long to wait for any 'condition' to return a value
    // If it is exceeded an exception is thrown.
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5.0));
    
    // Set the 'condition' as an anonymous function returning a boolean
    wait.Until(delegate(IWebDriver d)
    {
        // Check if our global variable is initialized by running a little JS
        return (Boolean)((IJavaScriptExecutor)d).ExecuteScript("return typeof(window.TestReady) !== 'undefined' && window.TestReady === true");
    });
    

提交回复
热议问题