诸如 Ant 这样的集成工具可以方便地构建 Selenium 测试和顺畅地运行测试用例,无需单独启动 Selenium 服务器。如果 Selenium 测试由 TestNG 驱动,那么定义清单 1所示 TestNG Ant 任务。清单 1中假设 classpath 是 TestNG.jar 文件的文件路径。
<taskdef resource="testngtasks" classpath="testng.jar"/> |
主要的目标是启动服务器、运行测试,然后停止服务器。这些任务按照 bulid.xml 中定义的顺序实现在清单 2 中。
清单 2. 启动服务器、运行测试用例并停止服务器的 Ant 任务
<target name="run_test" description="start,run and stop" depends="dist">
<parallel>
<antcall target="start-server" />
<sequential>
<echo taskname="waitfor" message="Waitforproxy server launch" />
<waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
<http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
</waitfor>
<antcall target="runTestNG" />
<antcall target="stop-server" />
</sequential>
</parallel>
</target>
|
代码更可取的地方是使用 waitfor 任务来测试 Selenium 服务器是否已成功启动,而不是暂停一段固定的时间。如果 URL http://localhost:4444/selenium-server/driver/?cmd=testComplete 可用,就意味着 Selenium 已经成功启动。在清单 2 中,它最多等待两分钟,并且每 100 毫秒在本地主机上检查一次 Selenium 服务器,以提供完整的 URL。
start-server 任务的详细内容定义在清单 8 中。Firefox profile 模板位置和其他参数可以指定在标记 <arg> 中。
<target name="start-server">
<java jar="lib/selenium-server.jar" fork="true">
<arg line="-firefoxProfileTemplate ${selenium}/profile/" />
</java>
</target>
|
runTestNG 任务的详细内容定义在清单 4 中。testng 任务的常用属性包括 outputDir 和 xmlfileset。属性 outputDir 用于设置输出报告位置。属性 xmlfileset 用于包含启动 XML 文件。更多选项请参考 TestNG 正式网站。
<target name="runTestNG">
<testng outputDir="${testng.report.dir}" sourcedir="${build}"
classpathref="run.cp" haltOnfailure="true">
<xmlfileset dir="${build}" includes="testng.xml" />
</testng>
</target>
|
stop-server 任务的详细内容定义在清单 5 中。
<target name="stop-server"> <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown" ignoreerrors="true" /> <echo taskname="selenium-shutdown" message=" Errors during shutdown are expected" /> </target> |
上面列出了关键任务。将它们组合到您的构建文件,以便利用 Ant 完成良好集成的测试。
423

被折叠的 条评论
为什么被折叠?



