自动化工具 解决 Ant 邮件发送 Jmter 测试报告乱码问题

test_ray · 2017年04月14日 · 1149 次阅读

Guide

写这篇文章的目的是让更多人通过 Ant 实现邮件发送 html 报告时,避免邮件正文乱码误区。

过程

最开始,步骤如下
1、选择 apache-ant-1.8.0+apache-jmeter-3.1 组合,具体步骤就不说了,网上很多
2、Ant 编译,build.xml 中 target sendEmail 模块代码如下:

<property name="mail_to" value="test1@qq.com,test2@163.com" /> 
<target name="sendEmail">
    <mail mailhost="smtp.qq.com" 
            mailport="465" 
            subject="Test sendmail" 
            messagefile="${jmeter.result.html.dir}/${ReportName}${time}.html"
                messagemimetype="text/html" 
                user="86********@qq.com"  
                password="fhiu********"  
                from="86********@qq.com"
                tolist="${mail_to}"
                charset="UTF-8"
                ssl="true">

           <fileset dir="D:\apache-jmeter-3.1\demo\report\html">
                        <include name="*.png"/>
                <include name="${ReportName}${time}.html"/>
        </fileset>
    </mail>
</target> 

关键步骤:messagefile="${jmeter.result.html.dir}/${ReportName}${time}.html",实现在 Email 插入 HTML 文件
很奇葩的是,jmeter 生成的 HTML 测试报告,里面包含很多中文字符,比如某些接口的返回值就是中文,总不能让领导看一堆乱码吧!
邮件虽然发送成功,但苦思冥想,搜尽资料也找不到解决办法。

解决办法

其实原因很简单,我用的 apache-ant-1.8.0 存在这么一个 Bug,Ant 官方文档也给出如下描述(http://ant.apache.org/manual/Tasks/mail.html

apache-ant-1.8.0 不支持 messagemimetype 参数,这就是导致乱码的终极原因。
messagemimetype:Specifies the encoding of the input file. Please see Supported Encodings for a list of possible values. Defaults to the platform's default character encoding. Since Ant 1.9.4
重新下载安装 apache-ant-1.9.9,在 build.xml 添加此参数后,一切都搞定!

<property name="mail_to" value="test1@qq.com,test2@163.com" /> 
<target name="sendEmail">
    <mail mailhost="smtp.qq.com" 
            mailport="465" 
            subject="Test sendmail" 
            messagefile="${jmeter.result.html.dir}/${ReportName}${time}.html"
                messagemimetype="text/html" 
                        messagefileinputencoding="UTF-8"
                user="86********@qq.com"  
                password="fhiu********"  
                from="86********@qq.com"
                tolist="${mail_to}"
                charset="UTF-8"
                ssl="true">

           <fileset dir="D:\apache-jmeter-3.1\demo\report\html">
                        <include name="*.png"/>
                <include name="${ReportName}${time}.html"/>
        </fileset>
    </mail>
</target> 

最后,是不是清爽多了!!!

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册