GebConfig.groovyに設定値を定義してSpockテストでその定義を呼び出したい
【Geb】【Tips】GebConfig.groovyに設定値を定義してSpockテストでその定義を呼び出したい
著者:ふじさわゆうき
問題
- Geb+Spockの正常系のテストにおいて、ユーザーID、パスワードを共通設定ファイルに定義しておくと全テストケースで共有できるので定義したい。しかし、Geb+Spockにおいてその方法がわからない
解決
- "GebConfig.groovy"に共通設定を定義することで全テストケースで共有することができる
- 以下の文法で"GebConfig.groovy"に定義した設定値をテストケースで呼び出すことができる
browser.config.rawConfig.[key] 例:browser.config.rawConfig.userId
実装
- 「googleログイン画面にアクセス」→「ログイン処理」→「アカウント設定画面」というテストケースをもとに実装の説明を進める
設定化前
- 「$("form").Email = "test@gmail.com"」のようにメールアドレスがテストコードに書かれているので、他テストケースとユーザーIDを共有することができない。つまり、ユーザーIDに変更があった場合は、すべてのテストケースを修正する必要がでてくる
- ConfigTest.groovy
import geb.spock.GebSpec class ConfigTest extends GebSpec { def "google login test"() { setup: def googleLoginUrl = "https://accounts.google.com" when: go googleLoginUrl then: waitFor{ title == "ログイン - Google アカウント" } when: $("form").Email = "test@gmail.com" $("form").Passwd = "password" $("input" , name:"signIn").click() then: waitFor{ title == "アカウント設定"} } }
設定化後
- "googleUserId"と"googlePassword "を"GebConfig.groovy"に定義することで、他テストケースと共有することができるようにしている
- 「key = value」の形式で定義
- "ConfigTest.groovy"では、"GebConfig.groovy"で定義した"googleUserId"と"googlePassword"を呼び出している
- def googleUserId= browser.config.rawConfig.googleUserId
- def googlePassword= browser.config.rawConfig.googlePassword
- GebConfig.groovy
googleUserId = "test@gmail.com" googlePassword = "password"
- ConfigTest.groovy
import geb.spock.GebSpec class ConfigTest extends GebSpec { def "google login test"() { setup: def googleLoginUrl = "https://accounts.google.com" def googleUserId = browser.config.rawConfig.googleUserId def googlePassword = browser.config.rawConfig.googlePassword when: go googleLoginUrl then: waitFor{ title == "ログイン - Google アカウント" } when: $("form").Email = googleUserId $("form").Passwd = googlePassword $("input" , name:"signIn").click() then: waitFor{ title == "アカウント設定"} } }