TA的每日心情 | 无聊 14 小时前 |
---|
签到天数: 531 天 连续签到: 1 天 [LV.9]测试副司令
|
1测试积点
我现在只做到了把测试报告以文件的形式发送到邮箱,请问怎么把这个测试报告以附件的形式发送邮件到邮箱?
以下为源代码:
- # coding=utf-8
- from selenium import webdriver
- import time, unittest, os
- import HTMLTestRunner
- import smtplib
- from email.mime.text import MIMEText
- from email.header import Header
- class h5(unittest.TestCase):
- def openweb1(b):
- b = webdriver.Chrome()
- time.sleep(2)
- b.get('https://192.168.180.150/meeting/login')
- b.maximize_window()
- b.find_element_by_xpath('//*[@id="right"]/div[1]/input').send_keys('test001')
- b.find_element_by_xpath('//*[@id="right"]/div[2]/input').send_keys('0003000')
- b.find_element_by_xpath('//*[@id="right"]/div[3]/div').click()
- time.sleep(2)
- now_url = b.current_url
- if now_url == "https://192.168.180.150/meeting/meetingToStart":
- print("登录成功")
- else:
- raise error("登录失败")
- b.quit()
-
- def openweb(b):
- b = webdriver.Chrome()
- time.sleep(2)
- b.get('https://192.168.180.150/meeting/login')
- b.maximize_window()
- b.find_element_by_xpath('//*[@id="right"]/div[1]/input').send_keys('test001')
- b.find_element_by_xpath('//*[@id="right"]/div[2]/input').send_keys('000000')
- b.find_element_by_xpath('//*[@id="right"]/div[3]/div').click()
- time.sleep(2)
- now_url = b.current_url
- if now_url == "https://192.168.180.150/meeting/meetingToStart":
- print("登录成功")
- else:
- raise error("登录失败")
- b.quit()
- def sendmail(file_new):
- # 发信邮箱
- mail_from = 'xxx@qq.com'
- mail_to = ['xxx@qq.com'] # 收件箱
- # 定义正文
- f = open(file_new, 'rb')
- mail_body = f.read()
- f.close()
- msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
- # 定义标题
- msg['Subject'] = u"自动化测试报告"
- # 定义发送时间(不定义的可能有的邮件客户端会不显示发送时间)
- msg['from'] = Header("xxx@qq.com", 'utf-8')
- msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
- smtp = smtplib.SMTP()
- # 连接 SMTP 服务器,此处用的 qq 的 SMTP 服务器
- smtp.connect('smtp.qq.com')
- # 用户名密码
- smtp.login('xxx@qq.com', 'fhqilugaqvilbcbi')
- smtp.sendmail(mail_from, mail_to, msg.as_string())
- smtp.quit()
- print('email has send out')
- def send_report(testreport):
- result_dir = testreport
- lists = os.listdir(result_dir)
- lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn))
- # print (u'最新测试生成的报告: '+lists[-1])
- # 找到最新生成的文件
- file_new = os.path.join(result_dir, lists[-1])
- print(file_new)
- # 调用发邮件模块
- sendmail(file_new)
- testunit = unittest.TestSuite()
- testunit.addTest(h5("openweb"))
- testunit.addTest(h5("openweb1"))
- now = time.strftime("%Y-%m-%d-%H-%M-%S")
- # 定义报告存放路径,支持相对路径
- filePath = 'D://testcase//'
- filename = filePath + now + "result.html"
- fp = open(filename, 'wb')
- # 定义测试报告
- runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')
- # 运行测试用例
- runner.run(testunit)
- fp.close()
- send_report(filePath)
- 下图为执行结果和邮件
复制代码
|
|