Selenium 2自动化测试实战37(自动发邮件功能)

自动发邮件功能html

例如,若是想在自动化脚本运行完成以后,邮箱就能够收到最新的测试报告结果。
SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规划,由它来控制信件的中转方式。
python的smtplib模块提供了一种很方便的途径用来发送电子邮件,它对SMTP协议进行了简单的封装。可使用SMTP对象的sendmail方法发送邮件,经过help()可查看SMTP所提供的方法,命令以下:python

-from smtplib import SMTP
-help(SMTP)

导入SMTP对象,经过help()查看对象的注释,从中找到sendmail()方法的使用说明。web

-connect(host,port)方法参数说明以下。
-host:指定链接的邮箱服务器
-port:指定链接服务器的端口号

-login(user,password)方法参数说明以下。
-user:登陆邮箱用户名
-password:登陆邮箱密码

sendmail(from_addr,to_addrs,msg……)方法参数说明以下
-from_addr:邮件发送者地址
-to_addr:字符串列表,邮件发送地址
-msg:发送消息

quit()方法:用于结束SMTP会话。 

通常发邮件有两种方式
方式一:自已邮箱的web页面(如:mail.126.com),输入本身邮箱的用户名和密码登陆,打开发邮件页面,填写对方的邮箱地址及邮件标题与正文,完成后单击发送。
方式二:下载安装邮箱客户端(如:Outlook、Foxmail等),填写邮箱帐号、密码及邮箱服务器(如:smtp.126.com),通常的邮箱客户端会默认记下这些信息,因此,这个过程只需填写一次,后面发邮件的过程与方法一相同。服务器

经过python的SMTP对象发邮件则更像方式二,由于须要填写邮箱服务器。app

 

2.发送HTML格式的邮件测试

#send_mail.py
#coding:utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header #发送邮箱服务器 smtpserver='smtp.sina.com' #发送邮箱用户/密码 user='ritasxxxx' password='xxxxxxx'#发送人邮件的客户端受权码 #发送邮箱 sender='ritasxxx@sina.com' #接收邮箱 receiver='27338xx@qq.com' #发送邮件主题 subject='python email test' #编写HTML类型的邮件正文 msg=MIMEText('<html><h1>你好呀!qqEmail</h1></html>','html','utf-8') msg['subject']=Header(subject,'utf-8') msg['from'] = sender # 设置发送人 msg['to'] = receiver # 设置接收人 #链接发送邮件 smtp=smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user,password) smtp.sendmail(sender,receiver,msg.as_string()) smtp.quit() 

运行以后,以下图所示:ui

 

 

本例中,除SMTP模块外,咱们还用到了email模块,它主要用来定义邮件的标题和正文;Header()方法用来定义邮件标题;MIMEText()用于定义邮件正文,参数为html格式的文本。spa

 

3.发送带附件的邮件
在发送文件时,有时须要发送附件。3d

#send_email.py
#coding:utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart #发送邮箱服务器 smtpserver='smtp.sina.com' #发送邮箱用户/密码 user='ritaxxxxx' password='xxxxxx'#发送人邮件的客户端受权码 #发送邮箱 sender='ritaxxx@sina.com' #接收邮箱 receiver='2733xxxx@qq.com' #发送邮件主题 subject='python email test again' #发送的附件 sendfile=open("F:\\study\\web_demo1\\report\\log.txt",'rb').read() att=MIMEText(sendfile,'base64','utf-8') att["Content-Type"]='application/octet-stream' att["Content-Disposition"]='attachment;filename="log.txt"' msgRoot=MIMEMultipart('related') msgRoot['Subject']=Header(subject,'utf-8') msgRoot.attach(att) msgRoot['from'] = sender # 设置发送人 msgRoot['to'] = receiver # 设置接收人 #链接发送邮件 smtp=smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user,password) smtp.sendmail(sender,receiver,msgRoot.as_string()) smtp.quit()

(经过MIMEMultipart()模块构造的带附件)执行以后结果以下图所示:server

 

 

 

4.查找最新的测试报告

#find_file.py
#coding:utf-8 import os #定义文件目录 result_dir='F:\\study\\web_demo1\\test_case' lists=os.listdir(result_dir) #从新按时间对目录下的文件进行排序 lists.sort(key=lambda fn:os.path.getmtime(result_dir+"\\"+fn)) print((u"最新的文件为: "+lists[-1])) file=os.path.join(result_dir,lists[-1]) print file 

执行以后,结果以下图所示:

首先定义测试报告的目录result_dir,os_listdir()能够获取目录下的全部文件及文件夹。利用sort()方法对目录下的文件及文件夹按时间从新排序。List[-1]取到的就是最新生成的文件或文件夹。

相关文章
相关标签/搜索