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()方法,所以需要修改一下

相关推荐
并不会1 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
一点媛艺2 小时前
Kotlin函数由易到难
开发语言·python·kotlin
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
_.Switch3 小时前
高级Python自动化运维:容器安全与网络策略的深度解析
运维·网络·python·安全·自动化·devops
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
萧鼎6 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
学地理的小胖砸6 小时前
【一些关于Python的信息和帮助】
开发语言·python
疯一样的码农6 小时前
Python 继承、多态、封装、抽象
开发语言·python
Python大数据分析@6 小时前
python操作CSV和excel,如何来做?
开发语言·python·excel
黑叶白树6 小时前
简单的签到程序 python笔记
笔记·python