我想熟悉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特性。 –