在使用geb&时,在页面之间传递状态斯波克 [英] Passing state between pages when using geb & spock
问题描述
在下面的例子中(摘自 Geb Book ),我们点击一个按钮,将我们带到另一个页面。
In the example below (Taken from the Book of Geb), we're clicking on a button that takes 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 going to another page? eg The user has selected this language, in the next page, I expect the page to be in that language. A more generic 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)
}
}
这段代码有两个问题, 没有这样的属性:searchField for class:G oogleHomePage在searchButton.click()时试图填充searchTerm。即使我对传递的内容进行了硬编码,GoogleResultsPage.searchTerm也是null,并且at声明失败。有任何想法吗?
This code has 2 problems, I get a "No such property: searchField for class: GoogleHomePage" on searchButton.click() when trying to populate searchTerm. Even if I hardcode what gets passed, GoogleResultsPage.searchTerm is null and the at assert fails. Any ideas?
推荐答案
这对于0.4来说并不可行。模板定义的映射选项成为该模板的选项。更重要的是,内容模板是在静态上下文中定义的,但是它们的闭包定义是在实例上下文中进行评估的。最终的结果是,从这一点无法访问实例状态。
This is not really possible with 0.4. The map options to template definitions become the options to that template. What is more, content templates are defined in a static context, but their closure definitions are evaluated in an instance context. The end result being that it's impossible to access instance state from that point.
解决方案是使用0.5中的新生命周期钩子:
The solution is to use the new lifecycle hooks in 0.5:
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"
}
}
可从codehaus快照资源库中下载最新的0.5-SNAPSHOT版本。
This is available in that latest downloadable 0.5-SNAPSHOT versions from the codehaus snapshot repository.
这篇关于在使用geb&时,在页面之间传递状态斯波克的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!