Proper way to wait for a page to load

3,315 views
Skip to first unread message

Nuno Santos

unread,
Oct 21, 2015, 7:34:21 AM10/21/15
to Geb User Mailing List
Hello everyone,

  Using the Page Pattern in Geb, and with the following scenario, what would be your hindsights on the navigation between pages?

  By using a waitFor statement on a Page, I want the waitFor to only end after the page has fully loaded.

  My current solution is to either look at the page title or the page id.

  The problem I have with this approach is that when running the tests in bulk, sometimes (randomly) an element is not displayed and the test fails right away, e.g. isDisplayed returns false.

  I believe one of the approaches would be to wait for that particular element to be displayed but since that element is just part of a larger form, is there a specific way that I can say the following: "wait for the whole DOM to load" and the exit the waitFor statement.

Thanks in advance for your replies,
Nuno Santos.

Kamil Lendo

unread,
Oct 22, 2015, 11:07:22 AM10/22/15
to Geb User Mailing List
Hi Nuno,

Sure there is!
What you can do is using a javascript call:

waitFor {js.('document.readyState') == 'complete'}

Additionally if your page is asynhronus, you can use something like this:

waitFor {js.('jQuery.active') == 0}

There is only one thing you should be wary of, what kind of ajax framework is used, but often it is jQuery.

Best Regards
Kamil

Marcin Erdmann

unread,
Oct 22, 2015, 5:23:16 PM10/22/15
to geb-...@googlegroups.com
Nuno,

Selenium (and thus Geb) loads the whole DOM after an url change and only then passes the control back to your code. By loading the whole DOM I mean getting the whole html returned from the requested url, all assets (css, js, images) defined by that html and executing all synchronous (!!!) javascript. If there is any asynchronous javascript (for example ajax calls) happening on page load then that will not be waited for.

So you need to answer yourself what exactly do you mean by "wait for the whole DOM to load". Are you using async js calls on page load? Or some framework like for example Angular that does it under the covers? Also, are you sure that the failures you are seeing are down to some async code happening on page load or maybe it's down to async code triggered by an action performed by the test like for example clicking a button that triggers an ajax call?

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/7009d4c4-c8c0-4f98-a0bc-0ff0edc22b9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages