How to access the attribute value of soap UI respo

2019-02-26 06:32发布

I am very new to Groovy & Soap UI

I have response XML as

<carrierUDOResponse ReferenceId="" Result="SUCCESS" xmlns:xsi="" xsi:noNamespaceSchemaLocation="CarrierUDOReponse.xsd
        <errors>
            <error code="0" description="**i WANT TO ACCESS THIS**" property=""/>
        </errors>
        <warnings>
            <warning code="0" description="" property=""/>
        </warnings>
    </errorsAndWarnings>
</carrierUDOResponse>

In the Groovy script I want to capture the value of attribute and pass it to next step. How should I capture?

The groovy script is below

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Create_shipment#Response" )
// loop item nodes in response message
for( item in holder.getNodeValues( "//error.code" ))
log.info "errors : [$item]"
def no = holder["count(//error)"]

2条回答
对你真心纯属浪费
2楼-- · 2019-02-26 07:11

@Rao provides the right answer :), only to give and alternative instead of XmlHolder you can do the same with a XmlSlurper as follows:

// get the response contenxt
def content = context.expand('${Create_shipment#Response}')
// parse the content
def xml = new XmlSlurper().parseText(content)
// get descriptions
def descriptions =xml.depthFirst().findAll{it.name() == 'error'}*.@description
descriptions.each{
    log.info "description: $it"
}
查看更多
▲ chillily
3楼-- · 2019-02-26 07:29

Here is the groovy script that extracts the required description attributes using xpath.

Looks like the given xml is partial, and not well formed.

In order to show you the how to extract the data, modified a bit of your xml snippet. But basically idea is same.

  • Create the XmlHolder
  • Apply the xpath to get the value
def xml='''<carrierUDOResponse ReferenceId="" Result="SUCCESS">
        <errorsAndWarnings>
        <errors>
            <error code="0" description="Description for code 0" property=""/>
            <error code="1" description="Description for code1" property=""/>
        </errors>
        <warnings>
            <warning code="0" description="" property=""/>
        </warnings>
    </errorsAndWarnings>
</carrierUDOResponse>'''
def holder = new com.eviware.soapui.support.XmlHolder(xml)
//use the xpath to retrieve the desctiption.
def descriptions = holder.getNodeValues("//*:errorsAndWarnings/*:errors/*:error/@description")
//logging the descriptions
descriptions.each{
    log.info "Error: $it"
}

Here the variable descriptions contains the list of error descriptions.

I am adding below information based on your statement that you wanted to use these descriptions in the following step. But, the information is not completely available how your use case is. Hoping that below would be helpful too.

If you want this data to be available to the next step, the following are possibilities. But it may vary how the data is really needed in the next step. Also note that here there are list of error descriptions available.

  • Use it in another groovy script: It is possible to pass data / object from one groovy script step to other groovy step using context variable. For example you can add the descriptions to context in the above groovy script, so that the same can be retrieved in the following step.

  • Use it in another test request step (soap / rest): Here you will be able use a string data, but not really list can be used in a simple way.

查看更多
登录 后发表回答