<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>
<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输出的信息可以当做是调试用的
<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>
<target name="selenium.clean"> <delete dir="${selenium.dir}/dist" failonerror="false"/> <delete dir="${selenium.dir}/test-output" failonerror="false"/> </target>
<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>
<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>
<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完成了.