Forum Discussion

tiro1's avatar
tiro1
New Contributor
12 years ago

How to change response meaning?

Hello!

I am begginer in soapUI and I don't know much about it. So I need your help. I have response (example in attachment). The problem is that there can be 2 meanings of response "T" or "F". If it is "T" then I need to change it to "0", if "F" then "1" and use this meaning in request of other test step. How can I do this? I will appreciate your help!

5 Replies

  • Aaronliu's avatar
    Aaronliu
    Frequent Contributor
    Hi,
    Here is a sample of groovy to set value:

    def groovyUtil = new com.eviware.soapui.support.GroovyUtils(context)
    def xmlHolder = groovyUtil.getXmlHolder('TestStepName#Response') //change into your test step name
    xmlHolder.namespaces['ns1] = 'http://www.xxxx.com' //namespace of node
    xmlHolder.namespaces['ns2'] = 'http://www.yyyy.com' //namesapce of node

    def val = xmlHolder.getNodeValue('//ns1:Node1/ns2:Node2') //get node value from response

    if (val != null){
    if(val == "T")
    xmlHolder.setNodeValue('//ns1:Node1/ns2:Node2',0) //set node value in response
    else
    xmlHolder.setNodeValue('//ns1:Node1/ns2:Node2',1)
    }




    Thanks,
    Aaron
  • tiro1's avatar
    tiro1
    New Contributor
    def groovyUtil = new com.eviware.soapui.support.GroovyUtils(context)
    def xmlHolder = groovyUtil.getXmlHolder('TestStepName#Response') //change into your test step namexmlHolder.namespaces['ns2'] = 'http://www.yyyy.com' //namesapce of node

    def val = xmlHolder.getNodeValue('//ns1:Node1/ns2:Node2') //get node value from response

    if (val != null){
    if(val == "T")
    xmlHolder.setNodeValue('//ns1:Node1/ns2:Node2',0) //set node value in response
    else
    xmlHolder.setNodeValue('//ns1:Node1/ns2:Node2',1)
    }[/code]

    In response I get ~ 60 elements, but I need to change just one "ownerTypeFromCSDD" and somewhere is a mistake because I get response NULL. And I need the response is integer, not string like it was before.
  • Aaronliu's avatar
    Aaronliu
    Frequent Contributor
    so you need to navigate to "ownerTypeFromCSDD" node, then you will get one value rather than 60. Cuz invoking is getNodeValue() rather than getNodeValues().To change your xpath expression in method. If type of return value is String, using .toInteger() to convert into Integer.
    example:
    def num = val.toInteger() //precondition is you successfully set "T" or "F" into 1 or 0,or throw exception

    Thanks,
    Aaron
  • Aaronliu's avatar
    Aaronliu
    Frequent Contributor
    Hi Nadia,

    I refer to a sample web service to show what you talked. I don't know why I cannot attach img, so have to use text to illustrate here:

    Example:
    Endpoint: http://www.webservicex.net/uszip.asmx?wsdl
    (Not sure if you can access this web service)

    Test case consist of 2 steps:
    step1: GetInfoByCity - Request 1
    step2: Groovy Script - Change Response

    Request:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
    <soapenv:Header/>
    <soapenv:Body>
    <web:GetInfoByCity>
    <!--Optional:-->
    <web:USCity>Houston</web:USCity>
    </web:GetInfoByCity>
    </soapenv:Body>
    </soapenv:Envelope>


    Response:
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <GetInfoByCityResponse xmlns="http://www.webserviceX.NET">
    <GetInfoByCityResult>
    <NewDataSet xmlns="">
    <Table>
    <CITY>Houston</CITY>
    <STATE>AK</STATE>
    <ZIP>99694</ZIP>
    <AREA_CODE>907</AREA_CODE>
    <TIME_ZONE>K</TIME_ZONE>
    </Table>
    </NewDataSet>
    </GetInfoByCityResult>
    </GetInfoByCityResponse>
    </soap:Body>
    </soap:Envelope>

    Scripts:

    def groovyUtil = new com.eviware.soapui.support.GroovyUtils(context)
    def xmlHolder = groovyUtil.getXmlHolder('GetInfoByCity - Request 1#Response')
    xmlHolder.namespaces["ns1"] = "http://www.webserviceX.NET"
    def city = xmlHolder.getNodeValue("//ns1:GetInfoByCityResult/NewDataSet/Table/CITY")

    if (city != null){
    if (city == "Houston")
    xmlHolder.setNodeValue("//ns1:GetInfoByCityResult/NewDataSet/Table/CITY",0)
    else
    xmlHolder.setNodeValue("//ns1:GetInfoByCityResult/NewDataSet/Table/CITY",1)
    log.info xmlHolder.getNodeValue("//ns1:GetInfoByCityResult/NewDataSet/Table/CITY")
    }


    Output:
    0


    Thanks,
    Aaron