python解析robot framework的output.xml并生成html

一、用pyh模块解析stat结点数据(output.py

python 复制代码
#coding=utf-8

import xml.dom.minidom
import xml.etree.ElementTree

#打开xml文档
dom = xml.dom.minidom.parse('./ui/output.xml');

root2 = xml.etree.ElementTree.parse('./ui/output.xml')
#得到文档元素对象
root = dom.documentElement

total = root.getElementsByTagName('total');
total_len = len(total)
tag = root.getElementsByTagName('tag');
tag_len = len(tag)
suite = root.getElementsByTagName('suite');
suite_len = len(suite)

#total的stat节点个数
total2 = root2.getiterator("total")
total_stat_num = len(total2[total_len-1].getchildren())
#tag的stat节点个数
tag2 = root2.getiterator("tag")
tag_stat_num = len(tag2[tag_len-1].getchildren())
#suite的stat节点个数
suite2 = root2.getiterator("suite")
suite_stat_num = len(suite2[suite_len-1].getchildren())

statlist = root.getElementsByTagName('stat');

def get_total_statistics():
    list = []
    for i in range(0,total_stat_num):
        d = dict()
        d['fail'] = int(statlist[i].getAttribute("fail"))
        d['pass'] = int(statlist[i].getAttribute("pass"))
        d['total'] = d['fail']+d['pass']
        d['text'] = statlist[i].firstChild.data
        list.append(d)
        #print 'total:'+d['fail']+', '+d['pass']+', '+d['text']

    return list
# list =  get_total_statistics()
# print list
# print list[0]['fail']

def get_statistics_by_tag():
    list = []
    for i in range(total_stat_num,total_stat_num+tag_stat_num):
        d = dict()
        d['fail'] = int(statlist[i].getAttribute("fail"))
        d['pass'] = int(statlist[i].getAttribute("pass"))
        d['total'] = d['fail']+d['pass']
        d['text'] = statlist[i].firstChild.data
        list.append(d)
        #print 'total:'+d['fail']+', '+d['pass']+', '+d['text']

    return list

def get_statistics_by_suite():
    list = []
    for i in range(total_stat_num+tag_stat_num,len(statlist)):
        d = dict()
        d['fail'] = int(statlist[i].getAttribute("fail"))
        d['pass'] = int(statlist[i].getAttribute("pass"))
        d['total'] = d['fail']+d['pass']
        d['text'] = statlist[i].firstChild.data
        list.append(d)
        #print 'total:'+d['fail']+', '+d['pass']+', '+d['text']

    return list

二、pyh生成带表格的HTML(report.py

python 复制代码
在这里插入代码片#coding=utf-8
from pyh import *
# from msilib.schema import Font
from fontTools.ttLib import TTFont
from output import *

page = PyH('My wonderful PyH page')

page << h2('Total statistics')

value = get_total_statistics()
print(value) 

mytab = page << table(border='1')
mytr = mytab << tr()
mytr << th("Total Statistics",width="200",bgcolor="#CCFFCC")+th("total",width="100",bgcolor="#CCFFCC")+th("pass",width="100",bgcolor="#CCFFCC")+th("fail",width="100",bgcolor="#CCFFCC")
for i in range(len(value)):
    mytr = mytab << tr()
    mytr << td(value[i]['text'])+td(value[i]['total'])+td(value[i]['pass'])+td(value[i]['fail'])


value = get_statistics_by_tag()
page << br()
mytab2 = page << table(border='1')
mytr = mytab2 << tr()
mytr << th("Statistics by Tag",width="200",bgcolor="#CCFFCC")+th("total",width="100",bgcolor="#CCFFCC")+th("pass",width="100",bgcolor="#CCFFCC")+th("fail",width="100",bgcolor="#CCFFCC")
for i in range(1,len(value)):
    mytr = mytab2 << tr()
    mytr << td(value[i]['text'])+td(value[i]['total'])+td(value[i]['pass'])+td(value[i]['fail'])


value = get_statistics_by_suite()
page << br()
mytab3 = page << table(border='1')
mytr = mytab3 << tr()
mytr << th("Statistics by Suite",width="200",bgcolor="#CCFFCC")+th("total",width="100",bgcolor="#CCFFCC")+th("pass",width="100",bgcolor="#CCFFCC")+th("fail",width="100",bgcolor="#CCFFCC")
for i in range(1,len(value)):
    mytr = mytab3 << tr()
    mytr << td(value[i]['text'])+td(value[i]['total'])+td(value[i]['pass'])+td(value[i]['fail'])


page.printOut('table.html')

报错

for n, v in self.attributes.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

python3去掉了 iteritem()方法,所以需要修改一下

相关推荐
Stara051131 分钟前
基于YOLOV5+Flask安全帽RTSP视频流实时目标检测
python·yolo·目标检测·flask
重整旗鼓~35 分钟前
2.flask中使用装饰器统一验证用户登录
后端·python·flask
杜小白也想的美42 分钟前
FlaskAPI-路径参数、查询参数
python·fastapi
EterNity_TiMe_44 分钟前
【论文复现】农作物病害分类(Web端实现)
前端·人工智能·python·机器学习·分类·数据挖掘
香橙薄荷心1 小时前
学一学前沿开发语言之Python
人工智能·python
潜意识起点1 小时前
Java数组:静态初始化与动态初始化详解
java·开发语言·python
余生H1 小时前
深入理解HTML页面加载解析和渲染过程(一)
前端·html·渲染
KevinRay_1 小时前
Numpy指南:解锁Python多维数组与矩阵运算(下)
python·矩阵·numpy·排序·文件读写
m0_748233361 小时前
Python大数据可视化:基于python的电影天堂数据可视化_django+hive
python·信息可视化·django
睡觉待开机1 小时前
python-判断语句(黑马程序员B站Python免费教学, 第三章内容总结)
python