Purpose
- In order to collect the data of all mobile phones after running the automated test, and send it to the specified user in the form of a form after processing
- Send as an email, and the email body is in HTML format
Disassembly of HTMLTable
- start a table title
Table header, as the name implies, one can roughly know what the table is about by looking at the table header
from HTMLTable import HTMLTable
table = HTMLTable(caption=table_name)
- Set header content
Table header design on demand, start time, test result, platform, system version, device name, app version
table.append_header_rows((
('开始时间', '用例总数', '用例通过数', '用例通过率', '平台', '系统版本', '设备厂商', 'app版本'),
)
)
- Cell content, passed in as a parameter, in the format of tuple-in-tuple ((),()). one example:
Configure the data content of the cell
table.append_data_rows(
(('2022-01-05 00:30:45', '28', '24', '85.71%', 'Android', '8.0.0','Xiaomi','3.6.2.022'),)
)
- debug style, debug header style
You can set the font, centering, etc. of the title
# 标题样式
caption_style = {
'text-align': 'center',
'cursor': 'pointer'
}
table.caption.set_style(caption_style)
- Set the border and set the table to be centered
Table basic settings: border, overall centering
# 设置边框
border_style = {
'border-color': '#000',
'border-width': '1px',
'border-style': 'solid',
'border-collapse': 'collapse',
# 同时设置了表格居中
'margin': 'auto',
}
# 外边框,set_style表格居中
table.set_style(border_style)
# 单元格边框
table.set_cell_style(border_style)
- Debug header style
Set title centering, background color, font, etc.
# 表头样式
header_cell_style = {
'text-align': 'center',
'padding': '4px',
'background-color': '#aae1fe',
'color': '#FFFFFF',
'font-size': '0.95em',
}
table.set_header_cell_style(header_cell_style)
- If the pass rate is less than 80%, mark red
Set as needed, requiring that the pass rate of automated test results should not be lower than 80%
# 如果通过率小于80%,标红
for row in table.iter_data_rows():
# 共 28,通过 24,通过率= 85.71%
tmp_value = row[3].value
# 85.71%
# tmp_res = re.findall(r".+?通过率= (.+)", tmp_value)[0]
# 85.71
res = tmp_value.strip('%')
if float(res) < 80:
# 通过率<80%,标红
row[3].set_style({
'background-color': '#ffdddd',
})
Easy to debug, set to >80 marked red
- generate html
You can debug it locally to your own satisfactory style.
html = table.to_html()
Implementation
def creat_email_html(table_name, rows_text):
'''
生成邮件正文内容,HTML格式
'''
# 标题内容
# table = HTMLTable(caption="xxxUI自动化测试结果(附件为报告详情)")
table = HTMLTable(caption=table_name)
# 没有实现空行
table.append_data_rows('')
# 表头
table.append_header_rows((
('开始时间', '用例总数', '用例通过数', '用例通过率', '平台', '系统版本', '设备厂商', 'app版本'),
)
)
'''
单元格内容,作为参数传入,格式为元组套元组((),())
table.append_data_rows(
(('2022-01-05 00:30:45', '28', '24', '85.71%', 'Android', '8.0.0','Xiaomi','3.6.2.022'),)
)
'''
table.append_data_rows(rows_text)
# 标题样式
caption_style = {
'text-align': 'center',
'cursor': 'pointer'
}
table.caption.set_style(caption_style)
# 设置边框
border_style = {
'border-color': '#000',
'border-width': '1px',
'border-style': 'solid',
'border-collapse': 'collapse',
# 实现表格居中
'margin': 'auto',
}
# 外边框
table.set_style(border_style)
# 单元格边框
table.set_cell_style(border_style)
# 单元格样式
# 先得设置cell_style,cell_style包括header_cell_style,会把header_cell_style配置覆盖
cell_style = {
'text-align': 'center',
'padding': '4px',
'background-color': '#ffffff',
'font-size': '0.95em',
}
table.set_cell_style(cell_style)
# 表头样式
header_cell_style = {
'text-align': 'center',
'padding': '4px',
'background-color': '#aae1fe',
'color': '#FFFFFF',
'font-size': '0.95em',
}
table.set_header_cell_style(header_cell_style)
# 如果通过率小于80%,标红
for row in table.iter_data_rows():
# 共 28,通过 24,通过率= 85.71%
tmp_value = row[3].value
# 85.71%
# tmp_res = re.findall(r".+?通过率= (.+)", tmp_value)[0]
# 85.71
res = tmp_value.strip('%')
if float(res) < 80:
# 通过率<80%,标红
row[3].set_style({
'background-color': '#ffdddd',
})
# 生产HTML
html = table.to_html()
# print(html)
return html
- achieve effect