Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get selenium to recognize that a page loaded?

Tags:

java

selenium

In certain unknown situations selenium does not detect that a page has loaded when using the open method. I am using the Java API. For example (This code will not produce this error. I don't know of an externally visible page that will.):

Selenium browser = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
browser.start();
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");

When the error occurs, the call to 'open' times out, even though you can clearly see that the page has loaded successfully before the timeout occurs. Increasing the timeout does not help. The call to 'type' never occurs, no progress is made.

How do you get selenium to recognize that the page has loaded when this error occurs?

like image 788
Matthew Jaskula Avatar asked Sep 17 '08 22:09

Matthew Jaskula


People also ask

How would you make sure that a page is loaded using Selenium?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.

How will you identify the page got loaded properly?

If their lenght is equal the htmls should be equal and that means the page is fully loaded.

How does Selenium verify page content?

We can check if some text exists or not in a page with Selenium. There are more than one ways to find it. We can use the getPageSource() method to fetch the full page source and then verify if the text exists there. This method returns content in the form of string.


2 Answers

I faced this problem quite recently.

All JS-based solutions didn't quite fit ICEFaces 2.x + Selenium 2.x/Webdriver combination I have.

What I did and what worked for me is the following:

In the corner of the screen, there's connection activity indicator.

            <ice:outputConnectionStatus id="connectStat"
                                        showPopupOnDisconnect="true"/>

In my Java unit test, I wait until its 'idle' image comes back again:

private void waitForAjax() throws InterruptedException {
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { 
            if ("visibility: visible;".equals(
                selenium.getAttribute("top_right_form:connectStat:connection-idle@style"))) { 
                break;
            }
        } catch (Exception e) {

        }
        Thread.sleep(1000);
    }
}

You can disable rendering of this indicator in production build, if showing it at the page is unnecessary, or use empty 1x1 gifs as its images.

Works 100% (with popups, pushed messages etc.) and relieves you from the hell of specifying waitForElement(...) for each element separately.

Hope this helps someone.

like image 186
Timur Evdokimov Avatar answered Nov 02 '22 01:11

Timur Evdokimov


Maybe this will help you....

Consider the following method is in page called Functions.java

public static void waitForPageLoaded(WebDriver driver) {

         ExpectedCondition<Boolean> expectation = new
    ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
              return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
          };

          WebDriverWait wait = new WebDriverWait(driver,30);
          try {
                  wait.until(expectation);
          } catch(Throwable error) {
                  Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
          }
     } 

And you can call this method into your function. Since it is a static method, you can directly call with the class name.

public class Test(){
    WebDriver driver;

    @Test
    public void testing(){
         driver = new FirefoxDriver();
         driver.get("http://www.gmail.com");
         Functions.waitForPageLoaded(driver);
   }
}
like image 25
Prabu Ananthakrishnan Avatar answered Nov 02 '22 02:11

Prabu Ananthakrishnan