我们将用户数据存储在枚举中,然后使用枚举数据将用户数据插入/从我们的数据库中删除。
这听起来有点罗嗦,但这里是我们使用的结构和我们如何使用它的粗略想法。我已经把它很多地泛化了,所以希望它仍然是半可操作的。
在我们下面GenericUserNavigationBlah类样品只是我们用来包裹了不少用户的导航模式,包括记录一个方便的类。
的ExternalUser类是我们所有的用户数据得到存储。我忽略了我们用来表示用户的大部分字段,但理想情况下它将与用户在自己的数据库中的外观相匹配。
你可能并不需要数据库类的样品,它是只包括作为搭配什么是在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
}
}
这是您准确就是我一直在寻找,谢谢你和我原汁原味您在这一切的帮助,我的代表处是15岁以下,但我仍然点击你的答案。再次感谢。 –