利用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}/*.jar"/>
<pathelement path="${classes.dir}" />
<pathelement path="${resources.dir}" />
<pathelement path="${src.dir}" />
</path>

<!-- 初始化任务 -->
<target name="init">
<java jar="${selenimhome}/selenium-server.jar" fork="true" spawn="true" />
</target>

<!--下载更新build-->
<target name="download" if="isdowload">
<get src="${targeturl}" dest="${filePath.dir}/${filename}" verbose="true" usetimestamp="true" />
</target>


<!--更新编绎war文件-->
<target name="war" depends="download">
<unwar src="${filePath.dir}/${filename}" dest="${filePath.dir}/project" />
<propertyfile file="${filePath.dir}/project/WEB-INF/classes/jdbc.properties" comment="My properties">
<entry key="jdbc.url" value="${jdbc.url}" />
<entry key="jdbc.username" value="${jdbc.username}" />
<entry key="jdbc.password" value="${jdbc.password}" />
</propertyfile>

<propertyfile file="${filePath.dir}/project/WEB-INF/classes/systemConfiguration.properties" comment="My properties">
<entry key="mail.host" value="${mail.host}" />
</propertyfile>

<war destfile="${filePath.dir}/${projectfile}" needxmlfile="false">
<fileset dir="${filePath.dir}/project" includes="**/*.*" />
</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="**/*.*" />
<exclude name="**/*ViewCustomerTest.*" />
<exclude name="**/*SubmittingMaintenanceApplicationTest.*" />
<exclude name="**/*SubmittingMaintenanceApplicationTest.*" />
<exclude name="**/*AddMaintenance.*" />
<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="**/*ViewCustomerTest.*"/>
<exclude name="**/*SubmittingMaintenanceApplicationTest.*"/>
<exclude name="**/*SubmittingMaintenanceApplicationTest.*"/>
<exclude name="**/*AddMaintenance.*"/>
<exclude name="**/*EditMaintenance.*"/>

</fileset>
</batchtest>
</junit>
</target>

<!-- 生成报告 -->
<target name="junitreport" depends="test">
<mkdir dir="${report.dir}/html" />
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}" includes="TEST-*.xml" />
<report format="frames" todir="${report.dir}/html" />
</junitreport>
</target>

<!-- 发送测试结果 -->
<target name="SendEmail" depends="junitreport">
<tstamp>
<format property="endTime" pattern="MM/dd/yyyy HH:mm:ss " />
<format property="endTime2" pattern="MM/dd/yyyy " />
<format property="endTime3" pattern="yyyyMMddHHmmss " />
</tstamp>
<zip destfile="${report.dir}/html/Report.zip">
<zipfileset dir="${report.dir}/html" includes="**/*.*" />
</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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值