【发布时间】:2017-01-05 20:15:59
【问题描述】:
我正在为网站测试编写基于 Spock + Geb 的测试。网站用于创建报价,随后可以对其执行各种操作(确认、拒绝、撤回)。我已经为创建报价创建了工作场景。但是现在当我需要为各种操作组合实现场景时,我希望能够在这些其他场景中重用“创建报价”。不幸的是,我无法找到任何示例或思考如何做到这一点的好方法。有人可以给我任何想法吗?
顺便说一句,我需要按预定义的顺序执行操作,所以我在规范中使用 Stepwise 注释。
【问题讨论】:
我正在为网站测试编写基于 Spock + Geb 的测试。网站用于创建报价,随后可以对其执行各种操作(确认、拒绝、撤回)。我已经为创建报价创建了工作场景。但是现在当我需要为各种操作组合实现场景时,我希望能够在这些其他场景中重用“创建报价”。不幸的是,我无法找到任何示例或思考如何做到这一点的好方法。有人可以给我任何想法吗?
顺便说一句,我需要按预定义的顺序执行操作,所以我在规范中使用 Stepwise 注释。
【问题讨论】:
如果不查看代码示例,很难说,但这样做的一种方法是将操作(确认、拒绝、撤回)的方法定义为 Page 类中的简单方法:
class ActionsPage extends Page {
static content = {
//Page content here
}
def confirm (){
//Code to perform confirm action on the page
}
def reject (){
//Code to perform reject action on the page
}
然后在你的规范类中你可以做
def "Test confirm action"(){
when: "we go to the actions pages"
to ActionsPage
and: "we perform a confirm action"
confirm()
then: "something happens"
//Code which checks the confirmation worked
}
之所以有效,是因为 Geb 使用 Groovy 的方法缺失东西来查找当前页面上名为“confirm()”的方法的名称,并将执行该代码。
如果操作很复杂并且涉及导航到多个页面,最好为需要执行操作的测试创建一个抽象基类:
//Not @Stepwise
abstract class BaseActionsSpec extends Specification {
//Note: don't use Spock when/then blocks in these methods as they are not feature methods
protected void confirm(){
//Code to perform multi-page action
}
protected void reject(){
//Code to perform multi-page action
}
}
然后是扩展类:
@Stepwise
class ConfirmActionSpec extends BaseActionsSpec{
def "Test confirm action"(){
when: "we go to the actions pages"
to ActionsPage
and: "we perform a confirm action"
confirm() //calls method in super class
then: "something happens"
//Code which checks the confirmation worked
}
}
【讨论】: