selenuim+testng集成ant和reportNG
selenuim+testng+ant
1、安装ant,配置环境变量,这个可以自行百度
2、在Myeclipse中配置本机的ant
3、在根目录下新增build,代码参考如下,包含了发邮件的部分,可以自行修改邮件内容:
其中注意下default写最后一个任务的名字,然后如果sendEmail需要依赖于上一个步骤,那需要配置depends“”,否则执行就会出错。
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestNG" default="sendEmail" basedir=".">
<property name="srcdir" location="${basedir}/src" />
<property name="libdir" location="${basedir}/lib" />
<property name="testng.output.dir" location="${basedir}/tes t-output" />
<property name="testng.file" value="testng.xml" />
<property name="report" value="e:\Users\Administrator\Workspaces\MyEclipse Professional\Demo\test-output\html\index.html" />
<property name="anthome" location="C:\apache-ant-1.9.7\lib" />
<path id="classpath">
<fileset dir="${libdir}" includes="*jar" />
<fileset dir="${libdir}" includes="*zip" />
<pathelement location="${basedir}/bin" />
</path>
<!-- Define <testng> task -->
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="./lib/testng-6.8.5.jar" />
</classpath>
</taskdef>
<!-- Before compile,delete the previous folder -->
<target name="clean">
<delete dir="${basedir}/bin" />
</target>
<!-- Compile file in 'srcdir' to 'destdir' -->
<target name="compile" depends="clean">
<mkdir dir="${basedir}/bin" />
<javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" classpathref="classpath" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />
</target>
<!-- use testng.xml to run the test -->
<target name="runtest" depends="compile">
<testng outputdir="${testng.output.dir}" classpathref="classpath">
<xmlfileset dir="${basedir}" includes="${testng.file}" />
</testng>
</target>
<!-- change to your mail address -->
<path id="lib_classpath">
<fileset dir="${anthome}">
<include name="mail*.jar" />
<include name="activation*.jar" />
<include name="commons-email*.jar" />
<include name="ant-contrib*.jar" />
</fileset>
</path>
<target name="sendEmail" depends="runtest">
<mail mailhost="smtp.XXXX.com" mailport="25" user="XXXX" password="XXXXX" ssl="false" from="g1@XX.com" subject="UI自动化冒烟测试报告" messageMimeType="text/html" tolist="g1@XX.com,g2@XX.com">
<fileset dir="${testng.output.dir}">
<include name="emailable-report.html" />
</fileset>
<message charset="gb2312">
<![CDATA[
<p>
UI自动化冒烟测试报告
</p>
<pre>
<a href= "${report}">暂时未开通此功能!</a>
</pre>
<p>落款</p>
]]>
</message>
</mail>
</target>
</project>
4、如何执行?
第一种方式,在Myeclipse中,打开build.xml,右键Run As –》Ant Build
第二种方式,如果是配置了selenium-grid,在note端或者hub端,build.xml所在路径执行用命令行执行ant即可启动执行
reportNG
1、引入reportNG的jar包,reportng-1.1.4,在网上下载即可,但是这个版本不支持中文,
有reportng-1.1.5版本是支持中文的,可以百度自行下载。包含:reportng-1.1.4和velocity-dep-1.4
2、在build中进行配置,参考上面的build.xml。执行完build.xml test-output\html\index.html 查看报告
reportNG返回的报告既有.js的也有.css的,如果直接作为附件发送邮件,会被拦截。所以最后想的办法是发送的testNG自带的emailable-report.html,样子比较丑,如果想看好看的,可以点击链接查看。
3、为啥没有选择“testng-xslt-1.1”这个生成报告,是因为在build.xml执行的时候总是报错,找不到if函数,上网无法找到解决方案。,所以改为使用reportNG。