Search code examples
user-interfacegroovyspockfunctional-testinggeb

Switching use of page objects in test - Geb Groovy Spock


I'm writing UI functional tests with Spock, Groovy and Geb implementing the page object pattern. During my flow of events im navigating away from the current page to get a result and as a result, i need to switch the page object in my test but havent been able to do so successfully

Test case below:

    def "Navigate to Second Page"() {
    when: "I navigate to second page"

    redirctButton.click()

    then: "Second Page Url should show"
    browser.getCurrentUrl() == secondpageUrl
}

def "Use method form second page"() {
    when: "Im on second page"
    SecondPage.performSearch("search")

    then: "result should show"
    SecondPage.resultBox == ""
}

Solution

  • You should add at-checking for your page objects, then you can use the at method to verify that you are on the expected page, making browser.getCurrentUrl() == secondpageUrl obsolete. The other effect of the at-check is that it changes the current page and also returns the page object for strongly typed access. If you don't care about strongly typed access you can remove the expect block in the second test, it is only there to give you access to typed page object.

    
    @Stepwise
    class PageTest extends GebReportingSpec {
    
    def "Navigate to Second Page"() {
        when: "I navigate to second page"
        redirctButton.click()
    
        then: "Second Page Url should show"
        at SecondPage
    }
    
    def "Use method form second page"() {
        expect:
        def secondPage = at SecondPage
    
        when: "Im on second page"
        secondPage.performSearch("search")
    
        then: "result should show"
        secondPage.resultBox == ""
    }
    }