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

相关推荐
FreakStudio3 小时前
不用装软件!这款MicroPython浏览器 IDE :让你在手机上也能调试树莓派 Pico
python·单片机·嵌入式·电子diy·tinyml
m0_743470374 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
数据科学小丫6 小时前
Python 数据存储操作_数据存储、补充知识点:Python 与 MySQL交互
数据库·python·mysql
Knight_AL6 小时前
Nacos 启动问题 Failed to create database ’D:\nacos\nacos\data\derby-data’
开发语言·数据库·python
查古穆7 小时前
python进阶-Pydantic模型
开发语言·python
佳木逢钺7 小时前
PyQt界面美化系统高级工具库:打造现代化桌面应用的完整指南
python·pyqt
工頁光軍8 小时前
基于Python的Milvus完整使用案例
开发语言·python·milvus
Csvn8 小时前
特殊方法与运算符重载
python
xht08328 小时前
PHP vs Python:编程语言终极对决
开发语言·python·php
2401_879693878 小时前
使用Python控制Arduino或树莓派
jvm·数据库·python