2017-05-12 17 views
0

我想弄清楚如何使用现有的数据并将其存储在数组中以用作数据值为我的自动化或直接从数据库使用数据。Geb + Spock + Groovy - 从数据库中获取数据并在自动化中用作输入语句的值

击穿

  1. 与填充的数据的现有分贝
  2. LoginPage要进行自动化
  3. Harded用户登录名和用户口令
  4. 现有分贝具有用于LoginPage(测试环境)的用户名和密码数据
  5. 创建用于连接和捕获所需值的脚本
  6. 接下来是什么?如何使用从数据库中捕获的用户名和密码在我的代码中使用,所以我不必硬编码的用户名和密码字段。

注意:我会分享它是公司特定的代码。感谢您的帮助

回答

0

我们将用户数据存储在枚举中,然后使用枚举数据将用户数据插入/从我们的数据库中删除。

这听起来有点罗嗦,但这里是我们使用的结构和我们如何使用它的粗略想法。我已经把它很多地泛化了,所以希望它仍然是半可操作的。

在我们下面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 
    } 
} 
+0

这是您准确就是我一直在寻找,谢谢你和我原汁原味您在这一切的帮助,我的代表处是15岁以下,但我仍然点击你的答案。再次感谢。 –

相关问题