Thursday, September 6, 2012

Selenium WebDriver utility for determining when page has finished rendering

We are using Selenium for automated testing of our web app.   We use the new Selenium WebDriver API to create java based unit tests for interfacing with the UI.  One problem we had was determining when the page had completely finished rendering as the page may still be in the process up updating after the initial page is loaded.   There is an ADF javascript function for checking this
the helper method below can be called by the test class to determine when the page is fully synchronized with the server and the next action can be performed.



public static void waitForPageToFinishRendering(WebDriver oDriver, int timeout) {
        ExpectedCondition e = new ExpectedCondition() {
            public Boolean apply(WebDriver d) {
              JavascriptExecutor js = (JavascriptExecutor) d;
              Boolean isReady = (Boolean)js.executeScript("return AdfPage.PAGE.isSynchronizedWithServer()");
              return isReady;
            }
          };
          WebDriverWait w = new WebDriverWait(oDriver,
timeout,100);
          w.until(e);
    }


For a Good overview of using Selenium with ADF see 
http://www.oracle.com/technetwork/articles/adf/part6-094560.html
That article talks about using waitForElementPresent before performing the next action but I have found using the technique above to be more reliable.  Especially for complex pages where there is a lot of lazy loading going on.

11 comments:

  1. Good article....

    http://www.evalleypoint.com/

    ReplyDelete
  2. Great tip, thanks very much! Works way better than the kludgy alternative methods, and seems to work well consistently.

    ReplyDelete
  3. Your welcome! glad it worked for you

    ReplyDelete
  4. I'm not sure what it is your asking but the
    API docs can be found at http://docs.oracle.com/cd/E23549_01/apirefs.1111/e12046/index-all-I.html

    ReplyDelete
  5. We were new to selenium web driver.plz let me know the best article for beginners

    ReplyDelete
    Replies
    1. There are several on the project site. Start with "getting started"
      http://code.google.com/p/selenium/

      Delete
  6. Hi Don,

    What should I do if were facing an exception while executing the javascript to check that partial rendering of the page.

    ReplyDelete
  7. You can wrap the statement in a try/catch and return something in the catch statement. Like..
    try (return AdfPage.PAGE.isSynchronizedWithServer();}
    catch(err){ return false;}

    Or return a message and handle it differently as if you landed on a non ADF page this would loop forever since there would be no AdfPage object.

    ReplyDelete
  8. I have figured it out to one more low level,

    When ever I tried to check this method IsSynchronizedwithServer when the ADF page is having a protected element like TaxFileNumber/Password , the exception occurs else case it is just returning false or true.

    I have verified and the ADFpage object is defined for that page.

    The exception stacktrace of javascript executer says _textFieldField is null.

    How can we know what exactly isSyncronizedWithserver() method consists and looks for in a ADF page.more likely I wish to see the code.

    Thanks in advance and also for an immediate response.I very glad that you were following our comments after this long time.

    ReplyDelete
  9. I'm no longer working on an ADF project so don't have time to investigate. You can always set a breakpoint in firebug or other javascript debugger and step through the isSyncronizedWithServer method but with the minimized function names and lack of documentation I haven't found that to be too helpful. If it gets that exception does it work if you re-call the method until it succeeds or does it continue to fail?

    ReplyDelete