How to assert the length of the field.

NeilNeil Posts: 38

Hello,

In SOA test 9.9 .

Is there any specific assertion to validate the length of fields coming in response?

e.g: I want to apply and assertion below field should be always 16 digits:
123456874874834

Regards,
Neil

Comments

  • benken_parasoftbenken_parasoft Posts: 395 ✭✭✭
    edited April 2017

    In the XPath used to select or extract the field, you could use the string-length() function. In this way, you could get the field length as opposed to the field value (for your JSON or XML assertion, for example).
    https://www.w3.org/TR/xpath-functions/#func-string-length

  • NeilNeil Posts: 38

    Hello Benken,

    I tried below x-path - In string comparison assertion my response is JSON.
    fn:string-length("/root/documents/item[1]/products/item/accountNumber") retruns 7
    fn:string-length((/root/documents/item[1]/products/item/accountNumber)) retruns 7

    error : Cannot evaluate. Not a valid XPath expression.

    please advise.

  • benken_parasoftbenken_parasoft Posts: 395 ✭✭✭
    edited April 2017

    You need to change two things.
    1. Just use "string-length" (without "fn:")
    2. The Assertor tools currently expect XPaths that return a node from the DOM. However, I expect this to be enhanced sometime in the future to validate XPaths results that return other types of things, like general strings. For now, instead of using an Assertor, use an XML Converter tool to covert the JSON to XML. Next, chain an XML Transformer to the output of the XML Converter and create an extraction using your string-length() function. Lastly, chain a Diff tool to the XML Transformer to validate the value returned by the XML Transformer.

  • VinayVinay Posts: 8

    Besides what previous post suggested and explained, you can try something like this:
    Let's say I have a payload that returns array of username and password, and I want to make sure password length is exactly 8 characters for each user.
    Payload:
    {
    "users": [
    {"username" : "admin1",
    "password" : "password"},
    {"username" : "admin",
    "password" : "password2"},
    {"username" : "admin3",
    "password" : "pass"}
    ]
    }

    Solution:
    I would do chain JSON Value assertion -> Custom Assertion to my response traffic, and script (Groovy):
    def enforcePasswordLength(input){
    return input.length() == 8
    }
    And set the element locator as: "/root/users/item/password/text()"

    Test fails, and indicate an error on which element failed the condition. Something like this for above payload:

    Error Message:
    Custom Assertion: Method "enforcePasswordLength" returned false when asserting on 2nd element of
    ../password.. (Note that this assertion is commonly used to extract an entire element but it has
    currently been configured to extract the content only. Please double check if the extract
    configuration is correct.)

    Custom Assertion: Method "enforcePasswordLength" returned false when asserting on 3rd element of
    ../password.. (Note that this assertion is commonly used to extract an entire element but it has
    currently been configured to extract the content only. Please double check if the extract
    configuration is correct.)

  • NeilNeil Posts: 38

    Thanks Vinay - it's easy and simple way to validate Lengths with groovy.

Sign In or Register to comment.