selenium自動發送郵件

1.咱們已經完成了selenium生成測試報告,那咱們要作將測試報告發送給相關人員,就能夠這樣使用html

#coding: utf - 8from case.t_single import Sinfrom case.test_baidu import Ceshifrom case.test_bokeyuan import Bofrom unittest import TestSuite,TestLoaderimport unittest,time,osimport HTMLTestRunnerimport smtplib#發送郵件模塊from email.mime.text import MIMEText #定義郵件neirfrom email.header import Header #定義郵件標題from email.mime.multipart import MIMEMultipartdef run_main():    suit = unittest.TestSuite()    suit.addTest(Bo('test_shouye'))    suit.addTest(Ceshi('test_search'))    suit.addTest(Ceshi('test_home'))    now=time.strftime('%Y%m%d%H%M',time.localtime(time.time())) #'%Y%m%d%H%M',time.localtime(time.time())    file=r'report/report.html'  #測試報告地址    fb=open(file,'wb')    runner = HTMLTestRunner.HTMLTestRunner(stream=fb,title='test_report',description='detail')    runner.run(suit)    fb.close()    with open(file,'rb') as ob:        main_body=ob.read()    msg=MIMEMultipart()  #添加容器    body=MIMEText(main_body,'html','utf-8')  #郵件正文    msg['Subject']=u'測試完成'    msg['sender']='xxxx'        #發件人    msg['receiver'] = 'xxx'  #收件人    msg.attach(body)    #添加附件    att=MIMEText(main_body,'bs64','utf-8')    att['Content-Type']="application/octet-stream"    att['Content-Disposition'] = 'attachment;filename="report.html"'    msg.attach(att)    try:        smtp=smtplib.SMTP_SSL('smtp.qq.com',465)  #扣扣郵件服務,端口=465    except:        smtp=smtplib.SMTP()        smtp.connect('smtp.qq.com',465)    smtp.login(msg['sender'],'password=xxx')   #郵件服務登錄用戶名,密碼    smtp.sendmail(msg['sender'],msg['receiver'],msg.as_string())  #發送郵件    smtp.quit()    print('mail send out')if __name__ == '__main__':    run_main()
相關文章
相關標籤/搜索