Ranorex通过Python如何将报告发送到邮箱。

     Ranorex测试报告如何发送到邮箱在网上看了下,其实可以通过在Ranorex上或者VS调用编写发送邮箱代码就可以执行发送了,RX主要涉及到的开发语言是C++或者.NET。但是我想用Python调用并发送,涉及到的应用以及范围会比较麻烦。因此,希望有广大猿友能够给点意见指点一二。

首先将Ranorex测试解决方案在Pycharm打开。

然后新建一个文件夹用来放Python发送邮件的CODE。

'''发送给********@163.com'''
from email.mime.application import MIMEApplication
import smtplib
import os


def send_email(new_log):
    '''
    发送邮箱
    :param new_log: 最新的报告
    :return:
    '''

    file = open(new_log, 'rb')
    mail_content = file.read()
    file.close()

    # 发送方用户信息
    send_user = '********@qq.com'
    send_password = '********'

    # 发送和接收
    sendUser = '********@qq.com'
    receive = '********@163.com'

    # 邮件内容
    send_subject = 'Ranorex自动化测试报告'
    msg = MIMEApplication(mail_content, 'rb')
    msg['Subject'] = send_subject
    msg.add_header('Content-Disposition', 'attachment', filename=new_log)

    try:

        # 登录服务器
        smt = smtplib.SMTP('smtp.qq.com')

        # helo 向服务器标识用户身份
        smt.helo('smtp.qq.com')
        # 服务器返回确认结果
        smt.ehlo('smtp.qq.com')

        smt.login(send_user, send_password)
        print('正在准备发送邮件。')
        smt.sendmail(sendUser, receive, msg.as_string())
        smt.quit()
        print('邮件发送成功。')

    except Exception as e:
        print('邮件发送失败:', e)


def new_report(report_dir):
    '''
    获取最新报告
    :param report_dir: 报告文件路径
    :return: file ---最新报告文件路径
    '''

    # 返回指定路径下的文件和文件夹列表。
    lists = os.listdir(report_dir)
    listLog = []
    # print(lists)
    for i in lists:
        if os.path.splitext(i)[1] == '.rxlog':
            # print(len(i))
            # print(i)
            listLog.append(i)
    # print(listLog)
    # print(listLog[-1])
    fileNewLog = os.path.join(report_dir, listLog[-2])
    return fileNewLog


if __name__ == '__main__':
    # 报告路径
    test_report = r'D:\学习笔记\Ranorex\Text\1105\text02\text02\Reports'
    # 获取最新测试报告
    newLog = new_report(test_report)
    # 发送邮件报告
    send_email(newLog)

运行后,邮件发送成功。

在Windows上,Ranorex报告打开后结果显示错误。

      自己尝试在Ranorex解决方案中将一份报告复制粘贴到桌面上,打开也是以上图的错误,原因可能需要在Ranorex解决方案中的环境条件,所以即使发送了也没什么用处,只能提醒Ranorex解决方案已经运行结束。

      最后还是在Ranorex上编写脚本发送邮箱最方便。

发布了13 篇原创文章 · 获赞 14 · 访问量 907

猜你喜欢

转载自blog.csdn.net/qq_39979646/article/details/103933737
今日推荐