Selenium2.x と Jetty のコラボ
前回の記事で、Selenium2.x 系を使って、テストを書いてみるところまで紹介した。
今回は、このテストをもっと自動で行うための設定周りについて書いておく。
(あくまで自分のため)
で、「もっと自動」と言っているのがどこまでなのかを明確にする。
コマンドラインから「mvn integration-test」で、テストを行うためのサーバの起動・停止、テストの実行、を一通り行うところまでとしておく。
(とりあえずここまで出来れば後々の Jenkins を使った自動テストの布石にもなると思うので)
では早速。手順は以下の順番。
(参考にさせていただいたのはこちらの記事。ありがとうございます!!)
- サーバ(Jetty)の起動
- Selenium テストの実行
- サーバ(Jetty)の停止
で、皆さんもしかしたらお分かりかもしれないが、今回は主に pom.xml を触ります。
(主にというか、全て。。?!)
まずは、1と3のサーバ起動、停止
これは、pom の Build Setting(ビルド設定)領域に以下のように記述します。
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>7.4.2.v20110526</version> <configuration> <webAppConfig> <contextPath>/${project.name}</contextPath> </webAppConfig> <scanIntervalSeconds>10</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <maxIdleTime>60000</maxIdleTime> <port>8080</port> </connector> </connectors> </configuration> <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>
また、接続する際のコネクタとして org.eclipse.jetty.server.nio.SelectChannelConnector を使用しているので、依存関係に以下を足す。
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>7.4.2.v20110526</version> </dependency>
最後に「integration-test」で実行するテストクラスを同じく Build Setting で指定する。
以下の例では、テストクラス名に「SeleniumTest」が含まれているものをテスト対象としている。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>selenium-test</id> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <skip>false</skip> <excludes> <exclude>none</exclude> </excludes> <includes> <include>**/*SeleniumTest*.java</include> </includes> </configuration> </execution> </executions> </plugin>
あとは、コマンドプロプトから、当該プロジェクトのルートで、「mvn integration-test」を実行するだけ。
そうすれば、自動でサーバが起動し、テストが行われ、テストが終わればサーバが停止する。
ほんまステキやぁ〜ん。