Search code examples
groovyspockgeb

Using multiple page objects in one Geb test


I'm trying to write a basic log in geb test using spock. I have created 2 page objects, one for the login page, and another for the page you are taken to after login.

Login page

package Pages

import geb.Page

class loginPage extends Page {
    static url = 'login/'
    static at = {title == "Login to TalentBank"}
    static content = {
        logo {$(".center-img img")}
        emailHeader {$(".form-group label", text:"Email")}
        emailTextBox {$('#email')}
        pwdHeader {$(".form-group label", text:"Password")}
        pwdTextBox {$("#password")}
        loginButton {$("#loginButton")}
    }
}

Home page

package Pages

import geb.Page

class homePage extends Page {
    static at = {title == "Home"}
    static content = {
        tile1 {$("#page-container > div.container-fluid > div > div:nth-child(2) > div")}
    }
}

TestSpec. This is a basic test to go to the login page, enter user credentials, click the login button, wait for an element on the home page, then verify you are on the home page.

import Pages.loginPage
import Pages.homePage
import geb.spock.GebReportingSpec


class loginPageSpec extends GebReportingSpec {


    def "Log in to TalentBank Core"(){
        given:
        to loginPage
        waitFor {loginButton.isDisplayed()}

        when:
        emailTextBox.value("Ruxin")
        pwdTextBox.value("Test1234")
        loginButton.click()

        then:
        waitFor {tile1.isDisplayed()}
        at homePage
    }
}

When I run the test, I get the below error

Caused by: groovy.lang.MissingPropertyException: Unable to resolve tile1 as content for Pages.loginPage, or as a property on its Navigator context. Is tile1 a class you forgot to import?

Its looking for tile1 in loginPage instead of homePage.


Solution

  • Change the at in location in the test, I would also add page reference, you'll benefit from the autocomplete.

    Login page

    package Pages
    
    import geb.Page
    
    class LoginPage extends Page {
    
        static url = 'login/'
    
        static at = {
               title == "Login to TalentBank"
        }
    
        static content = {
            logo         {$(".center-img img")}
            emailHeader  {$(".form-group label", text:"Email")}
            emailTextBox {$('#email')}
            pwdHeader    {$(".form-group label", text:"Password")}
            pwdTextBox   {$("#password")}
            loginButton  {$("#loginButton")}
        }
    }
    

    Home page

    package Pages
    
    import geb.Page
    
    class HomePage extends Page {
    
        static at = {
               waitFor {title == "Home"} // Add waitFor here to verify on page
        }
    
        static content = {
            tile1 {$("#page-container > div.container-fluid > div > div:nth-child(2) > div")}
        }
    }
    

    TestSpec:

    import Pages.LoginPage
    import Pages.HomePage
    import geb.spock.GebReportingSpec
    
    
    class LoginPageSpec extends GebReportingSpec {
    
        def "Log in to TalentBank Core"(){
            given:
            Page loginPage = to LoginPage
            waitFor {loginPage.loginButton.isDisplayed()}
    
            when:
            loginPage.emailTextBox.value("Ruxin")
            loginPage.pwdTextBox.value("Test1234")
    
            and: "Click login"
            loginPage.loginButton.click()
    
            then: "Check at home page"
            Page homePage = at HomePage
    
            and:
            waitFor {homePage.tile1.isDisplayed()}
        }
    }