Python 3 Script to Convert XML Report to JSON File Using json Module Full Project For Beginners

 

 

app.py

 

 

#!/usr/bin/python

from xml.dom.minidom import parse
import xml.dom.minidom
import json

xmldoc = xml.dom.minidom.parse("report.xml").documentElement
nodes = xmldoc.getElementsByTagName("testcase")
teastcase_list = []

for case_node in nodes:
    testcase = {
        'name': case_node.getAttribute("name"),
        'class': case_node.getAttribute("class")
    }

    errors = case_node.getElementsByTagName("error")

    if len(errors) > 0 :
        error = errors[0]
        testcase['error'] = error.getAttribute("type")
        testcase['error_detail'] = error.firstChild.nodeValue

    teastcase_list.append(testcase)

teastcase_list_json = json.dumps(teastcase_list, indent=2)

print(teastcase_list_json)

 

 

Now you need to make a report.xml file inside the same directory and copy paste the below code

 

 

report.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
    <testsuite name="acceptance_test" tests="129" assertions="456" failures="0" errors="1" time="6345.359460">
        <testsuite name="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" namespace="grn\selenium\acceptance_test" fullPackage="grn.selenium.acceptance.test" tests="5" assertions="11" failures="0" errors="0" time="132.940767">
            <testcase name="test_AddressBook_001_addShareBook" class="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" line="21" assertions="1" time="21.589742"/>
            <testcase name="test_AddressBook_002_addAddressIntoShareBook" class="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" line="41" assertions="3" time="29.417708"/>
            <testcase name="test_AddressBook_003_addPersonalAddressBook" class="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" line="61" assertions="3" time="28.361749"/>
            <testcase name="test_AddressBook_004_editPersonalAddressBook" class="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" line="87" assertions="3" time="24.068415"/>
            <testcase name="test_AddressBook_005_deletePersonalAddressBook" class="grn\selenium\acceptance_test\AddressBookTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/AddressBookTest.php" line="115" assertions="1" time="29.503153"/>
        </testsuite>
        <testsuite name="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" namespace="grn\selenium\acceptance_test" fullPackage="grn.selenium.acceptance.test" tests="6" assertions="17" failures="0" errors="1" time="214.833632">
            <testcase name="test_Personal_001_addMyGroupWithoutUsersAndFacilities" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="37" assertions="1" time="58.236458"/>
            <testcase name="test_Personal_002_addMyGroupWithUsersAndFacilities" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="47" assertions="7" time="56.739449"/>
            <testcase name="test_Personal_003_addEventWithMyGroup" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="76" assertions="0" time="27.752685"/>
            <testcase name="test_Personal_004_viewAppointmentOnGroupDayAndWeekView" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="96" assertions="2" time="33.689306"/>
            <testcase name="test_Personal_005_viewAppointmentOnGroupWeekAndDayView" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="123" assertions="7" time="32.812612"/>
            <testcase name="test_Personal_006_switchLanguage" class="grn\selenium\acceptance_test\PersonalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php" line="171" assertions="0" time="5.603122">
                <error type="PHPUnit_Framework_SkippedTestError">Skipped Test
                    /home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PersonalTest.php:173
                    /home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/src/common/GaroonWebTestCaseForWebDriver.php:68
                </error>
            </testcase>
        </testsuite>
        <testsuite name="grn\selenium\acceptance_test\PortalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PortalTest.php" namespace="grn\selenium\acceptance_test" fullPackage="grn.selenium.acceptance.test" tests="3" assertions="4" failures="0" errors="0" time="336.092382">
            <testcase name="test_Portal_001_addHtmlPortlet" class="grn\selenium\acceptance_test\PortalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PortalTest.php" line="26" assertions="1" time="22.249871"/>
            <testcase name="test_Portal_002_addPortalAndPublic" class="grn\selenium\acceptance_test\PortalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PortalTest.php" line="40" assertions="2" time="28.596418"/>
            <testcase name="test_Portal_003_addPortlet" class="grn\selenium\acceptance_test\PortalTest" file="/home/cybozu/jenkins/workspace/test-garoon-by-selenium-F9.0/tests/selenium/acceptance_test/PortalTest.php" line="61" assertions="1" time="285.246093"/>
        </testsuite>
    </testsuite>
</testsuites>

Leave a Reply