yfj2’s Automatic Web Test Related Blog

yfj2のWEBテスト自動化に関わるブログ

【入門】Geb+SpockではじめるWebテスト~スクリーンショット編~

【入門】Geb+SpockではじめるWebテスト~スクリーンショット編~
著者:ふじさわゆうき

この記事は、以下の記事の続きです。
初めて訪問した方は以下の記事を参照してください。

目次

  1. 前回までのあらすじ
  2. 今回の目的
  3. GebConfig.groovyの修正
  4. 継承クラスの変更(GebSpec→GebReportingSpec)
  5. まとめ

1. 前回までのあらすじ

  1. Gebとは何かの説明
  2. Gebのメリット説明
  3. 「Eclipse + maven + Geb + Spock」での環境構築説明
  4. サンプルプログラム実装
    1. google検索⇒検索結果⇒WIKIという流れのWebテスト実施
    2. 「Geb + Spock」による上記Webテストの実施
  5. クロスブラウザの説明
    1. 【入門】Geb+SpockではじめるWebテスト~クロスブラウザテスト編~ - yfj2’s Automatic Web Test Related Blog

2. 今回の目的

  • 「Geb + Spock」のWebテストで、テスト失敗時のスクリーンショットを出力できるようにすること

3. GebConfig.groovyの修正

  1. Geb用の設定ファイルGebConfig.groovyに"reportsDir"と"reportOnTestFailureOnly"を追加する
    • reportsDir = "target/geb-reports"
      • レポート出力先(スクリーンショット出力先)を"target/geb-reports"に設定する
    • reportOnTestFailureOnly = true
      • "true"だと、テストが失敗した場合のみスクリーンショットが出力される。"false"にすると成功した場合でもスクリーンショットが出力される
//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)

  1. GoogleWikipediaMainTest.groovyを開く
    • GoogleWikipediaTest/src/test/java/main/GoogleWikipediaMainTest.groovy
  2. "extends GebSpec"を"extends GebReportingSpec"に修正する
  3. "import geb.spock.GebSpec"を"import geb.spock.GebReportingSpec"に修正する
  4. 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"以下に失敗時のスクリーンショットが出力される

  • 失敗時のスクリーンショット

f:id:yfj2:20141115200804p:plain

  • Spockのエラーログ
Condition not satisfied:

firstResultLink.text() == "Wikipedi"
|               |      |
|               |      false
|               |      1 difference (88% similarity)
|               |      Wikipedi(a)
|               |      Wikipedi(-)
|               Wikipedia

5. まとめ

  1. Geb用の設定ファイルGebConfig.groovyに"reportsDir"と"reportOnTestFailureOnly"を追加する
    • reportsDir = "target/geb-reports"
    • reportOnTestFailureOnly = true
  2. 継承する(extends)クラスを"GebSpec"から"GebReportingSpec"に変更する
  3. テストが失敗すると"target/geb-reports"以下にスクリーンショットが出力される