python解析robot framework的output.xml并生成html
一、用pyh模块解析stat结点数据(output.py)
#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)
在这里插入代码片#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()方法,所以需要修改一下