一、shell中调用python函数
1.邮件正文是框架自带的生成的报告
2.邮件附件是第三方类库生成的炫酷的报告看板
send_email.py
代码语言:javascript复制import re
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL
from email.header import Header
import schedule
from selenium import webdriver
from email.mime.text import MIMEText
from selenium.webdriver.chrome.options import Options
def get_report_source_code():
"""
获取test报告源码页面
"""
url = 'http://[192::1:192]/cgi-bin/test_report.pl?build=netIAG_3_2_0_13_gaojs_716'
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
driver.maximize_window()
source_code = driver.page_source
return source_code
def send_email():
"""
发送test_report邮件
"""
# 获取页面源码
source_code = get_report_source_code()
# 以126邮箱为例
# ----------------发件相关参数----------------
smtpserver = 'smtp.126.com'
port = 0
sender = 'testops_xxxx@126.com'
password = 'xxxxxxxxxQYIAPTAQST'
receicer = ['gaojs@000000000.com.cn', '13152020000@163.com']
# ----------------编辑邮件内容----------------
subject = 'netIAG每日构建测试报告'
body = f'<p>{source_code}<p>'
msg = MIMEMultipart()
msg['Subject'] = Header(subject, 'utf-8') # 邮件主题
msg['From'] = sender # 发件人
msg['To'] = ';'.join(receicer)
msg.attach(MIMEText(body, 'html', 'utf-8'))
attchment = MIMEApplication(open(r'./reports/report.html', 'rb').read())
attchment.add_header('Content-Disposition', 'attachment', filename='report.html')
msg.attach(attchment) # 添加附件到邮件
smtp = SMTP_SSL(smtpserver)
smtp.login('testops_jianshuai@126.com', password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()
print('******************* 邮件发送完成 ,请查收附件************************')
if __name__ == '__main__':
send_email()
sh文件中调用send_mail函数
代码语言:javascript复制python3 -c 'import send_email; print(send_email.send_email())'
run.sh
代码语言:javascript复制# 删除上次产生的报告
rm -rf /home/array/src/reports/*
pytest -s ./smoke_test/aaa/radius/ --build netIAG_2_2_0_13_gaojs_716 --report=report.html --title=netIAG3.2.0.13每日构建测试报告 --tester=gaojs --desc=netIAG每>日构建报告 --template=2
# 将产生的报告重命名为report.html
mv /home/array/src/reports/*report.html /home/array/src/reports/report.html
# 调用发邮件函数
cd /home/array/src/
python3 -c 'import send_email; print(send_email.send_email())'
sleep 15s
echo ""
echo "test done