本文介绍了如何重构常见的Geb测试序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有多个Geb / Spock测试用于登录。例如: @Stepwise
类AddNewPictureSpec扩展GebSpec {
def用户在登录页面(){
给出:用户从登录页面生成
到LoginPage
}
def用户获取重定向到主页面(){
给出:用户在登录页面
在LoginPage
当:用户登录
登录用户名 ,pw
到MainPage
然后:
在MainPage
def其他测试序列跟随....(){
$ / code>
另一个测试规范的启动顺序完全相同: p>
@Stepwise
类EditPictureSpec扩展了GebSpec {
def登录页面的用户(){
给出:用户从登录页面
到LoginPage
}
def用户获取重定向到主页面(){
给出:用户登录页面
在LoginPage
时:用户登录
登录用户名,pw
到MainPage
然后:
at MainPage
def其他测试序列按照....(){
}
}
如何重构/提取常用登录步骤以便我没有重复的代码?还是我错误地写我的测试?感谢。
解决方案
我认为'geb'的方法是使用。
你可以创建一个像这样的登录模块:
class LoginModule extends Module {
static content = {
loginForm {$(form) }
loginButton {$(input,value:Sign in)}
}
void login(String username,String password =Passw0rd!){
loginForm.j_username = username
loginForm.j_password =密码
loginButton.click()
}
}
将它包含在
LoginPage
中:
class LoginPage extends Page {
static url =login / auth
static at = {title ==我的Grails应用程序}
static content = {
loginModule {module LoginModule}
}
}
然后在你的测试中,你可以引用你的模块的
login
方法:
$ b
@Stepwise
类EditPictureSpec扩展GebSpec {
def setupSpec() {
到LoginPage
loginModule.login(loginUsername)
}
defsome test(){
...
}
}
Suppose I have multiple Geb/Spock tests that beings with logging in. For example:
@Stepwise
Class AddNewPictureSpec extends GebSpec {
def "User at login page"() {
given: "User beings from login page"
to LoginPage
}
def "User gets redirected to Main page"() {
given: "User at Login page"
at LoginPage
when: "User signs in"
signIn "username", "pw"
to MainPage
then:
at MainPage
def "other test sequences follow...."() {
}
}
And another test spec with the exact same start sequence:
@Stepwise
Class EditPictureSpec extends GebSpec {
def "User at login page"() {
given: "User beings from login page"
to LoginPage
}
def "User gets redirected to Main page"() {
given: "User at Login page"
at LoginPage
when: "User signs in"
signIn "username", "pw"
to MainPage
then:
at MainPage
def "other test sequences follow...."() {
}
}
How do I refactor/extract out the common login "steps" so that I do not have duplicate code? Or am I writing my tests wrongly? Thanks.
解决方案
I think the 'geb' way to do this is to use modules.
You can create a login module like this:
class LoginModule extends Module {
static content = {
loginForm {$("form")}
loginButton {$("input", value: "Sign in")}
}
void login(String username, String password = "Passw0rd!") {
loginForm.j_username = username
loginForm.j_password = password
loginButton.click()
}
}
Include it in your
LoginPage
:
class LoginPage extends Page {
static url = "login/auth"
static at = {title == "My Grails Application"}
static content = {
loginModule { module LoginModule }
}
}
Then in your test, you can reference your module's
login
method:
@Stepwise
class EditPictureSpec extends GebSpec {
def setupSpec() {
to LoginPage
loginModule.login(loginUsername)
}
def "some test"() {
...
}
}
这篇关于如何重构常见的Geb测试序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!