Maven, Seleniumを使った受け入れテストの自動化

Apache Maven 3クックブック Javaソフトウェア開発のための特選レシピ集

Apache Maven 3クックブック Javaソフトウェア開発のための特選レシピ集

上記の書籍を使ってMavenの勉強中なのだが、「2.6 受け入れテストを自動化する」で書籍の内容通りにコーディングをしてもうまく動作しなかったので、うまく動作した結果を残しておく。

環境

Mavanプロジェクトの生成

Webアプリケーションプロジェクトを生成する

$ mvn archetype:generate -DgroupId=net.kuronicle.maven -DartifactId=webappsample
 -DarchetypeArtifactId=maven-archetype-webapp

jUnitのバージョン設定

pom.xmljUnitのバージョンを4.8.2とする。

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>

Seleniumライブラリの追加

pom.xmlのdependenciesに以下を追加することで、Seleniumに必要なライブラリがインポートされる。

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.24.1</version>
      <scope>test</scope>
    </dependency>

テスト対象の用意

デフォルトで存在する/src/main/webapp/index.jspを利用する。

<html>
  <body>
    <h2>Hello World!</h2>
  </body>
</html>

Seleniumテストコードの作成

/src/test/java/Selenium2Example.javaを作成する。

import static org.junit.Assert.*;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example {

    @Test
    public void testHelloWorld() {
        WebDriver driver = new FirefoxDriver();
        try {
            driver.get("http://localhost:8080/webappsample/index.jsp");
            WebElement element = driver.findElement(By.tagName("h2"));
            assertEquals("Hello World!", element.getText());
        } catch (Exception e) {
            fail(e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

integration-testフェーズの設定

pom.xmlにintegration-testの前後でJettyを起動・停止する設定と、integration-testで先ほど作成したSeleniumテストケースを実行する設定を追加する。

  <build>
    <finalName>webappsample</finalName>

    <plugins>
      <!-- JDK1.6でコンパイル -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

      <!-- integration-testフェーズ前後にJetty起動停止 -->
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>${maven-jetty-plugin.version}</version>
        <executions>
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <scanIntervalSeconds>0</scanIntervalSeconds>
              <daemon>true</daemon>
            </configuration>
          </execution>
          <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
            <configuration>
              <stopKey>foo</stopKey>
              <stopPort>9999</stopPort>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- integration-testフェーズでSeleniumテストケース(*Selenium*.java)を実行 -->
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
          <execution>
            <id>selenium-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <includes>
                <include>**/*Selenium*.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

integration-testの実行

integration-testを実行する。Firefoxが自動的に立ち上がり、試験が実行される。

$ mvn integration-test

以上。
なぜ書籍のサンプルコードが動かなかったのかは結局わからず…。