のためのおかげで、私たちはその後、私たちのDBから/へのユーザデータを挿入/削除するには、列挙型のデータを使用しています。
これは少し長ったらしいを取得しますが、ここで我々が使用する構造の大まかなアイデアですし、我々はそれを使用する方法について説明します。私はそれをたくさんジェネリック化したので、うまくいけばそれはまだ半実行可能です。私たちのGenericUserNavigationBlahクラス以下のサンプルで
は、我々はログインを含むユーザナビゲーションパターンの多くを包むために使用するだけで便利なクラスです。
当社のすべてのユーザーデータが取得する場所ExternalUserクラスがあります保存される。私は、ユーザーを表現するために使用するフィールドのほとんどを除外しましたが、理想的には、ユーザーが自分のDBでどのように見えるかに一致します。あなたはおそらくデータベースクラスのサンプルを必要としなかった
、それが唯一のタイアップExternalUserクラスで起こったものととして含まれていました。
最後にSampleSpecは、私がExternalUserクラスを払拭したため、欠落したビットを実装しない限り動作しません
希望これは、 Deonに役立ちます。
import geb.Browser
import ...ExternalUser
class GenericUserNavigationBlah {
Browser browser
GenericUserNavigationBlah(Browser browser){
this.browser = browser
}
void setBrowser(Browser browser) {
this.browser = browser
}
def methodMissing(String name, args) {
browser."$name"(*args)
}
def propertyMissing(String name) {
browser."$name"
}
def logon(ExternalUser credentials) {
assert browser.isAt(LogonPage)
browser.userID = credentials.userId
browser.password = credentials.pass
browser.logonButton.click()
}
// + tons more useful helper methods of generic user navigation blahness :)
}
import ...Database
enum ExternalUser {
TESTUSER1(1, "FirstName","LastName","99TEST_USER","Supercalifragilisticexpialidocious", "ImagineAnEncryptedPasswordGoesHere")
private ExternalUser(int uniqueId, String firstName, String lastName, String userId, String pass, String encryptedPass) {
this.uniqueId = uniqueId
this.firstName = firstName
this.lastName = lastName
this.userId = userId
this.pass = pass
this.encryptedPass = encryptedPass
}
// fields should match your database table
private final int uniqueId
private final String firstName
private final String lastName
private final String userId
private final String pass
private final String encryptedPass
def create() {
remove()
Database.instance.ourDatabase.execute("insert into YourUserTable (UniqueId, FirstName, LastName, UserID, Password, EncryptedPassword) values (?, ?, ?, ?, ?, ?)", [uniqueId, firstName, lastName, userId, pass, encryptedPass])
}
def remove() {
Database.instance.ourDatabase.execute("delete from YourUserTable where UniqueId = ?", uniqueId)
}
}
import groovy.sql.Sql
@Singleton(strict=false)
class Database {
private Database() {
super()
addShutdownHook {
closeConnections()
}
}
private static final String JTDS_DRIVER = "net.sourceforge.jtds.jdbc.Driver"
private Sql ourDatabaseSql = null
private Sql getSql(String url, String user, String pw) {
Sql.newInstance(url, user, pw, JTDS_DRIVER)
}
def Sql getOurDatabaseSql() {
if (null == ourDatabaseSql) {
ourDatabaseSql = getSql("DBURL", "DBUSER", "DBPASS") // We user a config slurper but I didn't want to go into that here
}
ourDatabaseSql
}
/**
* Cleanup any connections that have been created
* @return
*/
private closeConnections() {
closeQuietly(ourDatabaseSql)
}
/**
* Close the given connection if not null, swallowing any exceptions
* @param sql
* @return
*/
private closeQuietly(Sql sql) {
if (null != sql) {
try {
sql.close()
} catch (Exception e) {
println "Exception closing sql: " + e
}
}
}
}
import geb.spock.GebReportingSpec
import ...ExternalUser
import ...GenericUserNavigationBlah
...
class SampleSpec extends GebReportingSpec {
GenericUserNavigationBlah gunb = new GenericUserNavigationBlah(browser)
@Shared ExternalUser user = ExternalUser.TESTUSER1
def setup() {
user.create()
}
def "Main page is displayed when a user logs in successfully"() {
when: "A user logs in successfully"
gunb.logon(user)
then: "the main page is displayed"
at MainPage
}
def "User is locked out after 3 failed login attempts"() {
given: "a user has attempted 2 bad logins already"
user.setBadLoginAttempts(2)
and: "the user is on the login page"
at LogonPage
and: "they are not locked out"
!user.isLockedOut()
when: "they attempt a third bad login"
userNameInput = user.userId
passwordInput = "IWonderIfMyCapsLockIsOn..."
then: "they are now locked out"
user.isLockedOut()
and: "the generic Invalid credentials message is displayed on the logon page"
at LogonPage
someMessage.text() == "Invalid credentials used, try again."
}
def "User is logged in successfully on 3rd attempt after 2 bad attempts"() {
given: "a user has attempted 2 bad logins already"
user.setBadLoginAttempts(2)
and: "the user is on the login page"
at LogonPage
and: "they are not locked out"
!user.isLockedOut()
when: "they attempt a third good login"
userNameInput = user.userId
passwordInput = user.pass
then: "the main page is displayed"
at MainPage
}
}
これは、私の担当者がされ、私が探していたものextactlyであるあなたに感謝し、私はこの上のすべてのあなたの助けをappeciate 15歳未満でしたが、私はまだ答えをクリックしました。もう一度感謝します。 –