Groovy web console

subscribe to the feed Subscribe
to this

Groovy Parse Xml

Published 2 months ago by Jeremy
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.xml.*

def testResultsFile = """<?xml version="1.0" encoding="UTF-8"?><robot generator="Robot 4.1.3 (Python 3.9.5 on win32)" generated="20230124 17:22:40.756" rpa="false" schemaversion="2">
    <suite id="s1" name="VSB" source="C:\\Jenkins\\workspace\\Experimental\\Jira-ATC_Job\\Robot_tests\\MFOM\\VSB">
        <suite id="s1-s1" name="Suites" source="C:\\Jenkins\\workspace\\Experimental\\Jira-ATC_Job\\Robot_tests\\MFOM\\VSB\\Suites">
            <suite id="s1-s1-s1" name="simpleTest" source="C:\\Jenkins\\workspace\\Experimental\\Jira-ATC_Job\\Robot_tests\\MFOM\\VSB\\Suites\\simpleTest.robot">
                <test id="s1-s1-s1-t1" name="MFOM-T2484 open the browser to">
                    <kw name="Open Browser" library="SeleniumLibrary">
                        <doc>Opens a new browser instance to the optional ``url``.</doc>
                        <msg timestamp="20230124 17:22:40.994" level="INFO">Opening browser 'Edge' to base url ''.</msg>
                        <status status="PASS" starttime="20230124 17:22:40.993" endtime="20230124 17:22:44.431"/>
                    <status status="PASS" starttime="20230124 17:22:40.993" endtime="20230124 17:22:44.431"/>
                <status status="PASS" starttime="20230124 17:22:40.786" endtime="20230124 17:22:44.432"/>
            <status status="PASS" starttime="20230124 17:22:40.783" endtime="20230124 17:22:44.433"/>
        <status status="PASS" starttime="20230124 17:22:40.757" endtime="20230124 17:22:44.434"/>
            <stat pass="1" fail="0" skip="0">All Tests</stat>
            <stat pass="1" fail="0" skip="0" id="s1" name="VSB">VSB</stat>
            <stat pass="1" fail="0" skip="0" id="s1-s1" name="Suites">VSB.Suites</stat>
            <stat pass="1" fail="0" skip="0" id="s1-s1-s1" name="simpleTest">VSB.Suites.simpleTest</stat>

def testResultsXml = new XmlSlurper().parseText(testResultsFile)

println 'parsed File'

def tests = testResultsXml.depthFirst().findAll { == 'test' }

tests.each { test ->
    println "test id: ${test.@id}"
    println "test name: ${test.@name}"
    println "test status: ${test.status.@status}"
    println "test start time: ${test.status.@starttime}"
    println "test end time: ${test.status.@endtime}"

// Loop through all the testcases in the xUnit results file

testResultsXml.testcase.each { testCase ->
    println testCase
    def testCaseName = testCase.@name
    def testCaseStatus = testCase.@status
    def testCaseTime = testCase.@time

    // Create a JSON payload for the test execution
    def jsonPayload = [
        "testCaseKey" : testCaseName,
        "status" : testCaseStatus,
        "executionTime" : testCaseTime

    println jsonPayload

    def options = [:]
    options.body = jsonPayload