LoginSignup
6
6

More than 5 years have passed since last update.

maven + GebでE2Eテスト自動化を始める by IntelliJ IDEA

Posted at

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

プロジェクトの作成

  1. 「File」→「New」→「Project」を選択
  2. 「Groovy」を選択してプロジェクト作成 →FrameWork等はここでは特に追加する必要はなし
  3. 作成されたプロジェクトを右クリックし「Add Framework Support」を選択
  4. 「maven」にチェックを入れて「OK」を選択
  5. pom.xmlにgroupId等を設定

ここまででベースとなるプロジェクトは一旦作成完了

サンプルの作成

  1. pom.xmlに必要な情報を追記
    ※ここを参考にしてます→http://www.gebish.org/manual/current/#maven

    pom.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>
    
  2. pomでエラーが出るはずなので、右上に出たダイアログから「Enable Auto-Import」を選択

  3. src/test/java配下にPageを継承したクラスを追加

    GooglePage.groovy
    import 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があればこんなふうにチェックも出来ます
        }
    }
    
  4. 同じくsrc/test/java配下にGebReportingSpecを継承したクラスを追加

    GoogleGeb.groovy
    import geb.spock.GebReportingSpec
    
    class GoogleGeb extends GebReportingSpec {
    
        def "go_to_googleHome"() {
            given:
            to GooglePage
    
            expect:
            at GooglePage
        }
    }
    
  5. コンパイルしてGoogleGebを実行すると動くと思います
    →動かないようであればWebDriverのインストールがされていない可能性がありますので、エラーログに従ってインストールすればOKです

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6