Spring Boot + npm + Geb で入力フォームを作ってテストする ( その29 )( Geb をインストールする )
概要
記事一覧はこちらです。
参照したサイト・書籍
Geb - Very Groovy Browser Automation
http://www.gebish.org/Web画面自動テストフレームワーク「Geb」の紹介
http://lab.astamuse.co.jp/entry/geb_test_01Geb with spock
https://www.slideshare.net/MonikaGurram/geb-with-spock-24710923mozilla/geckodriver
https://github.com/mozilla/geckodriverGradle – How to exclude some tests
https://www.mkyong.com/gradle/gradle-how-to-exclude-some-tests/Getting Started With Gradle: Integration Testing
https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/Spring Boot and Gradle: Separating tests
https://moelholm.com/2016/10/22/spring-boot-separating-tests/
目次
- 方針
- Geb に必要なモジュールをインストールする
- GebConfig.groovy を作成する
- テスト用のパッケージを作成する
- 入力画面1の Page Objects を作成する
- 簡単なテストを作成する
- geckodriver をインストールする
- gebTest タスクを作成する
- 動作確認
- IntelliJ IDEA 上からテストを実行できるようにする
手順
方針
- テストは Geb + Spock の組み合わせで作成します。geb-core ではなく geb-spock を指定してインストールします。
- ブラウザは Firefox を利用します。今回テストを作成している時にインストールしている Firefox のバージョンは 56.0.2 (64ビット) です。
- Geb は現時点で最新の 2.0-rc-1、selenium は 3.6.0 をインストールします。
- Geb のテストは src/test/groovy/geb の下に作成します。src の下に main, test とは別にディレクトリは作成しません。
src/test/groovy ├ geb │ ├ gebspec │ └ page └ ksbysample └ webapp └ bootnpmgeb
Geb に必要なモジュールをインストールする
Geb + Spock でテストを作成するために必要なモジュールをインストールします。1.5. Installation & usage を参考に、build.gradle の dependencies に以下の3行を追加します。
dependencies { .......... // for Geb + Spock testCompile("org.gebish:geb-spock:2.0-rc-1") testCompile("org.seleniumhq.selenium:selenium-firefox-driver:3.6.0") testCompile("org.seleniumhq.selenium:selenium-support:3.6.0") }
変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新します。
GebConfig.groovy を作成する
共通の設定を記述するための GebConfig.groovy を src/test/resources の下に作成します。ファイルを作成した後、7. Configuration を参考に以下のように記述します。
driver = "firefox" baseUrl = "http://localhost:8080" waiting { timeout = 15 }
テスト用のパッケージを作成する
src/test/groovy の下に geb パッケージを作成し、geb の下に gebspec, page パッケージを作成します。
入力画面1の Page Objects を作成する
src/test/groovy/geb/page の下に inquiry パッケージを作成した後、inquiry の下に InquiryInput01Page.groovy を作成して以下の内容を記述します。
package geb.page.inquiry import geb.Page class InquiryInput01Page extends Page { static url = "/inquiry/input/01" static at = { title == "入力フォーム - 入力画面1" } }
簡単なテストを作成する
動作確認のために簡単なテストを作成します。src/test/groovy/geb/gebspec の下に SimpleTestSpec.groovy を作成して以下の内容を記述します。
package geb.gebspec import geb.page.inquiry.InquiryInput01Page import geb.spock.GebSpec class SimpleTestSpec extends GebSpec { def "動作確認用"() { expect: "入力画面1へアクセスする" to InquiryInput01Page waitFor { at InquiryInput01Page } } }
geckodriver をインストールする
Web画面自動テストフレームワーク「Geb」の紹介 の記事によると Firefox の 47.0以降 から geckodriver を gradle でのダウンロードとは別にインストールする必要があるとのことですので、ダウンロード&インストールします。
https://github.com/mozilla/geckodriver/releases を見ると最新バージョンは v0.19.0 です。下にある geckodriver-v0.19.0-win64.zip のリンクをクリックしてダウンロードします。
ダウンロードした geckodriver-v0.19.0-win64.zip を解凍すると geckodriver.exe が出来ますので、C:\geckodriver\0.19.0 の下に配置します。
gebTest タスクを作成する
build.gradle に gebTest タスクを追加します。また test タスクで ksbysample パッケージ配下のテストが実行されないようにします。
test { jvmArgs = ['-Dspring.profiles.active=unittest'] exclude "geb/**" } task gebTest(type: Test) { jvmArgs = [ '-Dspring.profiles.active=unittest' , '-Dwebdriver.gecko.driver=C:/geckodriver/0.19.0/geckodriver.exe' ] exclude "ksbysample/**" }
- test タスクに
exclude "geb/**"
を追加します。 - gebTest タスクを追加します。
変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして反映します。
動作確認
Tomcat を起動した後、gebTest タスクを実行してみます。。。が、java.lang.ClassNotFoundException: org.openqa.selenium.MutableCapabilities
というエラーメッセージが出て失敗しました。
モジュールを見てみると selenium 関連で4つ表示されているのですが、selenium-api と selenium-remote-driver が 2.53.1 と古いバージョンになっています。全て 3.6.0 になるよう調整します。
build.gradle を以下のように変更します。
dependencies { .......... // for Geb + Spock testCompile("org.gebish:geb-spock:2.0-rc-1") testCompile("org.seleniumhq.selenium:selenium-firefox-driver:3.6.0") testCompile("org.seleniumhq.selenium:selenium-support:3.6.0") testCompile("org.seleniumhq.selenium:selenium-api:3.6.0") testCompile("org.seleniumhq.selenium:selenium-remote-driver:3.6.0") }
- 以下の2行を追加します。
testCompile("org.seleniumhq.selenium:selenium-api:3.6.0")
testCompile("org.seleniumhq.selenium:selenium-remote-driver:3.6.0")
変更後、Gradle Tool Window の左上にある「Refresh all Gradle projects」ボタンをクリックして更新すると、今度は全て 3.6.0 になりました。
再度 getTest タスクを実行すると、今度は Firefox が起動して入力画面1にアクセスし、テストが成功しました。
Tomcat を停止した後、clean タスク → Rebuild Project → build タスクを実行すると BUILD SUCCESSFUL のメッセージが出力されました。test タスク中に Firefox は起動しなかったので、Geb で作成したテストは実行されませんでした。
IntelliJ IDEA 上からテストを実行できるようにする
今の設定だけでは IntelliJ IDEA のエディタの左側のメニューから Run 動作確認用()
を選択してもテストを実行できません。
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
というエラーメッセージが出力されます。
IntelliJ IDEA のメインメニューから「Run」-「Edit Configurations...」を選択して「Run/Debug Configurations」ダイアログを表示した後、「JUnit」の「VM Options」に -Dwebdriver.gecko.driver=C:/geckodriver/0.19.0/geckodriver.exe
を追加します。
再度 Run 動作確認用()
を選択してテストを実行すると Firefox が起動して入力画面1にアクセスし、テストが成功しました。
履歴
2017/10/27
初版発行。