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

Re: XPath : How to assert numerical values

@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

Re: XPath : How to assert numerical values

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

Re: XPath : How to assert numerical values

@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, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Contributor

Re: XPath : How to assert numerical values

@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.

 

 

Highlighted
Super Contributor

Re: XPath : How to assert numerical values

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

Re: XPath : How to assert numerical values

@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
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors