hnaderi
hnaderi

Reputation: 409

Very first example on Geb manual not executing

I have the following setup:

Using just the GroovyConsole i tried to execute the very first example given in the Geb manual :

import geb.Browser

Browser.drive {
    go "http://google.com/ncr"

// make sure we actually got to the page
assert title == "Google"

// enter wikipedia into the search field
$("input", name: "q").value("wikipedia")

// wait for the change to results page to happen
// (google updates the page dynamically without a new request)
waitFor { title.endsWith("Google Search") }

// is the first link to wikipedia?
def firstLink = $("li.g", 0).find("a.l")
assert firstLink.text() == "Wikipedia"

// click the link 
firstLink.click()

// wait for Google's javascript to redirect to Wikipedia
waitFor { title == "Wikipedia" }
}

but am getting the following error:

WARNING: Sanitizing stacktrace:

geb.waiting.WaitTimeoutException: condition did not pass in 5.0 seconds

Is there something wrong w/ the example? Am I doing something incorrect? this is very frustrating seeing as how the VERY first example won't even run!

Upvotes: 2

Views: 5152

Answers (4)

Jorge
Jorge

Reputation: 11

None of the above worked for me. After some debugging I got the code working like this:

Browser.drive {

    go "http://google.com"

    // make sure we actually got to the page
    assert title == "Google"

    // enter wikipedia into the search field
    $("input", name: "q").value("wikipedia")

    // wait for the change to results page to happen
    // (google updates the page dynamically without a new request)
    waitFor { title.endsWith("Google Search") }

    // is the first link to wikipedia?
    def firstLink = $("li.g", 0).find("a")
    assert firstLink.text() == "Wikipedia, the free encyclopedia"

    // click the link
    firstLink.click()

    // wait for Google's javascript to redirect to Wikipedia
    waitFor { title == "Wikipedia, the free encyclopedia" }
}

Upvotes: 1

Brendan Prin
Brendan Prin

Reputation: 183

I had the same problem you are experiencing.

The first WaitFor issue:

The first wait for can be fixed by J. Levine's answer. Adding:

$("input", name: "btnG").click()

after:

$("input", name: "q").value("wikipedia").

The second WaitFor issue:

The title of the page that Wikipedia opens to from google is different from the Wikipedia Homepage. On the homepage it is <title>Wikipedia<title> on the main page(which google opens to is <title>Wikipedia, the free encyclopedia<title>.

So change:

waitFor { title == "Wikipedia" } }
To:
waitFor { title == "Wikipedia, the free encyclopedia" }
}

And that should fix the second wait issue

Upvotes: 2

majjinator
majjinator

Reputation: 31

The example makes use of the auto load feature in Google whereby search results are displayed as you type in the search, hence you don't need to click on the search button. When you run the test, you should see that the search results are displayed and the Wikipedia link is the first.

The WaitTimeoutException you are getting is most probably because the browser closes too quickly after arriving at the Wikipedia page. To fix that, just update the waitFor call to make it wait longer before closing the browser i.e.

waitFor(10) (at WikipediaPage)

Alternatively, if you run gradle in debug mode, the process is much slower and hence allows the test to check for the title before the browser is terminated

gradlew firefoxTest --debug --stacktrace

Upvotes: 2

J. Levine
J. Levine

Reputation: 68

The script is entering wikipedia into the Search box, but it's not hitting the Google Search button to kick off the search.

If you add:

// hit the "Google Search" button

$("input", name: "btnG").click()

right after

// enter wikipedia into the search field

$("input", name: "q").value("wikipedia")

you'll get a little farther.

Upvotes: 3

Related Questions