天天看點

利用selenium + ant 實作自動化測試

selenium ThoughtWorks專門為Web應用開發的自動化測試工具,适合用于功能測試與驗收測試.也是近年來較為流行的開源測試工具.selenium這一強大的Web測試工具主要由selenium IDE, selenium core, selenium Remote Control 及selenium Grid組成. 其中selenium RC 應用較為廣泛亦算是功能最為強大的一個,通常它會與其它工具結合起來使用以更好地發揮其強大功能.現介紹建構工具Ant與selenium結合用于測試java應用項目.Ant+selenium可以建構測試build,然後部署到相應的測試環境中,再編譯執行生成測試報告,最後通過郵件發送測試報告.

對selenium 和ant 有所了解的同行應該看得出這個應用主要是使用Ant來建構版本,然後部署到測試環境中去,再然後執行測試腳本到生成發送測試報告.即是使用Ant這個強大的建構部署工具幫我們做那些外圍的工作,使測試跟開發完美結合,讓selenium 自動化測試應用完全自動化.是以這個應用主要是Ant的應用.

介紹下我當時使用情況.當時一個項目中使用selenium RC來做自動化測試,主要是冒煙測試跟回歸測試.我們測試所用的build是在香港開發部門每天使用Bamboo持續建構的,是以我們必須用Ant通過網絡從上面拿相應的build下來,修改相應的配置檔案,然後部署到相應的測試環境中去. 部署好測試系統後,我們就可以通過Ant編譯運作我們的測試腳本(用java寫的selenium RC 腳本).運作完成後用Ant自帶的方法來生成測試報告,最後還是能過Ant以郵件發送報告.是以,我們隻需要用滑鼠點一下,甚至不用點而用其它工具每天自動執行.

當然這也并不是什麼很高明的應用,但借助Ant強大功能,使用selenium自動化測試做得更為徹底,将測試各個步驟很好地連接配接起來,當然帶來更高效率.這個應用主要就是Ant的使用,難點也在Ant的使用,因為其中涉及到不少的Ant tasks.不過它官網上都有詳細的使用介紹:http://ant.apache.org/manual/index.html. 現貼出我之前使用的ant build 檔案,有興趣的同行可以參考下.

<?xml version="1.0"?>

<project name="camp-testuff" default="main">

<!-- properies -->

<property file="./resources/build.properties" />

<!-- 定義classpath -->

<path id="master-classpath">

<fileset file="${lib.dir}*.*" />

</war>

</target>

<!-- 部署項目 -->

<target name="deploy">

<mkdir dir="${tomcatlog.dir}" />

<mkdir dir="${tomcatconfig.dir}" />

<taskdef resource="cargo.tasks">

<classpath>

<pathelement location="${cargo-uberjar}" />

<pathelement location="${cargo-antjar}" />

</classpath>

</taskdef>

<cargo containerId="tomcat5x" home="D:\tomcat" ōutput="${tomcatlog.dir}/output.log" log="${tomcatlog.dir}/cargo.log" action="start" failοnerrοr="false" wait="false">

<configuration home="${tomcatconfig.dir}">

<property name="cargo.servlet.port" value="8080" />

<property name="cargo.logging" value="high" />

<property name="cargo.jvmargs" value="-Xmx500m"/>

<deployable type="war" file="${filePath.dir}/${projectfile}" />

</configuration>

</cargo>

</target>

<!-- 編譯腳本-->

<target name="compile" descrīption="compile the source files">

<mkdir dir="${classes.dir}" />

<javac srcdir="${src.dir}" failοnerrοr="false" destdir="${classes.dir}">

<include name="***ViewCustomerTest.*" />

<exclude name="***SubmittingMaintenanceApplicationTest.*" />

<exclude name="***EditMaintenance.*" />

<classpath refid="master-classpath" />

</javac>

</target>

<!-- 測試 Junit腳本-->

<target name="test" descrīption="run junit test">

<tstamp>

<format property="stratTime" pattern="MM/dd/yyyy HH:mm:ss" />

</tstamp>

<mkdir dir="${report.dir}" />

<junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">

<classpath refid="master-classpath" />

<formatter type="xml" />

<batchtest todir="${report.dir}">

<fileset dir="${src.dir}">

<include name="${testname}" />

<exclude name="***SubmittingMaintenanceApplicationTest.*"/>

<exclude name="***AddMaintenance.*"/>

<exclude name="***.*" />

</zip>

<mail mailhost="${host}" messagemimetype="${messagetype}"

mailport="${mailport}" subject="Test Report_${endTime3}"

user="${user}" password="${password}" messagefile="${messagefile}"

tolist="${tolist}" cclist="${cclist}">

<from name="${fromname}" address="${fromaddress}" />

<attachments>

<fileset dir="${report.dir}/html">

<include name="*Report.zip" />

</fileset>

</attachments>

</mail>

</target>

<!-- 清除生成檔案(報告) -->

<target name="clean">

<delete dir="${tomcatconfig.dir}" />

<delete dir="${report.dir}" />

<delete dir="${classes.dir}" />

<delete file="${filePath.dir}/project.war" />

<delete dir="${filePath.dir}/project" />

<sql driver="org.gjt.mm.mysql.Driver"

url="jdbc:mysql://${jdbc.urlAndPort}"

userid="${jdbc.username}"

password="${jdbc.password}"

>

drop schema ${jdbc.schema};

<classpath location="${lib.dir}/mysql-connector-java-5.0.5.jar"/>

</sql>

</target>

<!-- 測試完成關閉計算機 -->

<target name="shutDown">

<sleep minutes="2" />

<exec executable="shutdown">

<arg value="-s" />

</exec>

</target>

<!-- Stop tomcat -->

<target name="stoptomcat">

<taskdef resource="cargo.tasks">

<classpath>

<pathelement location="${cargo-uberjar}" />

<pathelement location="${cargo-antjar}" />

</classpath>

</taskdef>

<cargo containerId="tomcat5x" home="D:\tomcat" action="stop">

<configuration home="${tomcatconfig.dir}"/>

</cargo>

</target>

<!-- 主要目标 -->

<target name="main">

<!-- 清理并初始化環境 -->

<antcall target="clean" />

<antcall target="init" />

<!-- 部署項目 -->

<antcall target="war" />

<antcall target="deploy" />

<!-- 編譯腳本 -->

<antcall target="compile" />

<!-- 測試并發送報告 -->

<antcall target="SendEmail" />

<antcall target="stoptomcat" />

<antcall target="shutDown" />

</target>

</project>