zoukankan      html  css  js  c++  java
  • Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在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)

  • 相关阅读:
    关于update set from
    C#元组 Pair Triplet Tuple
    JCarouselLite滚动bug的修护
    Asp.net请求处理之 管道处理
    用socket来代替HttpWebRequest和HttpWebResponse
    web优化之Asp.net MVC js、css动态合并 动态压缩
    CScript 错误: 无法找到脚本“...js”的脚本引擎“JScript”
    Web优化 页面性能优化 自定义函数延迟触发 jquery插件
    纠正网上流传的SQL取某一时间的当月第一天和最后一天的时间写法
    C# Expression 表达式树 缓存
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7209665.html
Copyright © 2011-2022 走看看