標籤:webtest 整合測試 maven
自連結
最近在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的測試案例。具體配置如下:
<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> <loglevel>fatal</loglevel> </configuration> </plugin>WebTest用例檔案(Web-tests.xml)配置
?xml version="1.0" encoding="UTF-8"?><pre name="code" class="html"><!-- 匯入 config & login xmlf -->
<!DOCTYPE project [ <!ENTITY config SYSTEM "./config.xmlf"> <!ENTITY login SYSTEM "./login.xmlf">]>
<pre name="code" class="html"><!-- 定義預設跑的target -->
<project basedir="." default="run-all-tests">
<!-- 在ant中引入webtest標籤 -->
<taskdef resource="webtestTaskdefs.properties" /> <!-- 引入Regex: allows to build a message string with parameter replacement: "User {0} successfully created" --> <taskdef resource="net/sf/antcontrib/antcontrib.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">
<pre name="code" class="html"> <!-- 先執行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=".*${system}.*" 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"/> <verifytitle description="we should see the user profile title" text=".*${userProfile.title}.*" regex="true"/> </steps> </webtest> </target></project>
由於一般的測試都離不開這個Login介面,所以把Login的target抽出了,還有串連伺服器的配置config任務也可以抽出來放成兩個 單獨的檔案了。
login.xmlf:登陸頁面具體操作
<span style="color:#3333FF;"><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"/><verifytitle description="Home Page follows if login ok" text=".*${welcome}.*" regex="true"/></span>config.xmlf:webtest的配置,使用webtest-maven-plugin中configuration值做為輸入參數一部分
<span style="color:#3333FF;"><config host="${host}" port="${port}" protocol="http" basepath="${basepath}" resultpath="${resultpath}" saveresponse="true" resultfile="web-tests-result.xml" haltonfailure="${haltonfailure}" haltonerror="${haltonerror}" summary="true"> <header name="Accept-Language" value="${user.language}"/> <option name="ThrowExceptionOnScriptError" value="true"/></config></span>
附:Webtest Maven Plugin連結
WebTest的官方連結網址
使用實現Web應用整合功能性測試 -- WebTest & Maven