【入門】Geb+SpockではじめるWebテスト~スクリーンショット編~
【入門】Geb+SpockではじめるWebテスト~スクリーンショット編~
著者:ふじさわゆうき
この記事は、以下の記事の続きです。
初めて訪問した方は以下の記事を参照してください。
目次
- 前回までのあらすじ
- 今回の目的
- GebConfig.groovyの修正
- 継承クラスの変更(GebSpec→GebReportingSpec)
- まとめ
1. 前回までのあらすじ
- Gebとは何かの説明
- Gebのメリット説明
- 「Eclipse + maven + Geb + Spock」での環境構築説明
- サンプルプログラム実装
- google検索⇒検索結果⇒WIKIという流れのWebテスト実施
- 「Geb + Spock」による上記Webテストの実施
- クロスブラウザの説明
2. 今回の目的
- 「Geb + Spock」のWebテストで、テスト失敗時のスクリーンショットを出力できるようにすること
3. GebConfig.groovyの修正
- Geb用の設定ファイルGebConfig.groovyに"reportsDir"と"reportOnTestFailureOnly"を追加する
- reportsDir = "target/geb-reports"
- レポート出力先(スクリーンショット出力先)を"target/geb-reports"に設定する
- reportOnTestFailureOnly = true
- "true"だと、テストが失敗した場合のみスクリーンショットが出力される。"false"にすると成功した場合でもスクリーンショットが出力される
- reportsDir = "target/geb-reports"
//choose "htmlunit", "firefox", "ie", "chrome" driver = "firefox" //reports setting reportsDir = "target/geb-reports" reportOnTestFailureOnly = true //chrome - http://chromedriver.storage.googleapis.com/index.html System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe") //ie - http://selenium-release.storage.googleapis.com/index.html System.setProperty("webdriver.ie.driver", "driver/IEDriverServer.exe")
4. 継承クラスの変更(GebSpec→GebReportingSpec)
- GoogleWikipediaMainTest.groovyを開く
- GoogleWikipediaTest/src/test/java/main/GoogleWikipediaMainTest.groovy
- "extends GebSpec"を"extends GebReportingSpec"に修正する
- "import geb.spock.GebSpec"を"import geb.spock.GebReportingSpec"に修正する
- GoogleWikipediaMainTest.groovyのテストが失敗するように修正する。"wikipedia"→"wikipedi"
- firstResultLink.text() == "Wikipedi"
package main import geb.spock.GebReportingSpec import page.GoogleHomePage import page.GoogleResultsPage import page.WikipediaPage class GoogleWikipediaMainTest extends GebReportingSpec { def "first result for wikipedia search should be wikipedia"() { given: to GoogleHomePage expect: at GoogleHomePage when: search.field.value("wikipedia") then: waitFor { at GoogleResultsPage } and: firstResultLink.text() == "Wikipedi" when: firstResultLink.click() then: waitFor { at WikipediaPage } } }
5. GoogleWikipediaMainTest.groovyの右クリック > 実行 > JUnitテスト
6. "target/geb-reports/main/GoogleWikipediaMainTest"以下に失敗時のスクリーンショットが出力される
- 失敗時のスクリーンショット
- Spockのエラーログ
Condition not satisfied: firstResultLink.text() == "Wikipedi" | | | | | false | | 1 difference (88% similarity) | | Wikipedi(a) | | Wikipedi(-) | Wikipedia
5. まとめ
- Geb用の設定ファイルGebConfig.groovyに"reportsDir"と"reportOnTestFailureOnly"を追加する
- reportsDir = "target/geb-reports"
- reportOnTestFailureOnly = true
- 継承する(extends)クラスを"GebSpec"から"GebReportingSpec"に変更する
- テストが失敗すると"target/geb-reports"以下にスクリーンショットが出力される