How to make Geb code reactive/responsive and not use Thread.sleep

66 views
Skip to first unread message

Manny Rodriguez

unread,
May 23, 2016, 2:21:45 PM5/23/16
to Geb User Mailing List
I have the following Groovy code that uses Geb (0.13.1):

class LaunchGeb {
    static void main(String[] args) {
        FirefoxDriver firefoxDriver = new FirefoxDriver()
        Browser browser = new Browser(driver: firefoxDriver)

        Browser.drive(browser) {
            to(GoogleHomepage)
            at(GoogleHomepage)

            search().value('gmail')
            searchButton.click()
            at(GoogleSearchResultsPage)

            quit()
        }
    }
}

class GoogleSearchResultsPage extends Page {
    static at = {
        title == 'gmail - Google Search'
    }
}

When I run this it throws an exception at the "at checker" for GoogleSearchResultsPage because it claims the page title is still "Google":

Exception in thread "main" Assertion failed: 

    title == 'gmail - Google Search'
    |     |
    |     false
    Google
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:402)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)

So on a hunch, I sprinkled in a Thread.sleep in between the time I click the searchButton and the time I run the at checker:

    search().value('gmail')
    searchButton.click()

    Thread.sleep(5000)

    at(GoogleSearchResultsPage)

When I make this change, the code now runs perfectly (no exceptions).

So I ask: is it typical/standard for Geb code to add pauses/sleeps into its execution so that we can wait for browsers to finish loading pages or resolving resources/backend calls? Using Thread.sleep is generally regarded as an anti-pattern and I guess I'm wondering if I can make my Geb code my reactive/responsive here. Ideas?

Brian Kotek

unread,
May 23, 2016, 3:00:44 PM5/23/16
to Geb User Mailing List
Manny, I'm still not sure where you're getting this code from, but I think you'd have a much easier time if you used one of the current examples




--
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/f9c884ab-e646-4188-a95c-402385f6f006%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manny Rodriguez

unread,
May 23, 2016, 3:23:45 PM5/23/16
to Geb User Mailing List
Thanks @brian428 - I'm not "getting" this code from anywhere, I'm simply writing it! I'm reading the Geb docs and playing around with Geb on my own, as it makes sense for my particular use case. Also, you keep recommended that I use examples provided in the docs/Book of Geb. In every case, I have, and now I'm braving out onto my own and am running into small obstacles; hence my questions here. But moving forward, you do not need to recommend that I read the docs, as I assure you, I have. I just might have missed some things here and there (like waitFor as you pointed out - thanks!) or perhaps there are use cases not covered in the docs (this is plausible too). Thanks again for you help thus far!

Brian Kotek

unread,
May 23, 2016, 6:16:24 PM5/23/16
to Geb User Mailing List
OK, I was just making sure that you're aware of the docs and the example project, because most (if not all) of these sorts of things are covered there. I simply didn't want you to be spinning your wheels unnecessarily.

Reply all
Reply to author
Forward
0 new messages