Home > Net >  How to navigate to same page before running any test script in GEB SPOCK
How to navigate to same page before running any test script in GEB SPOCK

Time:03-24

I am creating one automation script using geb spock and groovy. In my test class I have multiple tests and I want to navigate to home page before running any of the test meaning

  1. login to application and navigate to home page (default page after login is home page).
  2. Click on Link1. Check Page.navigate to home page
  3. Click on Link2. Check Page . navigate to home page
  4. Click on Link3. Check Page. navigate to home page...

I created a method and try to reuse it in my geb test but getting the following error:

geb.error.PageInstanceNotInitializedException: Instance of page class pages.Test_HomePage has not been initialized. Please pass it to Browser.to(), Browser.via(), Browser.page() or Browser.at() before using it.
at geb.Page.uninitializedException(Page.groovy:521)
        at geb.content.UninitializedPageContentSupport.getContent(UninitializedPageContentSupport.groovy:30)
        at geb.content.PageContentSupport.propertyMissing(PageContentSupport.groovy:39)
        at geb.Page.propertyMissing(Page.groovy:99)
        at pages.Test_HomePage.clickOnHomePage(Test_HomePage.groovy:46)
        at com.abc.vcctest.TestNavigationInitialTestSpec.Navigate to Home page of test portal(TestNavigationInitialTestSpec.groovy:44)

Here is my sample geb test:

@TestMixin(GrailsUnitTestMixin)
    class TestNavigationInitialTestSpec extends LoginBaseTestSpec {
        @Shared
        Test_HomePage test_HomePage = new Test_HomePage()
        def cleanupSpec() {
            browser.close()
        }
        def setup() {
        }
        def cleanup() {
        }
        def navigateToHomePage() {
            Test_HomePage test_HomePage = at Test_HomePage
            test_HomePage.clickOnHomePage()
            at Test_HomePage
        }
        def "Navigate to Home page of test portal"() {
            given:
            HomePage homePage = at HomePage

            when: "Click on Home tab/link"
            navigateToHomePage()
            test_HomePage.clickOnHomePage()
            def crashDisplayed = test_HomePage.crashPageDisplayed()

            then: "You are on Home Page"
            !crashDisplayed || { at Test_HomePage } 
        }

            def "Navigate to Change Password page of testportal"() {
                given:
                at Test_HomePage

                when: "Click on Change Password"
                test_HomePage.clickOnChangePassword()
                Test_ChangePassword test_ChangePassword = at Test_ChangePassword
                def crashDisplayed1 = test_ChangePassword.crashPageDisplayed()

                then: "You are on Change Password Page"
                !crashDisplayed1 || { at Test_ChangePassword }
                at Test_ChangePassword
               
            } 

This is my Test_HomePage class:

class Test_HomePage extends HomePage2{
      static url = '/home'
      static at = {
        waitFor(message:"The Test Portal title is missing."){driver.title == "Test Portal"}
      }
      static content = {
        pageCrash(required: false, cache: false) { $("h1") }
        homePageLink {
          $("a[href = '/Link1']")
        }
       changePasswordLink{
          $("a[href='/Link2']")
        }
        updateEmailLink{
          $ ("a[href = '/Link3']")
        }
      }
      void crashPageDisplayed() {
        if (pageCrash.isDisplayed()) {
          driver.navigate().back()
        }
      }
      void clickOnHomePage() {
        homePageLink.click()
      }
      void clickOnChangePassword(){
        changePasswordLink.click()
      }
      void clickUpdateEmail(){
        updateEmailLink.click()
      }
    }

I am new to geb and spock and I might be doing a very silly mistake but any help would be really appreciated or suggestion to implement this in an effective way.

CodePudding user response:

You should make you feature methods independent of each other, so they can be called in any order. Why don't you put something like to Test_HomePage into your setup() method? Then it will be called at the beginning of each feature method. Even if you do it in the given: block for each single feature method, you still need to before you can check at. I think you should study the Book of Geb a little bit. This is what the error message is trying to explain to you, by the way:

geb.error.PageInstanceNotInitializedException:
  Instance of page class pages.Test_HomePage has not been initialized.
  Please pass it to Browser.to(), Browser.via(), Browser.page() or Browser.at() before using it.

In navigateToHomePage(), your first command should therefore be:

Test_HomePage test_HomePage = to Test_HomePage
  • Related