4

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

Arbind Singh
  • 153
  • 1
  • 2
  • 12
  • Do you mean trim or remove spaces from the value assigned to a var? – Chris Adams Oct 06 '17 at 15:38
  • I mean removing spaces from the value assigned to a variable. – Arbind Singh Oct 06 '17 at 15:41
  • 1
    Would you mind providing the sample data (in the form of text, not image) and mention what kind of assertion are you using? And the issue? Issue neither clear from summary / description nor from your comments from below answer. – Rao Oct 07 '17 at 01:51

3 Answers3

15

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.

Chris Adams
  • 1,376
  • 1
  • 8
  • 15
1

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

Arbind Singh
  • 153
  • 1
  • 2
  • 12
0

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

Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
  • 1
    Thanks for the response. I got a simple method as i mentioned below. So could not try this. I am sure this will also help to resolve this issue. – Arbind Singh Oct 09 '17 at 14:11