selenium是一个非常棒的测试web应用的工具。特别是使用Selenium Remote Control (RC),你可以在多种操作系统下用多种浏览器测试你的web应用,并且可以使用多种语言编写testcase,当然包括Java。
参考: http://seleniumhq.org/
下文讨论了如何在maven2的环境下实现一键式的集成测试,进而可以使用持续集成工具完成自动化的持续集成。
测试环境:
ubuntu 8.10
JDK6
maven2
tomcat6
maven2的默认生命周期中的阶段中在package之后有:
pre-integration-test | 执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境 |
integration-test | 如果有必要的话,处理包并发布至集成测试可以运行的环境 |
post-integration-test | 执行一些在集成测试运行之后需要的动作。如清理集成测试环境。 |
那么要做的事情就很清楚了,
第一步:在pre-integration-test阶段,把package阶段打好的war包redeploy到tomcat。
把Selenium Remote Control(RC)启动起来
第二步:在integration-test阶段,运行selenium的test cases
第三步:在post-integration-test阶段,把Selenium Remote Control(RC)停止
我的pom.xml代码片段:
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://www.test.com:8080/manager</url>
<server>test</server>
<path>/</path>
</configuration>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>undeploy</goal>
<goal>deploy-only</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/demo/action/**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>selenium/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
tomcat-maven-plugin的配置请参考 http://blog.csdn.net/dqatsh/archive/2009/02/18/3905274.aspx
这里在配置文件中使用了execution把plugin的某个goal绑定到生命周期的某个阶段。
但对于maven-surefire-plugin这个plugin来说,它缺省就被绑定到test这个阶段了。由于其中的test cases既有单元测试的,又有selenium集成测试的,所以用include来过滤区分一下,保证不同的阶段运行所需要运行的测试。