如何在Ant的build.xml里面正确配置TestNG呢?
1. 在build.xml里面定义testng任务,在classpath里面指定testng.jar
<taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/>
2. 在build.xml里面新建一个 叫regression的target
<project basedir="." default="regression" name="automation test"> <property name="base.dir" value="/home/maguschen/workspaces/automation"/> <property name="testng.output.dir" value="${base.dir}/test-output"/> <property name="3rd.lib.dir" value="${base.dir}/libs"/> <property name="testng.file" value="testng.xml"/> <taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/> <target name="clean"> <delete dir="${base.dir}/bin"/> </target> <target name="compile" depends="clean"> <mkdir dir="${base.dir}/bin"/> <javac srcdir="${base.dir}/src" destdir="${base.dir}/bin" classpathref="classes" includeantruntime="off" debug="on" debuglevel="lines,vars,source"/> </target> <path id="classes"> <fileset dir="${3rd.lib.dir}" includes="*jar"/> <fileset dir="${3rd.lib.dir}" includes="*zip"/> <pathelement location="${base.dir}/bin"/> </path> <target name="regression" depends="compile"> <testng outputdir="${testng.output.dir}" classpathref="classes" delegateCommandSystemProperties="true"> <xmlfileset dir="${base.dir}" includes="${testng.file}"/> </testng> </target> </project>
在target里面新建一个testng标签,里面需要设置的属性有:outputdir – 测试结果输出目录;classpathref – 那些自动化测试代码的目标路径,通常就是编译完成以后的那个目标路径,例如 xxx/bin;delegateCommandSystemProperties – 接受传递命令行参数作为系统变量,这个设置为true可以在调用Ant的时候通过 -Dfoo=value 把参数传递给TestNG;里面还有一个xmlfileset节点,这个节点就是指定testng.xml文件的目录以及具体文件。
regression 的 target 有一个depends属性,意思就是跑regression之前需要做compile,而跑compile之前需要clean,应该很容易理解。直接在命令行里面运行:
ant -Durl=http://www.google.com -f build.xml regression
这里出现了 -Durl=http://www.google.com ,回到之前的配置,delegateCommandSystemProperties=”true”。如果这个参数为true,那么通过命令行的 -D 参数可以把一些变量传递给TestNG。譬如说TestNG的测试方法里面是有@Parameters({“url”})标签的话,就能通过ant -Durl=xxx 来传递url的值给到TestNG。例如
@Parameters({"url"}) @Test public void search(String url){ WebDriver driver = new FirefoxDriver(); driver.get(url); WebElement query = driver.findElement(By.name("q")); query.sendKeys("Cheese"); query.submit(); }
如果这样调用:ant -Durl=http://www.google.com -f build regression 。那么就会进入google的首页搜索,如果是: ant -Durl=http://magustest.com -f build regression ,那么就会找不到叫“q”的元素,呵呵。
接下来只要把cron job配好就完成了
15 * * * * ant -f /home/maguschen/workspaces/automation/build.xml regression