Maven实现Web应用集成测试自动化 -- 测试自动化(WebTest Maven Plugin)
来源:互联网 发布:阿里百秀 seo 编辑:程序博客网 时间:2024/06/07 09:45
最近在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抽出了,还有连接服务器的配置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默认是回给客户端一个自刷新页面,导致测试失败
下篇:Maven实现Web应用集成测试自动化 -- 部署自动化(Cargo Maven Plugin)
3 0
- Maven实现Web应用集成测试自动化 -- 测试自动化(WebTest Maven Plugin)
- Maven实现Web应用集成测试自动化 -- 部署自动化(WebTest Maven Plugin)
- Maven实战(五)自动化Web应用集成测试
- Maven实战(五)——自动化Web应用集成测试
- Maven实战(五)——自动化Web应用集成测试
- Maven实战(五)——自动化Web应用集成测试
- 学习笔记——Maven实战(五)自动化Web应用集成测试
- Maven jenkins +Jmeter自动化测试
- Jenkins+maven+jmeter接口可持续集成自动化测试
- Jenkins+maven+jmeter接口可持续集成自动化测试
- 用maven自动化测试java web应用程序页面的配置
- Web自动化测试四:eclipse中maven的使用
- jenkins+maven+svn搭建自动化部署测试
- 自动化测试入门教程(Maven + TestNG + Selenium)
- Intellij IDEA maven配置执行自动化测试
- 自动化在集成测试中的应用
- AngularJS集成自动化测试
- maven自动化部署插件sshexec-maven-plugin
- 闭包
- Python之一些小知识点
- 浅谈 Adaboost 算法
- javascript之博客前端连缀,css自定义封装代码笔记
- 基于案例推理技术的研究与应用(case based reasoning)
- Maven实现Web应用集成测试自动化 -- 测试自动化(WebTest Maven Plugin)
- HDOJ求绝对值
- 设计模式之命令模式-command
- HDOJ计算球体积
- Unique Paths II
- 《数据结构》实验一: VC编程工具的灵活使用
- shell编程(1)
- 快速幂运算
- Lua中的迭代器