Maven实现Web应用集成測试本身主动化 -- 測试本身主动化(WebTest Maven Plugin)

近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下。感受很不错。对于Web应用本身主动构建颇有帮助,在性能測试以前可以保证Web应用的基本功能工做正常,分享给你们。html

WetTest工做原理

它是基于Ant来执行的Web页面的測试工具。java

经过执行不一样的target,測试页面上面提供的所有功能。它的工做原理是运用比較出名的HtmlUnit来实现对一个页面功能的測试。jquery

它的工做流程就是模拟一个浏览器的事件(页面提供的功能:可以调用一个Url,可以点击一个button,label等,可以为页面上的元素赋值),而后经过抓取返回的页面上的Title或者是element的值来校验是否返回预期的结果。web


WetTest与Maven的集成配置

Maven的配置

在Web应用的pom.xml中引入webtest-maven-plugin,定义集成測试阶段运行測试,验证阶段运行结果验证,系统集成測试以后生成报告。spring

同一时候指定Web应用的地址,測试用例所在文件,生成文件所在路径,日志级别以及遇到错误的时候採取的策略。这样在Maven构建阶段就会本身主动运行WebTest的測试用例。apache

详细配置例如如下:api

在build节点增长webtest插件  (插件详细參数參考
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>webtest-maven-plugin</artifactId>
                        <version>1.0.0</version>
                        <executions>
                            <execution>
                                <id>webtest-test</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>webtest-verify</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>verify-result</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>webtest-report-html</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <host>${project.cargo.host}</host>
                            <port>${project.cargo.port}</port>
                            <sourcedirectory>src/test/resources</sourcedirectory>
                            <sourcefile>web-tests.xml</sourcefile>
                            <target>${project.webtest.target}</target>
                            <basepath>${project.build.finalName}</basepath>
                            <resultpath>target/webtest/webtest-results</resultpath>
                            <resultpath>target/webtest/webtest-results</resultpath>
                            <haltonfailure>false</haltonfailure>
                            <haltonerror>false</haltonerror>
<pre name="code" class="html">                            <autorefresh>true</autofresh>
                            <loglevel>fatal</loglevel>
                        </configuration>
                    </plugin>
 
  

WebTest用例文件(Web-tests.xml)配置

 
   
  
?

xml version="1.0" encoding="UTF-8"?浏览器

> <!-- 导入 config & login xmlf --> <!DOCTYPE project [ <!ENTITY config SYSTEM "./config.xmlf"> <!ENTITY login SYSTEM "./login.xmlf"> ]> <!-- 定义默认跑的target --> <project basedir="." default="run-all-tests"> <!-- 在ant中引入webtest标签 --> <taskdef resource="webtestTaskdefs.properties" /> <!-- web系统很是多都是多语言的。作页面验证的时候也需要多语言支持, 第二个找不到其它语言时候的默认语言 --> <property file="../../../target/classes/ApplicationResources_${user.language}.properties"/> <property file="../../../target/classes/ApplicationResources.properties"/> <!-- 定义 runs all targets。依赖系统中各个功能模块,登录。登出。用户操做--> <target name="run-all-tests" depends="Login,Logout,UserTests" description="Call and executes all test cases (targets)"/> <!-- 定义runs user-related tests,RUD,系统不存在用户Create功能 --> <target name="UserTests" depends="EditUser,SearchUser,SaveUser" description="Call and executes all user test cases (targets)"> <echo>Successfully ran all User UI tests!</echo> </target> <!-- 登录測试,Login to the application --> <target name="Login" description="Runs login test and verifies Home's Title"> <!-- 定义登录測试的webtest详细内容 --> <webtest name="login"> <!-- 先运行webtest配置 --> &config; <!-- 详细測试步骤,来自login.xml --> <steps> &login; </steps> </webtest> </target> <!-- Logout of the application --> <target name="Logout" description="Runs logout test and verifies Login's Title"> <webtest name="logout"> &config; <steps> &login; <invoke description="get Logout Page" url="/j_security_logout"/> <verifytitle description="we should see the login title" text=".*${<span style="font-size:14px;">login.service</span>}.*" regex="true"/> </steps> </webtest> </target> <!-- Verify the edit user screen displays without errors --> <target name="EditUser" description="Tests selecting the 'Edit Profile' forward"> <webtest name="editUser"> &config; <steps> &login; <invoke description="click Edit Profile button" url="/userInfo/save.action"/> <verifytitle description="we should see the user profile title" text=".*${userProfile.title}.*" regex="true"/> </steps> </webtest> </target> </project>oracle

由于通常的測试都离不开这个Login界面,因此把Login的target抽出了,还有链接server的配置config任务也可以抽出来放成两个 单独的文件了。login.xmlf:登录页面详细操做
<invoke description="get Login Page" url="/"/>
<verifytitle description="we should see the login title" text=".*${system}.*" regex="true"/>
<setinputfield description="set user name" name="j_username" value="admin"/>
<setinputfield description="set password" name="j_password" value="password"/>
<clickbutton label="${login.submit}" description="Click the submit button"/>
<verifytext description="Home Page follows if login ok" text=".*${welcome}.*" regex="true"/>
config.xmlf:webtest的配置,使用webtest-maven-plugin中configuration值作为输入參数一部分
<config host="${host}" port="${port}" protocol="http"
    basepath="${basepath}" resultpath="${resultpath}" saveresponse="true"
    resultfile="web-tests-result.xml" haltonfailure="${haltonfailure}"
    haltonerror="${haltonerror}" autorefresh="${autorefresh}">
    <header name="Accept-Language" value="${user.language}"/>
    <option name="ThrowExceptionOnScriptError" value="true"/>
</config>

配置web-tests.xml的细节可以看:官方manual
当心地雷:
1. webtest-maven-plugin很是久木有更新了。里面依赖的htmlunit的版本号太旧了。以前我直接使用plugin自带的依赖htmlunit2.8直接致使測试挂死,jstack dump出来的线程是running状态,把log改为debug也看不出来为何挂死,幸亏看到appfuse里面说更新了htmlunit的版本号解决和jquery的兼容性问题。这是个很是大的坑,必定要去htmlunit看看它兼容什么js,或者在appfuse里面看也行。
2. 在測试的时候遇到了下面异常。当时觉得是语法写错了,研究了一阵子才发现是resource file中未定义login.title,定义被改为了login.service,这里不是语法问题。。

。详细复杂配置语法參考java reg官方文档
java.util.regex.PatternSyntaxException: Illegal repetition near index 2
.*${login.title}.*
3. 最開始没有把autorefresh打开,结果login page spring security3默认是回给client一个自刷新页面,致使測试失败

下篇:Maven实现Web应用集成測试本身主动化 -- 部署本身主动化(Cargo Maven Plugin)
app

相关文章
相关标签/搜索