用于处理BeautifulReport生成的测试报告,提取html文件中的字段信息
from bs4 import BeautifulSoup
from setting.config import *
import json
from string import digits
import requests
def get_html_info(html_name):
'''
获取测试报告中的内容
param html_name:报告的名称
return:json list
'''
htmlname = u'%s/%s' % (REPORT_PATH, html_name)
# 获取文件对象
html = BeautifulSoup(open(htmlname, encoding='utf-8'), features='html.parser')
# 获取所有的文本
text = html.get_text()
# 字符串处理,把一些冗余的字符串处理掉
text = text.replace('
', '')
text = text.replace(' ', '')
text = text.split("详细信息")[-1]
text = text.split("查看详情")
text_list_temp = []
for i in text:
if 'Traceback' not in i:
text_list_temp.append(i)
elif 'Traceback' in i:
i = i.split('】')[-1]
text_list_temp.append(i)
# 将结果提取出来,组装成一个列表
res_json_list = []
for text in text_list_temp[0:-1]:
suite_name = text.split('Test')[-1].split('test')[0]
i = text.split('_')[-1].split(":")
case_id = i[0][-3:]
case_id = int(case_id)
case_title = i[1].split('.')[0]
# remove digits
case_title = case_title.translate(str.maketrans('', '', digits))
# 转换结果
result = i[1].split('s')[-1]
if result == '成功':
result = 'pass'
else:
result = 'fail'
# 结果组成字典, product_id, task_id
res_dict = {
# 'product_id': 42440,
'testtask_name':'集成测试',
'suite_name': suite_name,
# 'task_id': 43641,
'case_id': case_id,
'test_type':'UI',
'result': result,
'reals': case_title,
'area':'成都',
'user_name': 'mikigo'
}
# dict —> json
res_json = json.dumps(res_dict, ensure_ascii=False)
res_json_list.append(res_json)
return res_json_list