繁体   English   中英

Python XML按顺序解析子标签

[英]Python XML Parsing the child tags in order

我的XML文件类似于以下文件:

<suite name="regression_1">
    <test name="Login check" id="s1-t1">
        <keyword name="Valid Username and Password">
            <keyword name="Invalid Username or Password">
                <keyword name="Invalid password">
                    <message level="TRACE" >Return error</message>
                    <status status="PASS"/>
                </keyword>
                <message level="INFO">Return error</message>
                <status status="FAIL"/>
            </keyword>
            <message level="INFO">Return: None</message>
            <status status="PASS"/>
        </keyword>
        <status status="FAIL"/>
    </test>
    <test name="test-2" id="s1-t1">
        <keyword name="abc">
            <keyword name="def">
                <message level="INFO">Return error</message>
                <status status="FAIL"/>
            </keyword>
            <message level="INFO">Return: None</message>
            <status status="PASS"/>
        </keyword>
        <status status="FAIL"/>
    </test>
</suite>

我的输出应检查关键字,并为状态为“失败”的关键字提供关键字结构。 一个测试中将有许多关键字,并且可能有或没有子关键字。

****样本输出*******

套件:regress_1

测试名称:登录检查

关键字失败:[“有效的用户名和密码”,“无效的用户名或密码”]

失败测试案例消息:返回错误

套件:regress_1

测试名称:test-2

关键字失败:[“ abc”,“ def”]

失败测试案例消息:返回错误


我的代码能够挖掘到最后一个孩子,以收集失败状态。 但是无法解析分析所需的正确路径。 我也认为完整的循环没有得到执行。 例如,如果第三个孩子是“ PASS”,则它不会返回第二个孩子来检查其状态。

def getStatusForNode(tc):
    status_to_be_returned = []
    is_just_father = False


    for child in tc.childNodes:
        if child.nodeName == "keyword":
            is_just_father = True
            status_to_be_returned.append(getStatusForNode(child)[0])
            keyword_track.append(child.getAttribute("name"))

    if not is_just_father:
        status = tc.getElementsByTagName('status')
        return [(tc, status)]


    return  status_to_be_returned


DOMTree = xml.dom.minidom.parse("output.xml")
collection = DOMTree.documentElement
tc_entry = collection.getElementsByTagName("suite")

top = Element('tests')
comment = Comment("This xml is generated only for failing tests")
top.append(comment)


for tc in tc_entry:
    if tc.hasAttribute("name"):
       print("Suite name: {}".format(tc.getAttribute("name")))

    tests = tc.getElementsByTagName('test')
    for test in tests:
        keyword_track = []
        for child in test.childNodes:
            if child.nodeName == "keyword":
                children_status = getStatusForNode(child)
                for (tc_name, status) in children_status:
                    for state in status:
                        if state.getAttribute("status") != "PASS":
                            print("---")
                            print("Test name: {}".format(test.getAttribute("name")))
                            print("Keyword failed: {}".format(tc_name.getAttribute("name")))
                            print("Status: {}".format(state.getAttribute("status")))
                            messages = tc_name.getElementsByTagName('msg')
                            print("Failure test case messages:")
                            for message in messages:
                                print(message.childNodes[0].data)
                            print ("")

从此代码收到的输出:

测试名称:ABC

关键字名称:keyword_1-2-3

状态:失败

失败测试案例消息:在级别3中失败

对代码有任何建议的优化吗?

问题 :XML按顺序解析子标记

例如,使用xml.etree.ElementTree解决方案:

注意 :让<keyword>的第一个<keyword> Keyword faild:仍然没有意义Keyword faild:都具有PASS 如果要在输出中包含第一个<keyword> ,请删除#

from xml.etree import ElementTree as ET

with open('output.xml') as fh:
    suite = ET.fromstring(fh.read())

# Find all <test>"
for test in suite.findall('./test'):
    keyword_failed = []
    # first_keyword = test.find('./keyword')
    # keyword_failed = [first_keyword.attrib['name']]
    message = None

    # Find all <test><keyword> <status status="FAIL">
    for keyword in test.findall('.//keyword/status[@status="FAIL"]/..'):
        keyword_failed.append(keyword.attrib['name'])
        message = keyword.find('./message')

        print('Suite: {}'.format(suite.attrib['name']))
        print('\tTest Name: {}'.format(test.attrib['name']))
        print('\tKeyword failed: {}'.format(keyword_failed))
        print('\tFailure test case message : level={} {}'.format(message.attrib['level'], message.text))

输出
套件:regress_1
测试名称:登录检查
关键字失败:['无效的用户名或密码']
失败测试案例消息:level = INFO返回错误
套件:regress_1
测试名称:test-2
关键字失败:['def']
失败测试案例消息:level = INFO返回错误

使用Python测试:3.4.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM