Passing state between pages when using geb & spock

In the example below (taken from Book of Geb ), we press a button that brings us to another page.

class GoogleHomePage extends Page {
    static url = "http://google.com"
    static at = { 
        title == "Google" 
    }
    static content = {
        searchField { $("input[name=q]") }
        searchButton(to: GoogleResultsPage) { $("input[value='Google Search']") }
    }
}

Browser.drive(GoogleHomePage) {
    searchField.value("Chuck Norris")
    searchButton.click()
    assert at(GoogleResultsPage)
    assert resultLink(0).text() ==~ /Chuck/
}

How can we pass state when we go to another page? for example, the user has selected this language, on the next page I expect the page to be in that language. A more general example:

import geb.*
import grails.plugin.geb.GebSpec

class GoogleHomePage extends Page {
   static url = "http://google.com"
   static at = { title == "Google" }
   static content = {
       searchField { $("input[name=q]") }
       searchButton(to: GoogleResultsPage, searchTerm:searchField.value()) { $("input[value='Google Search']") }
   }
}

class GoogleResultsPage extends Page {

  def searchTerm

  static at = {
    title == "${searchTerm} - Google Search"
  }
}

class MainFunctionalSpec extends GebSpec {

 def "Google search"() {
   when:
   to GoogleHomePage

   then:
   searchField.value("Chuck Norris")
   searchButton.click()
   assert at(GoogleResultsPage)
 }
}

This code has 2 problems, I get "There is no such property: searchField for class: GoogleHomePage" on searchButton.click () when I try to populate searchTerm. Even if I hardcode what I get, GoogleResultsPage.searchTerm is null and at assert doesn't work. Any ideas?

+3
source share
1

0,4. . , , . .

, 0,5:

http://bamboo.ci.codehaus.org/browse/GEB-MASTERDEFAULTS/latest/artifact/Manual/pages.html#lifecycle_hooks

class GoogleHomePage extends Page {
    static url = "http://google.com"
    static at = { title == "Google" }
    static content = {
        searchField { $("input[name=q]") }
        searchButton(to: GoogleResultsPage, searchTerm:searchField.value()) { $("input[value='Google Search']") }
    }

    def onUnload(GoogleResultsPage nextPage) {
        nextPage.searchTerm = searchField.value()
    }
}

class GoogleResultsPage extends Page {
    def searchTerm
    static at = {
        title == "${searchTerm} - Google Search"
    }
}

0.5-SNAPSHOT .

+3

Source: https://habr.com/ru/post/1762953/


All Articles