Forum Discussion

Flo's avatar
Flo
Contributor
5 years ago
Solved

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 ?

  • Flo's avatar
    Flo
    5 years ago

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

5 Replies

  • Radford's avatar
    Radford
    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.

     

     

  • 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"

     

    • Flo's avatar
      Flo
      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.

       

       

      • Radford's avatar
        Radford
        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.