我一直在尝试使用Geb / Spock测试一些网络应用程序,但遇到了一个我似乎无法弄清楚的问题。
我有一个带有setupSpec()fixture方法的测试类,该方法调用AuthenticationSpec中定义的登录方法,然后是一堆在此页面上断言各种内容的其他方法:
class TestThisPage extends AuthenticationSpec {
def setupSpec() {
loginComplete("some user ID", "some password")
assert page instanceof ThisPage
}
def "Verify some link's text is correct"() {
when: "The user is at this page"
then: "The this specific link's text is correct"
assert someLink.text().equalsIgnoreCase("I am some text")
}
def "Verify that the correct user name is displayed"() {
when: "The user is signed in and at this page"
then: "The signed-in user's display name appears correctly on the page"
assert signInText.equalsIgnoreCase("Signed in as: some user ID")
}
def "Verify this page's copyright"() {
when: "The user is redirected to this page"
then: "The copyright is correct"
assert legalInfo.text().trim().equalsIgnoreCase("Some copyright text here")
}
}
现在,当我将TestThisPage作为测试套件运行时,只有第一个测试用例(在setupSpec()方法之后)通过,所有其他测试用例都失败了:groovy.lang.MissingPropertyException: Unable to resolve <some content identifier specified on ThisPage>
我知道我的内容已正确定义:
class ThisPage extends Page {
static at = {
title == "This page"
}
static content = {
someLink(to: SomeOtherPage) {$("a[href='someOtherPage.jsp'")}
signInText {loginInfo.find("#static-label").text().trim()}
}
}
我可以切换测试方法的顺序,第一个将始终通过,即使上次运行失败也是如此。如果我将这些测试方法作为单个单元测试运行,它们也都会通过。
我实现此功能的唯一方法是将at ThisPage
添加到when或given块。我想在每个测试方法中将页面显式设置为ThisPage是有意义的,但是为什么在将整个类作为测试套件运行时,只会传递第一个测试方法(即使没有at ThisPage
)?
答案 0 :(得分:0)
每个后续测试方法都不知道您所在的页面。
您可以创建页面的共享实例,然后可以通过相同规范中的每个测试访问该实例:
class TestThisPage extends AuthenticationSpec {
@Shared ThisPage thisPage
def setupSpec() {
thisPage = loginComplete("some user ID", "some password")
assert page instanceof ThisPage
}
def "Verify some link's text is correct"() {
when: "The user is at this page"
then: "The this specific link's text is correct"
assert thisPage.someLink.text().equalsIgnoreCase("I am some text")
}
def "Verify that the correct user name is displayed"() {
when: "The user is signed in and at this page"
then: "The signed-in user's display name appears correctly on the page"
assert thisPage.signInText.equalsIgnoreCase("Signed in as: some user ID")
}
def "Verify this page's copyright"() {
when: "The user is redirected to this page"
then: "The copyright is correct"
assert thisPage.legalInfo.text().trim().equalsIgnoreCase("Some copyright text here")
}
}