diff --git a/README.md b/README.md index e6186c2..e8c18cf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # 使用Python Selenium 进行自动化测试 +- 支持在PC、H5上运行,代理获取接口数据,无头模式,生成报告(含截图),发送邮件等 + ## 快速开始 - 复制并修改全局配置,`cp config.example.yaml config.yaml`,此文件为全局配置,config目录下文件可覆盖此配置 @@ -52,10 +54,13 @@ > 8. 根据 action 执行动作 -* 配置文件内容分为五类,格式说明如下: - 1. DEBUG: 布尔,`True/False`,是否开启调试模式,True-不生成生成报告,方便调试 +* 配置文件分为以下内容,格式说明如下: + 1. DEBUG: 布尔,`True/False`,是否开启调试模式,True-不生成生成报告,方便调试 + IMAGE: 布尔,`True/False`,是否截图,DEBUG为False且IMAGE设置为True时截图 + + 2. MAIL: 邮箱配置,配置正确会发送测试用例执行报告 - 2. BROWSER,对浏览器层的设置 + 3. BROWSER,对浏览器层的设置 > type: 字符串,使用浏览器类型,目前支持 `Chrome/Firefox` @@ -69,11 +74,11 @@ > headless: 布尔,`True/False`,是否开启无头模式,无界面测试,方便集成到服务器自动化部署 - 3. WEBSITE,启动页面的设置 + 4. WEBSITE,启动页面的设置 > url: 字符串,启动页地址,目前只有这一个配置可用 - 4. MENU,测试用例详情 + 5. MENU,测试用例详情 > key: 字符串,菜单路径 @@ -127,6 +132,19 @@ DEBUG: True/False #是否开启调试模式,True-不生成生成报告,方便调试 IMAGE: True/False #是否截图,DEBUG为False且IMAGE设置为True时截图 +MAIL: + SEND: True/False + # 邮箱服务端配置 + SMTP: + username: sting_bo@163.com + password: xxxxxxxxxx + host: smtp.163.com + port: 25 + # 收件人列表 + receiver: + - 598207213@qq.com + - 1107453152@qq.com + BROWSER: #浏览器类型 type: Chrome/Firefox diff --git a/config.example.yaml b/config.example.yaml index b4efcbf..5a16f06 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -1,6 +1,19 @@ DEBUG: True/False #是否开启调试模式,True-不生成生成报告,方便调试 IMAGE: True/False #是否截图,DEBUG为False且IMAGE设置为True时截图 +MAIL: + SEND: True/False #是否开启发送邮箱配置,前提是,在DEBUG为false情况下生效 + # 邮箱服务端配置 + SMTP: + username: sting_bo@163.com + password: xxxxxxxxxx + host: smtp.163.com + port: 25 + # 收件人列表 + receiver: + - 598207213@qq.com + - 1107453152@qq.com + BROWSER: #浏览器类型 type: Chrome/Firefox diff --git a/test.py b/test.py index bb8eb73..be75b0f 100644 --- a/test.py +++ b/test.py @@ -12,6 +12,7 @@ from utils.action import Action from utils.http import Http from utils.menu import Menu +from utils.mail import Mail from HwTestReport import HTMLTestReport as HTMLTestRunner from utils.parametrized_test_case import ParametrizedTestCase from utils.test_config import TestConfig, getFileName @@ -165,6 +166,17 @@ def main(): sleep(5) browser.quit() + # send mail or not + mail = config.get('MAIL') + if not debug and mail and mail.get('SEND'): + email_title = report_title + email_content = report_desc + smtp = Mail(config.get('MAIL'), report_path) + smtp.connect() + smtp.login() + smtp.send(email_title, email_content, report_file) + smtp.quit() + if is_open_proxy: proxy_client.close() proxy_server.stop() diff --git a/utils/mail.py b/utils/mail.py new file mode 100644 index 0000000..978bb9d --- /dev/null +++ b/utils/mail.py @@ -0,0 +1,75 @@ +# coding = utf-8 +from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication +from email.mime.text import MIMEText +from email.mime.image import MIMEImage +from email.mime.audio import MIMEAudio +import smtplib +import os + + +class Mail(object): + def __init__(self, config, email_attachment_path): + """ + init config + """ + self.attachment_path = email_attachment_path + self.smtp = smtplib.SMTP() + self.username = config.get('SMTP').get('username') + self.password = config.get('SMTP').get('password') + self.sender = config.get('SMTP').get('username') + self.host = config.get('SMTP').get('host') + self.port = config.get('SMTP').get('port') + self.receiver = config.get('receiver') + + def connect(self): + """ + connect server + """ + self.smtp.connect(self.host, self.port) + + def login(self): + """ + login email + """ + try: + self.smtp.login(self.username, self.password) + except: + raise AttributeError('Can not login smtp!!!') + + def send(self, email_title, email_content, email_attachment=""): + """ + send email + """ + msg = MIMEMultipart() # create MIMEMultipart + msg['From'] = self.sender # sender + msg['To'] = ', '.join(self.receiver) + msg['Subject'] = email_title # email Subject + content = MIMEText(email_content, _charset='gbk') # add email content, coding is gbk, because chinese exist + msg.attach(content) + + # 是否指定附件,否,则发送目录下所有文件 + for attachment_name in os.listdir(self.attachment_path): + if email_attachment and attachment_name != email_attachment: + continue + else: + attachment_file = os.path.join(self.attachment_path, attachment_name) + + with open(attachment_file, 'rb') as attachment: + if 'application' == 'text': + attachment = MIMEText(attachment.read(), _subtype='octet-stream', _charset='GB2312') + elif 'application' == 'image': + attachment = MIMEImage(attachment.read(), _subtype='octet-stream') + elif 'application' == 'audio': + attachment = MIMEAudio(attachment.read(), _subtype='octet-stream') + else: + attachment = MIMEApplication(attachment.read(), _subtype='octet-stream') + + attachment.add_header('Content-Disposition', 'attachment', filename=('gbk', '', attachment_name)) + # make sure "attachment_name is chinese" right + msg.attach(attachment) + + self.smtp.sendmail(self.sender, self.receiver, msg.as_string()) # format msg.as_string() + + def quit(self): + self.smtp.quit()