cancel
Showing results for 
Search instead for 
Did you mean: 

XPath : How to assert numerical values

SOLVED
Highlighted
Contributor

XPath : How to assert numerical values

Let's say a web service returns this part of response

<result>
  <string>Good</string>
  <number>10.0</number>
</result>

For any external reasons, sometimes the answer contains <number>10</number> instead.

I would like to assert the content of //result for both number values (with or without decimals). Is there a keyword/function to use in "Expected result" to achieve that ?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

@Radford Yes you understand it correctly. Contains assertion may be an idea. As you said it is not ideal solution and in my case I think it is better to have a single assertion for "//number" instead of "//result".

View solution in original post

5 REPLIES 5
Highlighted
Super Contributor

While not set in the "Expected Result" area, you can use the XPath function number() in the XPath Expression area. This will convert the different strings "10.0" and "10" into the same number.

 

 

Highlighted
Community Leader

@Flo ,

 

You can type cast your number into integer. have a look at below example:

 

def floatNumber = 123.67

assert floatNumber.toInteger() == 123 : "Not Matched"

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Contributor

@HimanshuTayal I want to do with XPath, not in a groovy assert.

 

@Radford number() could be interesting if my expression path were "//result/number". But I would like to manage to do it with the XPath expression "//result". The thing is I want to avoid to do an assert by numeric value.

 

 

Super Contributor

Sorry @Flo I think I misinterpreted your original question. So if I understand you correctly, you want to match the entire contents of the "result" element, including all of its sub-elements in a single XPath match assertion. Is this correct?

 

If this is so, have you looked at the Contains assertion that can use a RegEx? If you definitely need to use an XPath assertion, you can use a RegEx match, if you right click in the response in the "Outline" view you can select the option "Add Assertion... > for Content matching RegEx". I know this is probably not an ideal solution, but it's the only one I can think of.

Highlighted
Contributor

@Radford Yes you understand it correctly. Contains assertion may be an idea. As you said it is not ideal solution and in my case I think it is better to have a single assertion for "//number" instead of "//result".

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors