 |
Subscribe to this site |
|
Groovy Parse Xml

Published 2 months ago
by
Jeremy
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 google.com">
<kw name="Open Browser" library="SeleniumLibrary">
<arg>"http://www.google.com"</arg>
<arg>"Edge"</arg>
<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 'http://www.google.com'.</msg>
<status status="PASS" starttime="20230124 17:22:40.993" endtime="20230124 17:22:44.431"/>
</kw>
<status status="PASS" starttime="20230124 17:22:40.993" endtime="20230124 17:22:44.431"/>
</test>
<status status="PASS" starttime="20230124 17:22:40.786" endtime="20230124 17:22:44.432"/>
</suite>
<status status="PASS" starttime="20230124 17:22:40.783" endtime="20230124 17:22:44.433"/>
</suite>
<status status="PASS" starttime="20230124 17:22:40.757" endtime="20230124 17:22:44.434"/>
</suite>
<statistics>
<total>
<stat pass="1" fail="0" skip="0">All Tests</stat>
</total>
<tag></tag>
<suite>
<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>
</suite>
</statistics>
<errors></errors>
</robot>
"""
def testResultsXml = new XmlSlurper().parseText(testResultsFile)
println 'parsed File'
def tests = testResultsXml.depthFirst().findAll { it.name() == '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
}