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><td class="report_table_content">replace0</td><td class="report_ta

本文介绍如何在Python3环境下,通过读取Robot Framework的log.html或output.xml文件,提取数据并组装HTML模板,最后使用SMTP协议发送包含报告的HTML格式邮件。
最低0.47元/天 解锁文章
607

被折叠的 条评论
为什么被折叠?



