Forum Discussion

ngoyal's avatar
ngoyal
Occasional Contributor
6 years ago

Modify Teststep request to add new nodes with attributes using groovy script

Team I am facing an issue where I am able to fetch muliple values of an attibute from reponse using groovy script and want to create new nodes with attributes in next test step request. I have gone throught the earlier post before creating this new post but none of them provide concreate solution.

 

Response XML -

 

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soap:Body>
      <ns2:OTA_HotelSearchRS AltLangID="en" EchoToken="20BB36F8AC152AC46E95033289D89328" PrimaryLangID="en" RetransmissionIndicator="false" Target="Test" TimeStamp="2019-04-07T15:35:51.869-07:00" TransactionIdentifier="SmokeTestHotelSearch" TransactionStatusCode="" Version="1.003" xmlns:ns2="http://www.opentravel.org/OTA/2003/05">
         <ns2:Success/>
         <ns2:Properties>
            <ns2:Property ChainCode="ABC" HotelCode="03062">
               <ns2:RelativePosition Distance="3.0" Name="City Center" UnitOfMeasureCode="1"/>
            </ns2:Property>
            <ns2:Property ChainCode="ABC" HotelCode="03092">
               <ns2:RelativePosition Distance="7.44" Name="City Center" UnitOfMeasureCode="1"/>
            </ns2:Property>
            <ns2:Property ChainCode="ABC" HotelCode="03144">
               <ns2:RelativePosition Distance="8.04" Name="City Center" UnitOfMeasureCode="1"/>
            </ns2:Property>
         </ns2:Properties>
      </ns2:OTA_HotelSearchRS>
   </soap:Body>
</soap:Envelope>

 

Groovy Script to fetch Hotel code -

import groovy.xml.*

def response = testRunner.testCase.getTestStepByName("HotelSearch_2011B").getProperty("Response").getValue()
def parsedXml = new XmlSlurper().parseText(response)
def HotelCodes = parsedXml.'**'.findAll{ it.name() == 'Property' }*.@HotelCode
log.info HotelCodes.size()


def testStep = context.testCase.getTestStepByName('AreaAvail_2011B')

// Parse the request
def xml = new XmlSlurper().parseText(testStep.getPropertyValue('request'))

for(hotel in HotelCodes) {
xml.Criterion.appendNode {   
 HotelRef.@ChainCode="ABC"
 HotelRef.@HotelCode=hotel 
 }

}

log.info XmlUtil.serialize(xml)

Next Test Step - AreaAvail request xml  - before

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">
   <soapenv:Header/>
   <soapenv:Body>
      <OTA_HotelAvailRQ EchoToken="1234" RateRangeOnly="true" Target="Test" TimeStamp="2006-07-04T09:00:27.1343316+02:00" TransactionIdentifier="b123456789" Version="4.000" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_HotelAvailRQ.xsd" xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <AvailRequestSegments> 
            <AvailRequestSegment>
               <HotelSearchCriteria>
                  <Criterion>   
                  </Criterion>
               </HotelSearchCriteria>
            </AvailRequestSegment>
         </AvailRequestSegments>
      </OTA_HotelAvailRQ>
   </soapenv:Body>
</soapenv:Envelope>

 

Output I am trying to get -

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.opentravel.org/OTA/2003/05">
   <soapenv:Header/>
   <soapenv:Body>
      <OTA_HotelAvailRQ EchoToken="1234" RateRangeOnly="true" Target="Test" TimeStamp="2006-07-04T09:00:27.1343316+02:00" TransactionIdentifier="b123456789" Version="4.000" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_HotelAvailRQ.xsd" xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <AvailRequestSegments> 
            <AvailRequestSegment>
               <HotelSearchCriteria>
                  <Criterion>
                     <HotelRef ChainCode="ABC" HotelCode="03092"/>
                     <HotelRef ChainCode="ABC" HotelCode="03062"/>

                     <HotelRef ChainCode="ABC" HotelCode="03144"/> 

                  </Criterion>
               </HotelSearchCriteria>
            </AvailRequestSegment>
         </AvailRequestSegments>
      </OTA_HotelAvailRQ>
   </soapenv:Body>
</soapenv:Envelope>

I am using soapui opensource.

  • ngoyal ,

     

    Get the list of ChainCode and HotelCode and put them in a map.

    Then loop thru map and append nodes.

     

    You were almost there, but trivial error.

     

    Here is the script that you try & test online

     

    //You need to build the map from previous response
    def map = ['03062': 'ABC', '03092': 'ABC', '03144': 'ABC']
     
    criteria = xml.'**'.find {'Criterion' == it.name()}
    criteria.appendNode {
    	map.each { k, v ->
    		HotelRef('ChainCode' : v, 'HotelCode': k)
    	}
    }
    log.info groovy.xml.XmlUtil.serialize(xml)
  • nmrao's avatar
    nmrao
    Champion Level 3

    ngoyal ,

     

    Get the list of ChainCode and HotelCode and put them in a map.

    Then loop thru map and append nodes.

     

    You were almost there, but trivial error.

     

    Here is the script that you try & test online

     

    //You need to build the map from previous response
    def map = ['03062': 'ABC', '03092': 'ABC', '03144': 'ABC']
     
    criteria = xml.'**'.find {'Criterion' == it.name()}
    criteria.appendNode {
    	map.each { k, v ->
    		HotelRef('ChainCode' : v, 'HotelCode': k)
    	}
    }
    log.info groovy.xml.XmlUtil.serialize(xml)
  • nmrao's avatar
    nmrao
    Champion Level 3

    ngoyal ,

     

    In order to build map from previous response (as mentioned in my earlier reply), use below snippet 

    Change from 

    def HotelCodes = parsedXml.'**'.findAll{ it.name() == 'Property' }*.@HotelCode

    To

     

    def map = parsedXml.'**'.findAll {'Property' == it.name()}.collectEntries{ [(it.@HotelCode): it.@ChainCode]}
    log.info map

    You may see full script here to test

    • ngoyal's avatar
      ngoyal
      Occasional Contributor

      Thank you man, it has solved the issue. Appreciate your quick reply.

       

      I have one quick observation. The newly added nodes are not prefixed with "ns:" and rest of the old tags are prefixed with ns in new request xml, because of which my request xml is failing. Any pointer? 

      • nmrao's avatar
        nmrao
        Champion Level 3
        Would you please show the screen shot of the updated script and the generated new reques?t