Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stingbo committed Feb 10, 2021
2 parents 03ea182 + c12a410 commit 1e57a10
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 5 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 使用Python Selenium 进行自动化测试

- 支持在PC、H5上运行,代理获取接口数据,无头模式,生成报告(含截图),发送邮件等

## 快速开始

- 复制并修改全局配置,`cp config.example.yaml config.yaml`,此文件为全局配置,config目录下文件可覆盖此配置
Expand Down Expand Up @@ -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`
Expand All @@ -69,11 +74,11 @@
> headless: 布尔,`True/False`,是否开启无头模式,无界面测试,方便集成到服务器自动化部署
3. WEBSITE,启动页面的设置
4. WEBSITE,启动页面的设置

> url: 字符串,启动页地址,目前只有这一个配置可用
4. MENU,测试用例详情
5. MENU,测试用例详情

> key: 字符串,菜单路径
Expand Down Expand Up @@ -127,6 +132,19 @@
DEBUG: True/False #是否开启调试模式,True-不生成生成报告,方便调试
IMAGE: True/False #是否截图,DEBUG为False且IMAGE设置为True时截图

MAIL:
SEND: True/False
# 邮箱服务端配置
SMTP:
username: [email protected]
password: xxxxxxxxxx
host: smtp.163.com
port: 25
# 收件人列表
receiver:
- [email protected]
- [email protected]

BROWSER:
#浏览器类型
type: Chrome/Firefox
Expand Down
13 changes: 13 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
DEBUG: True/False #是否开启调试模式,True-不生成生成报告,方便调试
IMAGE: True/False #是否截图,DEBUG为False且IMAGE设置为True时截图

MAIL:
SEND: True/False #是否开启发送邮箱配置,前提是,在DEBUG为false情况下生效
# 邮箱服务端配置
SMTP:
username: [email protected]
password: xxxxxxxxxx
host: smtp.163.com
port: 25
# 收件人列表
receiver:
- [email protected]
- [email protected]

BROWSER:
#浏览器类型
type: Chrome/Firefox
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
75 changes: 75 additions & 0 deletions utils/mail.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1e57a10

Please sign in to comment.