心路历程:本来想着按照时间创建文件夹,进入文件夹后进行执行自动化脚本,结果手动执行一掉毛病没有,但是上了定时任务凉凉了。折腾了好久,想到了最佳方案。为什么要分别创建文件夹,因为执行脚本会覆盖
最佳方案就是:创建周一到周五,五个文件夹,每天执行一遍,只保留5次历史记录,完美,判定周几,既执行周几,完美、
下边是发报告的脚本【周一到周五每天7点发送,要注意的是第一个一定要写0,不然分分钟沾满你的邮箱,没分发一个,发到你崩溃】
0 7 * * 1-5 ./etc/profile;/home/anaconda3/bin/python /usr/local/apache-tomcat-8.5.59/webapps/test/ti.py
import smtplib
from email.header import Header
from email.mime.text import MIMEText
import time
from datetime import datetime
receiver = ['li@shxseer.com', '155@qq.com','co@shxseer.com'] # 设置邮件接收人,这里是我的公司邮箱
host = 'smtp.exmail.qq.com' # 设置发件服务器地址
port = 25 # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式
sender = 'lii@shxseer.com' # 设置发件邮箱
pwd = 'Se56' # 设置发件邮箱的密码
body = 'hello' # 设置邮件正文,这里是支持HTML的
msg = MIMEText(body, 'html') # 设置正文为符合邮件格式的HTML内容
subject = '测试自动化报告'
dayOfWeek = datetime.now().isoweekday() ###返回数字1-7代表周一到周日
# print(dayOfWeek )
if dayOfWeek == 1:
api ='http://1980/test/mon/log.html'
elif dayOfWeek == 2:
api ='http://1980/test/tue/log.html'
elif dayOfWeek == 3:
api ='http://1920/test/wed/log.html'
elif dayOfWeek == 4:
api ='http://19080/test/thu/log.html'
elif dayOfWeek == 5:
api ='http://1980/test/fri/log.html'
# print(api)
msg = MIMEText('<html><h2></h2><html><form action=“”method=“”><fieldset><legend>自动化测试报告</legend><div style="line-height:30px"align=“Center”>'
'接口端</div> '+api+'<div style="line-height:40px"align=“Center”>'
'UI端</div> http://19080/svn_sale/UI/ui/run.log/log.html</fieldset></form>', 'html', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['from'] = sender # 设置发送人
msg['to'] = ';'.join(receiver) # 设置接收人
s = smtplib.SMTP(host, port) # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL
s.login(sender, pwd) # 登陆邮箱
time.sleep(5)
s.sendmail(sender, receiver, msg.as_string()) # 发送邮件!