Browser waitFor method doesn't seem to be working as expected

358 views
Skip to first unread message

Manny Rodriguez

unread,
May 24, 2016, 11:17:58 AM5/24/16
to Geb User Mailing List
Hello, Geb 0.13.1 here. I have read over the docs and am having difficulty getting the waitFor method to work for me:

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')

            waitFor {
                searchButton.click()
            }

            at(GoogleSearchResultsPage)

            quit()
        }
    }
}

class GoogleHomepage extends Page {
    static url = 'http://google.com'

    static at = {
        title == 'Google'
    }

    static content = {
        search {
            $('input', name: 'q')
        }

        searchButton {
            $('button', name: 'btnG')
        }
    }
}

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

When I run this I get the following exception:

    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)
      at sandbox.geb.GoogleSearchResultsPage$__clinit__closure1.doCall(GoogleSearchResultsPage.groovy:10)
      at sandbox.geb.GoogleSearchResultsPage$__clinit__closure1.doCall(GoogleSearchResultsPage.groovy)

So Geb is:
  1. Launching Firefox (yaaay)
  2. Going to Google's homepage
  3. Entering "gmail" in the search bar and clicking enter (to perform the search)
  4. But it is not properly waiting for that click to finish, as the "at checker" for GoogleSearchResultsPage is failing because Geb still thinks the browser is at the Google homepage
Interestingly enough, when I replace:

    search().value('gmail')

    waitFor {
        searchButton.click()
    }

    at(GoogleSearchResultsPage)

For:

    search().value('gmail')

    searchButton.click()

    Thread.sleep(5000)

    at(GoogleSearchResultsPage)

The code runs just fine.  However, I don't want to use thread sleeps, and would prefer to use waitFor (or any other Geb-endorsed solution) instead. Any ideas as to what is going on here?

Brian Kotek

unread,
May 24, 2016, 12:06:13 PM5/24/16
to Geb User Mailing List
waitFor is used to test for a condition that eventually becomes true. So you don't wait for a click(). You do the click() and wait for something that will change as a result of the click. http://www.gebish.org/manual/current/#waiting

So in this case, you don't use waitFor on your click(). You need to wait for the at check to become true.

Try:

searchButton.click()
waitFor { at GoogleSearchResultsPage }


Or, you could wait in the Page's at verifier: 

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


--
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/56781c2e-bc1c-4aac-b1e8-ba0298f483a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manny Rodriguez

unread,
May 24, 2016, 12:26:29 PM5/24/16
to Geb User Mailing List
Ahhh, thank you so much @brian428, works beautifully!
Reply all
Reply to author
Forward
0 new messages