 |
Subscribe to this site |
|
Groovy Parse XML

Published 2 months ago
by
Jeremy
import groovy.xml.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.Duration
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)
def tests = testResultsXml.depthFirst().findAll { it.name() == 'test' }
tests.each { test ->
def testCaseName = test.@name.toString()
def testCaseStatus = test.status.@status.toString()
def testCaseStart = test.status.@starttime.toString()
def testCaseEnd = test.status.@endtime.toString()
def testCaseZephyrId = testCaseName.tokenize(' ')[0].toString()
def projectKey = testCaseZephyrId.tokenize('-')[0].toString()
def browser = test.kw.arg[1].toString()
def msg = test.kw.msg[0].toString()
def f = DateTimeFormatter.ofPattern("yyyMMdd HH:mm:ss.SSS")
def endDate = LocalDateTime.parse(testCaseEnd, f)
def startDate = LocalDateTime.parse(testCaseStart, f)
// Create a JSON payload for the test execution
def jsonPayload = [:]
jsonPayload.projectKey = projectKey
jsonPayload.testCaseKey = testCaseZephyrId
jsonPayload.status = testCaseStatus
jsonPayload.environment = browser
jsonPayload.comment = msg
jsonPayload.assignedTo = ""
jsonPayload.executedBy = ""
jsonPayload.actualStartDate = testCaseStart
jsonPayload.actualEndDate = testCaseEnd
jsonPayload.executionTime = Duration.between(startDate, endDate).toMillis()
def customFields = [:]
customFields["CI Server"] = "Jenkins"
jsonPayload.customFields = customFields
jsonPayload.issueLinks = [testCaseZephyrId]
println jsonPayload
def options = [:]
options.body = jsonPayload
}