Ask a Question

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

SOLVED
ngoyal
Occasional Contributor

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.

11 REPLIES 11
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)


Regards,
Rao.
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



Regards,
Rao.
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
Champion Level 3

Would you please show the screen shot of the updated script and the generated new reques?t


Regards,
Rao.
nmrao
Champion Level 3

By the way, can you change
From
new XmlSlurper()

To
new XmlSlurper(false, false)

And see (as mentioned here https://ideone.com/H9peY3)


Regards,
Rao.
ngoyal
Occasional Contributor

Thank you for the reply, when I am using XmlSlurper(false,false), I am getting NullPointerException on appending the node. In fact xml is also null. Any clue?

ngoyal
Occasional Contributor

I am able to find a work around for null issue however, @nmrao is there anyway by which I can add these new node as the first child of parent node? 

 

As of now it is adding the nodes as the last child nodes  -  

<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>                     
                     <RoomStayCandidates>
                        <RoomStayCandidate Quantity="1">
                           <GuestCounts>
                              <GuestCount AgeQualifyingCode="1" Count="1"/>
                           </GuestCounts>
                        </RoomStayCandidate>
						 <HotelRef ChainCode="ABC" HotelCode="03092"/>
                         <HotelRef ChainCode="ABC" HotelCode="03144"/>
                     </RoomStayCandidates>
                  </Criterion>
               </HotelSearchCriteria>
            </AvailRequestSegment>
         </AvailRequestSegments>
      </OTA_HotelAvailRQ>
   </soapenv:Body>
</soapenv:Envelope>

 

But I am trying to achieve like below otherwise I am getting an error "cvc-complex-type.2.4.a: Invalid content was found starting with element 'HotelRef'"

<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="03144"/>				  
                    <RoomStayCandidates>
                        <RoomStayCandidate Quantity="1">
                           <GuestCounts>
                              <GuestCount AgeQualifyingCode="1" Count="1"/>
                           </GuestCounts>
                        </RoomStayCandidate>						 
                     </RoomStayCandidates>
                  </Criterion>
               </HotelSearchCriteria>
            </AvailRequestSegment>
         </AvailRequestSegments>
      </OTA_HotelAvailRQ>
   </soapenv:Body>
</soapenv:Envelope>
nmrao
Champion Level 3

1. Not sure what null issue you are talking about.
2. What you are specifying now is not part of the question. Most of the times, solutions would directly related to data. It would be appropriate to provide full information at the beginning, so that time is saved and multiple iterations can be avoided.


Regards,
Rao.
ngoyal
Occasional Contributor

Basically null issue was appearing when I am using direct response of first request in place of passing hardcoded xml to add the node, but that is not an issue any more.

 

I wasnt aware about the community best practices and also unaware of the fact the xml request is tightly structured. Anyways I will keep these thing in mind. Thanks for helping me out. 

 

Based on your expertise by any chance is it doable?

cancel
Showing results for 
Search instead for 
Did you mean: