【Selenium + Python】自动化测试之发送邮件正文以及附件同时发送
废话不多说,直接上代码:
import unittest
import time
import os
import smtplib
from HTMLTestRunner import HTMLTestRunner
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from models import driver def new_report(report_dir):
'''
:param report_dir:报告路径
:return:返回最新的文件
'''
#获取路径下的文件
lists = os.listdir(report_dir)
#按照时间顺序排序
lists.sort(key=lambda fn: os.path.getmtime(report_dir + fn))
#获取最近时间的
new_report = os.path.join(report_dir,lists[-1])
return new_report def send_mail(new_report,new_report_fail,now):
'''
:param new_report:获取最新的文件
:param new_report_fail:获取最新的文件的路径
:param now:当前生成报告的时间
:return:
''' senduser = 'xxx@126.com'
sendpswd = 'xxx'
receuser = 'xxx@xxx.com.cn' #获取报告文件:'related'43行
f = open(new_report,'rb')
body_main = f.read() msg = MIMEMultipart()
# 邮件标题
msg['Subject'] = Header('TCS系统自动化测试报告','utf-8')
msg['From'] = senduser
msg['To'] = receuser
#邮件内容
text = MIMEText(body_main,'html','utf-8')
msg.attach(text) #发送附件
att = MIMEApplication(open(new_report_fail, 'rb').read())
# att = MIMEText(sendfile, 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment', filename=('utf-8', '',now + "_report.html"))
msg.attach(att) smtp = smtplib.SMTP()
smtp.connect('smtp.126.com')
smtp.login(senduser,sendpswd)
smtp.sendmail(senduser,receuser,msg.as_string()) if __name__ == '__main__':
startime = time.strftime('%H:%M:%S')
print("开始时间为:%s" % startime)
#测试路径
test_dir = './tcs/test_case'
#报告路径
report_dir = './tcs/report/' now = time.strftime('%Y-%m-%d_%H-%M-%S')
# 创建完整报告文件
new_report_fail = report_dir + now + '_result.html'
fp = open(new_report_fail,'wb') runner = HTMLTestRunner(stream=fp,
title="大标题:测试报告",
description='执行测试用例如下:')
# 查找测试文件
discover = unittest.defaultTestLoader.discover(test_dir,pattern='*_sta.py') runner.run(discover)
fp.close() #②搜索最新生成的文件
new_report = new_report(report_dir)
#③发送邮件
send_mail(new_report,new_report_fail,now) #展示测试报告html
driver = driver.browser()
driver.get("F:/PyProject/project/tcs/report/"+ now +"_result.html") stoptime = time.strftime('%H:%M:%S')
print("结束时间为:%s" %stoptime)
【Selenium + Python】自动化测试之发送邮件正文以及附件同时发送的更多相关文章
- selenium+python自动化98--文件下载弹窗处理(PyKeyboard)
前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...
- selenium+python自动化79-文件下载(SendKeys)
前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...
- python自动化测试之连接几组测试包实例
python自动化测试之连接几组测试包实例 本文实例讲述了python自动化测试之连接几组测试包的方法,分享给大家供大家参考.具体方法如下: 具体代码如下: class RomanNumera ...
- python自动化---各类发送邮件方法及其可能的错误
一.发送文本邮件 可能的问题1.:需要注意,目前QQ邮箱来讲,不能收到完整的邮件,即有些内容不能显示,最好全部使用网易邮箱: 可能的问题2.:在以往的文本邮件发送中,只写了 msg = MIMETex ...
- python+selenium简易自动化框架,包含生成测试报告以及发送结果至Email
Selenium+python环境搭建见虫师的pdf文档,非常详尽 简易框架: 1.文件目录:
- selenium+python自动化登录脚本
利用selenium+python写的一个关于登录的自动化脚本
- 【python-excel】Selenium+python自动化之读取Excel数据(xlrd)
Selenium2+python自动化之读取Excel数据(xlrd) 转载地址:http://www.cnblogs.com/lingzeng86/p/6793398.html ·········· ...
- selenium+python自动化102-登录页面滑动解锁(ActionChains)
前言 登录页面会遇到滑动解锁,滑动解锁的目的就是为了防止别人用代码登录(也就是为了防止你自动化登录),有些滑动解锁是需要去拼图这种会难一点. 有些直接拖到最最右侧就可以了,本篇讲下使用 seleniu ...
- 邮件正文及其附件的发送的C++实现
这段代码我花了整整一天来编写,假设转载,请注明出处,谢谢! 前面的一篇文章已经讲了怎样发送邮件正文,原理我就不再叙述了.要了解的同学请到这里查看! http://blog.csdn.ne ...
随机推荐
- Spoj SUBST1 New Distinct Substrings
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- [USACO17DEC] Barn Painting
题目描述 Farmer John has a large farm with NN barns (1 \le N \le 10^51≤N≤105 ), some of which are alread ...
- skywalking学习
skywalking简介 SkyWalking一个开源可观测性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.SkyWalking提供了一种简单的方法,可以让你清晰的查看分布式系统. ...
- hdu 1015 Safecracker 水题一枚
题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...
- 警告Conversion specifies type'int' but the argument has type'size_t'
代码: #import<Foundation/Foundation.h> int main(int argc,const char * argv[]){ const char *words ...
- linux-后台运行程序-nohup
语法格式: nohup 命令 & (日志输出到当前目录nohup.out) nohup 命令 & >> 文件 (日志 ...
- 控制流程完整性:给大家介绍一种“另类”的Javascript反分析技术
写在前面的话 理解恶意软件的真实代码对恶意软件分析人员来说是非常有优势的,因为这样才能够真正了解恶意软件所要做的事情.但不幸的是,我们并不总是能够得到“真实”的代码,有时恶意软件分析人员可能需要类似反 ...
- Playonlinux
apt-get install playonlinux -y apt-get install winbind -y apt-get install unzip -y 开始中搜索:playonlinux ...
- ES6里关于函数的拓展(二)
一.构造函数 Function构造函数是JS语法中很少被用到的一部分,通常我们用它来动态创建新的函数.这种构造函数接受字符串形式的参数,分别为函数参数及函数体 var add = new Functi ...
- [转载]Linux 卸载JDK并安装新版本JDK (rpm,tar)
FROM:http://josh-persistence.iteye.com/blog/1908549 一.查看Jdk的安装路径: whereis javawhich java (java执行路径)e ...