Forum Discussion

Burned357Waffle's avatar
Burned357Waffle
New Contributor
5 years ago
Solved

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 = test...
  • Radford's avatar
    5 years ago

    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

     

    https://groovy-lang.org/processing-xml.html