近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下。感觉很不错。对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正常,分享给大家。

WetTest工作原理

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

通过执行不同的target,測试页面上面提供的全部功能。它的工作原理是运用比較出名的HtmlUnit来实现对一个页面功能的測试。

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

WetTest与Maven的集成配置

Maven的配置

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

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

详细配置例如以下:

在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>

因为一般的測试都离不开这个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)

最新文章

  1. haproxy的使用
  2. 使用dom4j解析XML文档
  3. c# 基本值类型及其默认值
  4. 使用XtraGrid自定义列计算1 z
  5. 【Andorid开发框架学习】之Mina开发之客户端开发
  6. python Django 学习笔记(六)—— 写一个简单blog做增删改练手
  7. 使用rsyslog+loganalzey收集日志显示客户端ip
  8. js compress and combine
  9. robots.txt网站爬虫文件设置
  10. UVALive 6959 - Judging Troubles
  11. CSS div阴影效果
  12. 9、vuex快速上手
  13. C# Note11:如何优雅地退出WPF应用程序
  14. VS插件File Nesting
  15. spring 项目分开发和生产环境
  16. C++11--20分钟了解C++11 (上)
  17. js-ES6学习笔记-Generator函数的异步应用
  18. .Net 环境
  19. docker使用示例
  20. WinRAR打包时不包含文件夹本身,只打包文件夹里的文件和目录

热门文章

  1. IRQ中断处理流程
  2. cocos2d-x游戏开发(一)之环境搭建篇
  3. Verilog学习笔记基本语法篇(八)&#183;&#183;&#183;&#183;&#183;&#183;&#183;&#183; 结构说明语句
  4. python基础——10(三元运算符、匿名函数)
  5. [android篇]声明权限
  6. 九度oj 题目1209:最小邮票数
  7. python 字典 key 和value 互换
  8. 【bzoj2597】[Wc2007]剪刀石头布 动态加边费用流
  9. 【kmp+最小循环节】poj 2406 Power Strings
  10. R 包安装、载入和卸载