简体   繁体   中英

getElementsByTagName() in Python's xml.dom.minidom is not working

I am parsing an output XML file generated from gtest. I want to find the result of each test case. A test case is failed only when "testcase" has element "failure" otherwise test case is passed. But I could not access element.

My xml file :-

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="11" failures="0" disabled="0" errors="0" timestamp="2015-03-23T17:29:43" time="1.309" name="AllTests">
  <testsuite name="AAA" tests="4" failures="0" disabled="0" errors="0" time="0.008">
    <testcase name="BBBB" status="run" time="0.002" classname="AAA" />
      <failure message="Value of: add(1, 1)&#x0A; Actual: 3&#x0A;Expected: 2" type="" />
    <testcase name="CCC" status="run" time="0.002" classname="AAA" />
    <testcase name="DDD" status="run" time="0.002" classname="AAA" />
    <testcase name="FFF" status="run" time="0.002" classname="AAA" />
  </testsuite>
</testsuites>

My python file is :-

from xlrd import open_workbook
from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("output.xml")
testsuites = DOMTree.documentElement 
testCaseCollection = testsuites.getElementsByTagName("testcase")
testCasefailure = testsuites.getElementsByTagName("failure")

OutputXLS = open_workbook('output.xls')

for testCase in testCaseCollection:

        #print testCase.firstChild;
        if testsuites.getElementsByTagName("failure"):
                print testCase.getAttribute("name"), " --> ","FAIL"
        else:
                print testCase.getAttribute("name"), " --> ","PASS"

And output is :-

BBB -->  PASS
CCC  -->  PASS
DDD  -->  PASS
FFF  -->  PASS

Though test case "BBB" is failed as it has "failure" attribute in xml, it shows pass in result. Kindly Help me out with this.

from xlrd import open_workbook
from xml.dom.minidom import parse

# Open XML document using minidom parser
DOMTree = parse("output.xml")
testsuites = DOMTree.documentElement 
testCaseCollection = testsuites.getElementsByTagName("testcase")

OutputXLS = open_workbook('output.xls')

for testCase in testCaseCollection:
    sibNode = testCase.nextSibling.nextSibling
    if sibNode and sibNode.nodeName == 'failure':
        print testCase.getAttribute("name"), " --> ","FAIL"
    else:
        print testCase.getAttribute("name"), " --> ","PASS"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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