selenuim+testng集成ant和reportNG自动执行和发送测试报告

本文介绍如何使用Selenuim与TestNG结合Ant构建自动化测试框架,并利用ReportNG生成测试报告。主要内容包括配置环境变量、定义Ant构建文件、执行测试并发送邮件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值