GEB - find and count all elements with specific word in text

Viewed 1042

So, I am pretty new to this language and I've got specific task to find and count all specific words withing tag in search page in google. So i managed to open, and find it but I can't find any way to move futher. My code:

class GoogleUiSpec extends GebSpec {
    def "checking for word"() {
        given: " Search for word 'ebay' in google"
        go "https://www.google.pl/"

        $("body").find("input", name: "q").value("ebay")
        $("center").$("input", 0, name: "btnK").click()
        waitFor { title.endsWith(" Szukaj w Google")}

        $("h3").findAll{ it.has("ebay")}
    }
}

This run smoothly but I am almost sure that it's wrong and I have no idea how to move on to count these elements. Thanks for any help.

2 Answers

You are pretty close! You can do the following to retrieve the count where h3 contains the word "ebay" and assert the correct number appear:

def "checking for word"() {
    given: " Search for word 'ebay' in google"

    go "https://www.google.pl/"

    $("body").find("input", name: "q").value("ebay")
    $("center").$("input", 0, name: "btnK").click()
    waitFor { title.endsWith(" Szukaj w Google")}

    then: "Correct results are show"

    $("h3").count { it.text().toLowerCase().contains("ebay") } == 10
}

Note the toLowerCase() as most results return as "eBay" and won't match "ebay".

I'd recommending looking into page objects, and creating a GoogleHomePage and GoogleResultsPage something similar to:

import geb.Page

class GoogleHomePage extends Page {

    static url = "http://www.google.com"

    static at = {
        logo.displayed
    }

    static content = {
        logo { $("#hplogo") }
        searchField { $("body").find("input", name: "q") }
        searchButton { $("center").$("input", 0, name: "btnK") }
    }

    ResultsPage searchFor(String search) {
        searchField.value(search)
        searchButton.click()

        browser.at(ResultsPage)
    }
}

Results page:

import geb.Page

class ResultsPage extends Page {

    static at = { title.endsWith(" Szukaj w Google") }

    static content = {

        results { $("h3") }
    }

    def countResultsContaining(String expectedResultPhrase) {
        results.count { it.text().toLowerCase().contains(expectedResultPhrase) }
    }
}

Then your test ends up looking a lot cleaner without all the selectors etc, and you have some reusable code for other tests:

class GoogleSpec extends GebReportingSpec {

    def "checking for word"() {
        given: " Search for word 'ebay' in google"

        def searchPhrase = "ebay"
        def googlePage = to GoogleHomePage

        when: "I search for ebay"

        def resultsPage = googlePage.searchFor(searchPhrase)

        then: "Correct results are shown"

        resultsPage.countResultsContaining(searchPhrase) == 10
    }
}

As for resources, the Geb Manual is good, but Geb is written in Groovy - so searching for how to do it using Groovy rather than Geb will aid you.

Thanks for the answers, they also work but i managed to do it another way so I am posting it here. The line which do the trick:

println $(By.className("LC20lb")).findAll {it.text().contains("ebay")}.size()
Related