How to trim (space) the variables in the the assertion section in SoapUI?

Viewed 45686

How to trim (leading and trailing spaces) the variables in the the assertion section of SoapUI/ReadyAPI ?

Ex:
Input String : "Failure   "
Output String : "Failure"

Ready API Assertion Popup

3 Answers

Use trim for trailing spaces. Use replace to get rid of spaces.

def trimExample = "Some string to be trimmed.      ";
def trimmed = trimExample.trim();

def removeSpacesExample = "Some String To Lose All Spaces."
def removedSpaces = removeSpacesExample.replace(' ', '');

Use log.info(varName) to see the effect.

After some google, I got the XSLT/XPath method which does space trim of given variables in Assertion window.

Method name : normalize-space()

Usage would be like: normalize-space(//Results[1]/ResultSet[1]/Row[1]/PAYMNT_RQST.PAYMNT_STAT_CD[1])

normalize-space function collapses whitespace in a string. Specifically, it performs three steps:

  1. Replaces each carriage return (#xD), line feed (#xA), and tab (#x9)
  2. character with a single space (#x20) Collapses all consecutive
  3. spaces into a single space Removes all leading and trailing spaces

Thanks

You are using 'Xpath Match' as of now

Could you try using 'XQuery Match' instead. it automatically trims the space

Sample Response where we have spaces after '-1 5 ' (so we will try to remove the spaces

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <ConversionRateResponse xmlns="http://www.webserviceX.NET/">
     <ConversionRateResult>-1       5    </ConversionRateResult>
  </ConversionRateResponse>
</soap:Body>
</soap:Envelope>

Put the below code in XQuery Match

declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace ns1='http://www.webserviceX.NET/';


for $x in //ns1:ConversionRateResponse
return <Result>{data($x/ns1:ConversionRateResult)}</Result>

Below is the result, where you can see spaces behind the 5 are removed

<Result>-1       5</Result>

So for your example the code will be like below inside a XQuery Match

for $x in //ns:Results[1]/ns:Resultset[1]/ns:Row[1]
return <Result>{data($x/ns:LM_ELEC_PAYMNT_PAYMNT.PLCY_STAT_CD[1])}</Result>

The best thing would be using script assertion for such or any other complex operation. But above should be helpful

Related