Forum Discussion

smilnik's avatar
smilnik
Occasional Contributor
2 months ago
Solved

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> ...
  • nmrao's avatar
    nmrao
    2 months ago

    smilnik 

    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)