Burned357Waffle
6 years agoNew Contributor
How to get a specific response from a TestCase with Groovy Script
Hello, I am trying to get a response from one of my TestCases using Groovy Script. I have the following and it gives me the Xml, but how do i pull out only the serial_number?
String temp = testRunner.testCase.testSteps.("TestCase").testRequest.response.getContentAsXml();
log.info(temp)
in the log i get this:
Tue Jul 16 23:27:51 UTC 2019: INFO: <Response xmlns="http://localhost/v1.0.0/vnms/">
<serial_number>102930478390026</serial_number>
<site_id>a2db3129-172a-704b994c</site_id>
</Response>
There are several ways to interact with XML strings, my preferred approach is the XmlSlurper, the following is a standalone example with your XML data:
import groovy.util.XmlSlurper def xmlData = '''\ <Response xmlns="http://localhost/v1.0.0/vnms/"> <serial_number>102930478390026</serial_number> <site_id>a2db3129-172a-704b994c</site_id> </Response>''' def Response = new XmlSlurper().parseText(xmlData) def serialNumber = Response.serial_number.text() log.info('Serial Number = ' + serialNumber)
The following page has all the details about the XmlSlurper