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 ?
Solved! Go to Solution.
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.
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,
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.
@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".