user6221615
user6221615

Reputation: 1

How to check if a value displays in particular format in XML response in soapui

I am trying to use using script assertion or by groovy code to check if a value under an element in xml response exits in particular format.

Sample response :

                 <ns5:price>99.99</ns5:price>
                 <ns5:date>2016-04-04</ns5:date>
                 <ns5:quantity>1</ns5:quantity>

For example, I want to check if any price exists and in format 5,4, same for date to see if date in format yyyy-mm-dd and quantity is not 0.

All the values are dynamic.

I am wondering if we can use using a script assertion or can use point and click with soap ui pro.

I am just learning soapui pro and groovy.

Thanks.

Upvotes: 0

Views: 1157

Answers (1)

albciff
albciff

Reputation: 18517

You can create an script assertion in your SOAP testStep and make the validations there. In the script assertion you can parse the response using a XmlSlurper, then get the desired nodes using findAll and perform all the asserts. You can do it using something like:

// from script assertion get the response 
def response = messageExchange.getResponseContent()
// parse the XML
def xml = new XmlSlurper().parseText(response)
// get all prices
def prices = xml.'**'.findAll { it.name() == 'price' }
// check that each one has at "max 5 digits.max 4 digits"
prices.each { assert it ==~ /\d{0,5}\.\d{0,4}/ }
// get all dates
def date = xml.'**'.findAll { it.name() == 'date' }
// check the date has yyyy-MM-dd format
date.each { assert it  ==~ /\d{4}\-\d{2}\-\d{2}/ }
// get all quantities
def quantity = xml.'**'.findAll { it.name() == 'quantity' }
// check that all quantities are > 0
quantity.each { assert it.text().toInteger() > 0 }

NOTE: To add an script assertion to your request, click on Assertions tab on left down corner of your SOAP Request panel:

enter image description here

Then right click on it and select add Assertion:

enter image description here

In a new panel select Script from a left side menu and then Script Assertion on the right options:

enter image description here

Finally use the code provided and make your checks :):

enter image description here

Upvotes: 1

Related Questions