maven + GebでE2Eテスト自動化を始める by IntelliJ IDEA
このページに関して
GebでE2Eテスト自動化したい!と思ったものの、GradleとEclipseのサンプルが多かったので
IntelliJ IDEAとmaven使って始めるサンプルのメモとして記録しておきます。
サンプルとして、GebでGoogleのトップページにアクセスして情報を取得してみます。
環境
OS:Windows 7
IDE:IntelliJ IDEA 2016.1.3
Java:1.8.0_71
maven:3.2.5
プロジェクトの作成
- 「File」→「New」→「Project」を選択
- 「Groovy」を選択してプロジェクト作成 →FrameWork等はここでは特に追加する必要はなし
- 作成されたプロジェクトを右クリックし「Add Framework Support」を選択
- 「maven」にチェックを入れて「OK」を選択
- pom.xmlにgroupId等を設定
ここまででベースとなるプロジェクトは一旦作成完了
サンプルの作成
-
pom.xmlに必要な情報を追記
※ここを参考にしてます→http://www.gebish.org/manual/current/#mavenpom.xml<dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.4.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>1.0-groovy-2.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.gebish</groupId> <artifactId>geb-spock</artifactId> <version>0.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>2.52.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-support</artifactId> <version>2.52.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <includes> <include>*Spec.*</include> </includes> <systemPropertyVariables> <!-- キャプチャの対象と出力先 --> <geb.build.baseUrl>http://google.com/ncr</geb.build.baseUrl> <geb.build.reportsDir>target/test-reports/geb</geb.build.reportsDir> </systemPropertyVariables> </configuration> </plugin> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <version>1.3</version> <configuration> <providerSelection>1.7</providerSelection> </configuration> <executions> <execution> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
pomでエラーが出るはずなので、右上に出たダイアログから「Enable Auto-Import」を選択
-
src/test/java配下にPageを継承したクラスを追加
GooglePage.groovyimport geb.Page class GooglePage extends Page { static url = "http://www.google.co.jp" static at = { title == "Google" $("meta[name=description]").attr("content") == "世界中のあらゆる情報を検索するためのツールを提供しています。さまざまな検索機能を活用して、お探しの情報を見つけてください。" // $("h1").text() == "" ←h1があればこんなふうにチェックも出来ます } }
-
同じくsrc/test/java配下にGebReportingSpecを継承したクラスを追加
GoogleGeb.groovyimport geb.spock.GebReportingSpec class GoogleGeb extends GebReportingSpec { def "go_to_googleHome"() { given: to GooglePage expect: at GooglePage } }
コンパイルしてGoogleGebを実行すると動くと思います
→動かないようであればWebDriverのインストールがされていない可能性がありますので、エラーログに従ってインストールすればOKです