场景:我有一个登录页面,它使用 ajax 来验证用户,如果登录无效,它会停留在同一页面上。
我想知道这是否是在 Geb 中使用 at 的正确方法,或者我可以即兴发挥。关注点:
- 我正在使用带有硬编码超时等的 waitFor。
- waitFor 应该在 at block 中吗?
- 有没有更好的写法?
规范定义
def "perform invald login"()
{
given: "I am at the login page"
to LoginPage
when : "I entered invalid data"
loginForm.loginClientCode = "test"
loginForm.loginSystemCode = "test"
loginForm.loginUserId = "test"
loginForm.loginPassword = "test"
loginButton().click()
then: "Log in attempt unsuccessful"
at(LoginPage)
}
页面对象
class LoginPage extends Page
{
static url = "login/login.jsf";
static at =
{
waitFor(10,0.5)
{ $("div.ic-h1").text() == "User Authentication" }
}
static content =
{
loginForm
{
$("form",id: "loginForm")
}
loginButton
{
$("button", id: "loginButton")
}
statusMessages
{
$('div.ui-messages').text()
}
}
}
最佳答案
我们通常只保留 at 用于验证目的,然后:
waitFor{ at LoginPage }
但是鉴于 0.7 中对隐式断言的新支持,这可能不需要。 http://www.gebish.org/manual/snapshot/implicit-assertions.html#at_verification
我认为您在这里真正想要的是测试是否存在错误消息并等待它。
即
loginButton().click()
then: "Log in attempt unsuccessful"
waitFor{ statusMessage == 'this login failed' }
因为否则你无法真正让你的 then 条件失败。
此外,您可能会将条件放入页面对象的状态中,例如
def isErrorState() {
statusMessage == 'this login failed'
}
并且在您的测试中,它变得更容易阅读。
waitFor{ isErrorState() }
关于java - 这是在 Geb 中使用 "at"闭包的正确方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823837/