BruceyBandit
BruceyBandit

Reputation: 4324

How to check if an xml element is displayed using a script assertion

I want to perform an assertion within SOAP UI to check if the 'BookingCode' is displayed but I am not sure how to do it. I am using .size() but it keeps failing whether there is or isn't any booking code:

Below is an xml example but I xxx out the information:

<soap:xxx">
   <soap:Body>
      <getBookingsResponse xmlns="http://xxx/">
               <Bookings>
                  <Booking Id="xxx" BookingCode="xxx" >

Below is the script assertion itself:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def serviceResponse = context.expand( '${getBookings#Response}'  )
def xml = new XmlSlurper().parseText( serviceResponse )

def BookingRef = xml.'soap:Body'.getBookingsResponse[0].Bookings[0].Booking.@BookingCode

assert BookingRef.size() != 0

Thank you

Upvotes: 0

Views: 83

Answers (1)

Rao
Rao

Reputation: 21379

Here you go, comments in-line:

Script Assertion

//Pass the xml string to parseText method
def pxml = new XmlSlurper().parseText(context.response)
//Get the BookingCode attributes
def codes = pxml.'**'.findAll{ it.name() == 'Booking' }*.@BookingCode*.text()
log.info codes
assert codes.size !=0

Upvotes: 1

Related Questions