之前一篇博客分享了如何使用WebDriver+TestNG实现UI自动化,现在就要让自动化测试自己跑起来,不需要人工干预。需要用到Ant,以及一些定时任务工具,例如Linux的crontab。
如何在Ant的build.xml里面正确配置TestNG呢?
1. 在build.xml里面定义testng任务,在classpath里面指定testng.jar
2. 在build.xml里面新建一个 叫regression的target
在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
成功了~ 谢谢分享
LikeLike
不客气 :)
LikeLike
good for me!
LikeLike
TestNG+Maven should be better than TestNG+Ant, we can easily configure the testNG in pom.xml, just like this.
org.apache.maven.plugins
maven-surefire-plugin
2.4
testng.xml
but thanks for sharing this.
LikeLike
correct the configuration as below:
org.apache.maven.plugins
maven-surefire-plugin
2.4
testng.xml
LikeLike
最后的运行是在linux环境下么,看cron job里的设置象是在linux 系统下,如果是在windows下如何定时运行?我个人认为通过任务计划执行。大家认为呢?
LikeLike
用Hudson或者Jenkins
LikeLike