Selenium自动化测试的Build.xml例子
刚刚开始学习Ant,Billy同学写的,下午仔细看了一下,并且将每个Target都独立运行了一下:
定义全局变量
<property name="selenium.dir" value="../MatrixAutomatedTesting-Trunk/" /><property name="selenium.src.dir" value="${selenium.dir}" /><property name="selenium.dist.dir" value="${selenium.dir}/dist" /><property name="selenium.lib.dir" value="${selenium.dir}/lib" /><property name="selenium.suite.dir" value="${selenium.dir}" /><path id="selenium.classpath.main"><pathelement location="." /><pathelement location="${selenium.dist.dir}" /><fileset dir="${selenium.lib.dir}/"><include name="**/*.jar" /></fileset></path>启动Selenium Server
<target name="start_selenium_server"><echo message="starting selenium server" /><java jar="${selenium.lib.dir}/selenium-server.jar" fork="true" spawn="false" output="selenium.log"><arg line="-port 4444" /></java></target>echo输出的信息可以当做是调试用的
停止Selenium Server
<target name="stop_selenium_server"><echo message="going to stop the selenium server" /><get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" dest="stop.out" ignoreerrors="true" /></target>
clean
<target name="selenium.clean"><delete dir="${selenium.dir}/dist" failonerror="false"/><delete dir="${selenium.dir}/test-output" failonerror="false"/></target>编译测试代码,并且Copy资源文件
<target name="selenium.compile"><echo message="starting to compile the classess" /><!--clean the old classes--><delete dir="${selenium.dist.dir}" failonerror="false" /><!--create new dist dir--><mkdir dir="${selenium.dist.dir}/com/perficient/properties/" /><!--compile--><javac classpathref="selenium.classpath.main" srcdir="${selenium.src.dir}" destdir="${selenium.dist.dir}" /><copy todir="${selenium.dist.dir}/com/perficient/properties/"><fileset dir="${selenium.src.dir}/src/com/perficient/properties/" /></copy><copyfile dest="${selenium.dist.dir}/log4j.properties" src="${selenium.src.dir}/src/log4j.properties" /></target>用TestNG执行测试过程
<taskdef resource="testngtasks" classpath="${selenium.lib.dir}/testng-jdk15.jar" /><target name="selenium.test"><testng classpathref="selenium.classpath.main" failureproperty="test.failed"><jvmarg value="-Dselenium.port=4444" /><!--xml test suite file --><xmlfileset dir="${selenium.suite.dir}"><include name="MatrixSuite.xml" /></xmlfileset></testng><antcall target="stop_selenium_server" /></target>Default的Target,也是最关键的部分
<target name="start_server_and_run_tests" depends="selenium.clean, selenium.compile" description="start selenium server and run tests"><parallel><antcall target="start_selenium_server" /><sequential><echo taskname="waitfor" message="wait for selenium server launch" /><waitfor maxwait="2" maxwaitunit="minute" checkevery="10"><http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" /></waitfor><antcall target="selenium.test"></antcall></sequential></parallel></target>
最后,在项目的根节点加入Default target:
<project name="selenium automation" default="start_server_and_run_tests" basedir=".">
这样一个Build.xml完成了.