SoapUi Groovy conditional property transfer
I'm automating some tests and i need your help to do a groovy script with conditional property transfer
<OfferItem>
<OfferItemID>wanted_id_1</OfferItemID>
<Service>
<ServiceDetails>
<PaxJourneyRefID>PJ1</PaxJourneyRefID>
</ServiceDetails>
</Service>
<Service>
<ServiceDetails>
<ServiceDetailsRef>
<PSRID>S1</PSRID>
<ServiceID>SR_YES</ServiceID>
</ServiceDetailsRef>
</ServiceDetails>
</Service>
</OfferItem>
<OfferItem>
<OfferItemID>not_id_1</OfferItemID>
<Service>
<ServiceDetails>
<PaxJourneyRefID>PJ1</PaxJourneyRefID>
</ServiceDetails>
</Service>
<Service>
<ServiceDetails>
<ServiceDetailsRef>
<PSRID>S1</PSRID>
<ServiceID>SR_NO</ServiceID>
</ServiceDetailsRef>
</ServiceDetails>
</Service>
</OfferItem>
My XML exemple : i need to transfer the offerItemID when ServiceID == SR_YES
Thank you in advance for help.
There is another approach which finds offer id for matching service def and doesn't use xpath. You may use which ever is comfortable for you.
NOTE: have used context.response as place holder value for xml data which inside parseText(), feel free to change as needed.
Follow the comments in-line.
//Define the match / search criteria, element name and value def match = [ key:'ServiceDefinitionRefID',value:'SRV-SAF'] //Find all OfferItem 's def oItems = new XmlSlurper().parseText(context.response).'**'.findAll {it.name() == 'OfferItem'} //Get the Offer Item Id by applying search criteria defined above in match variable, returns null if no match def getOfferItemIdForMatchingElement = { oItem, name, value -> oItem.'**'.find { it.name() == name && it.text()==value } ? oItem.OfferItemID.text() : null } //Get offer item id for all Offer items, of course, there will be one as we are expecting, so filter null values def offerItemId = oItems.collect { oItem -> getOfferItemIdForMatchingElement (oItem, match['key'], match['value']) }.find {it} log.info "Offer item id: $offerItemId" assert !offerItemId, 'No offer item id found for defined match criteria' context.testCase.setPropertyValue('ITEMID', offerItemId)