我想熟悉Geb。我试图从Grails内部运行它,但这根本不重要,因为我的问题是针对Geb的。通过示例登录Geb登录规范
我有以下test
目录结构:
myapp/
<lots of stuff here>
test/
functional/
GebConfig.groovy
LogInLogOutSpec.groovy
pages/
LogInPage.groovy
DashboardPage.groovy
其中LoginPage.groovy
是(显然)登录页面,并在DashboardPage
是你应该成功登录后,会重定向。实际上,我有安全过滤器,用于检查您尝试访问的URL是否需要身份验证。如果是这样,他们会将您重定向到登录页面,并且在成功登录后,将您重新定向到您尝试访问的URL。
良好的措施,这里包括我的登录页面(再次,这是Grails的,所以GSP文件被动态地转换成HTML在运行时)的HTML:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<form action=“/myapp/auth/signIn" method="post">
<input type="hidden" name="targetUri" value="/dashboard" />
<table>
<tbody>
<tr>
<td>Username:</td>
<td><input type="text" name="username" value="" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="" /></td>
</tr>
<tr>
<td />
<td><input type="submit" value="Sign in" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
所以我需要一个盖布测试:
- 尝试去仪表板页面(非授权)
- 验证我们,而不是被重定向到登录页面
- 日志中具有良好的CR edentials
- 检验我们是不是在仪表板页面
此外:
- 我想之间做出选择:(1)无论是基于浏览器(Firefox或Chrome)或( b)一个无头浏览器(我相信是
HtmlUnitDriver
,但纠正我,如果我错了) - 我想登录凭据使用的Geb可以从
GebConfig
文件或一些外部参数(可能是提供给Grails命令行的env vars或运行时参数)。基本上我不想在测试代码中存储用户名/密码。
我迄今为止最好的尝试:
GebConfig.groovy
:
driver = {
// Again, or Firefox
ChromeProfile profile = new ChromeProfile()
Driver driverInstance = new ChromeDriver(profile)
driverInstace.manage().window().maximize()
driverInstance
}
baseNavigatorWaiting = true
atCheckWaiting = true
// Need to inject from some external process somehow, but make these
// available to test specs somehow.
username = System.env("USERNAME")
password = System.env("PASSWORD")
LogInLogOutSpec.groovy
:
import geb.spock.GerbReportingSpec
import spock.lang.*
import pages.*
@Stepwise
class LogInLogOutSpec extends GebReportingSpec {
String username
String password
/*
* Attempt to go to user dashboard, and confirm you are instead
* redirected to the login page. Login with good credentials,
* and verify you are now logged in and at the dashboard.
*/
def "successful login redirects to dashboard page"() {
when:
to DashboardPage
then:
"Login".equals($(".page-header").text())
when:
$("#login-form input[name=username]").value(username)
$("#login-form input[name=password]").value(password)
then:
"Dashboard".equals($(".page-header").text())
}
}
我觉得我接近,但是这显然是错误的。任何想法,我要走错了?谢谢!
我没有看到您将表格提交到任何地方,那可能是问题所在。如果它仍然不起作用,我认为你必须缩小它无法缓解进一步帮助的地方。此外,该最新的视频可以帮助你一点点GEB最佳实践:https://www.youtube.com/watch?v=rpepX441E5g&feature=youtu.be – wwwclaes
是啊,这已经有一段时间,因为我做了一个测试盖布这样但wwwclaes说我的第一个想法。你在哪里提交登录表单或点击“登录”按钮。 –
所以你不要说这是如何失败的。冷杉在这种情况下所做的事情就是做最简单,最不抽象的测试脚本。不要将登录页面或仪表盘页面抽象成一些特殊的Geb配置。不要担心外部配置中的用户名/密码。先把它打到最“简单”的方式工作。一旦开始工作,开始重构以使用更高级的Geb特性。 –