如何使用Python通过Ranorex将测试报告自动发送至指定邮箱?

2026-06-09 20:014阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计657个文字,预计阅读时间需要3分钟。

如何使用Python通过Ranorex将测试报告自动发送至指定邮箱?

Ranorex测试报告发送至邮箱,网上查看方法如下:通过Ranorex或VS调用编写发送邮件代码即可执行发送。Ranorex主要涉及C++或.NET开发语言。但您想用Python调用并发送邮件,以下是一个简化的Python脚本示例:

pythonimport smtplibfrom email.mime.text import MIMETextfrom email.header import Header

def send_email(subject, content, to_email): sender='your_email@example.com' password='your_password'

msg=MIMEText(content, 'plain', 'utf-8') msg['From']=Header(sender) msg['To']=Header(to_email) msg['Subject']=Header(subject)

try: smtp_obj=smtplib.SMTP('smtp.example.com', 587) smtp_obj.starttls() smtp_obj.login(sender, password) smtp_obj.sendmail(sender, [to_email], msg.as_string()) print(邮件发送成功) except smtplib.SMTPException as e: print(无法发送邮件,错误信息:, e) finally: smtp_obj.quit()

使用示例subject='Ranorex测试报告'content='请查看Ranorex测试报告'to_email='recipient@example.com'send_email(subject, content, to_email)

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

如何使用Python通过Ranorex将测试报告自动发送至指定邮箱?

首先将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上编写脚本发送邮箱最方便。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计657个文字,预计阅读时间需要3分钟。

如何使用Python通过Ranorex将测试报告自动发送至指定邮箱?

Ranorex测试报告发送至邮箱,网上查看方法如下:通过Ranorex或VS调用编写发送邮件代码即可执行发送。Ranorex主要涉及C++或.NET开发语言。但您想用Python调用并发送邮件,以下是一个简化的Python脚本示例:

pythonimport smtplibfrom email.mime.text import MIMETextfrom email.header import Header

def send_email(subject, content, to_email): sender='your_email@example.com' password='your_password'

msg=MIMEText(content, 'plain', 'utf-8') msg['From']=Header(sender) msg['To']=Header(to_email) msg['Subject']=Header(subject)

try: smtp_obj=smtplib.SMTP('smtp.example.com', 587) smtp_obj.starttls() smtp_obj.login(sender, password) smtp_obj.sendmail(sender, [to_email], msg.as_string()) print(邮件发送成功) except smtplib.SMTPException as e: print(无法发送邮件,错误信息:, e) finally: smtp_obj.quit()

使用示例subject='Ranorex测试报告'content='请查看Ranorex测试报告'to_email='recipient@example.com'send_email(subject, content, to_email)

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

如何使用Python通过Ranorex将测试报告自动发送至指定邮箱?

首先将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上编写脚本发送邮箱最方便。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。