robot运行完成后会生成三个文件,现想发送log.html中的三个表格邮件,由于该日志是JS格式,所以直接发送,邮件会提示打开robot报告失败。可以通过读取log.html或者output.xml文件来提取所需数据信息,然后自己组装html格式文件。主要由以下几个步骤:
1、读取log.html,使用正则提取三个表格中的数据内容
2、组装html模板
3、使用smtp协议发送html格式的邮件内容,也可以带附件
直接上代码:
# -*- coding: utf-8 -*-
# @Time : 2020/6/15 10:27
# @Author :
# @desc :
import time
import smtplib
import email
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
from email.mime.multipart import MIMEMultipart
import os.path
import re
from bs4 import BeautifulSoup
class SendEmail(object):
def __init__(self):
self.log_path = self.get_log_path('log.html')
self.report_path = self.get_log_path('report.html')
self.head = '''
<head><style type="text/css">
.report_style{margin:20px 10%}
.header{text-align:center;width:100%;font-family:"Times New Roman"}
.report_table {text-align:center; width:100%; font-family:"Times New Roman"}
.report_table_title {height:40px; text-align:center; width:100%; font-weight: bold; font-size:24px}
.report_table_content {background-color:#99CCFF; width:27%; text-align:center; padding:0 10px;}
.report_table_total {background-color:#CCFFFF; width:18%; text-align:center; padding:0 10px;}
.table_content_success {background-color: #6bff8f; width:16%; text-align:center; padding:0 10px;}
.table_content_fail {background-color: #ff6c6d; width:15%; text-align:center; padding:0 10px;}
.table_elapsed_time {background-color:#99CCFF; width:24%; text-align:center; padding:0 10px;}
.table_head {background-color: #66CCFF; text-align:center; font-weight:bold; padding:0 10px;}
.table_path {background-color:#FFCC99; width:0%; text-align:center; padding:0 10px;}
</style></head>
<div class="header"><h1>自动化测试报告</h1></div>
'''
self.table_head = '''<div class="report_style"><table class="report_table">
<tr><th colspan="6" class="report_table_title">table_title_name</th></tr>
<br>
<tr><td class="table_head">name</td><td class="table_head">Total</td><td class="table_head">Pass</td><td class="table_head">Fail</td><td class="table_head">Elapsed Time</td></tr>
'''
self.table_content = '<tr>&