How to assert node rather than node text

ParasofttoudayaParasofttoudaya Posts: 200 ✭✭
edited May 31 in SOAtest

Hi Team,
I have a data load scripts where i wanted to validate the count of the node name if its available.

Example,
[{
"product" : "one",
"price" : "22.00"
},
{
"product" : "two",
"price" : "10.00"
},
{
"product" : "Three",
"price" : "30.00"
},
{
"product" : "Four",
"price" : "22.00"
},
{
"product" : Five",
"price" : "25.00"
}]

**Check the node presence, **
if(root\price\name() == "price" ){
if(count("price" == "total.No.of.recordcount"){
return true;
}
}

Conditional assertion is not working for name(), it was asking for node to apply xpath assertion. But in my situation it goes as string

Comments

  • Thomas MooreThomas Moore Posts: 64 admin

    Hi Udaya,

    If you want to verify the number of the same name elements in a response, I would recommend using the Occurrence assertion under Structure Assertions in your JSON/XML Assertor. This will allow you to assert on the number of times an element will appear in a response.

    Important note: Make sure that you apply the assertion to all occurrences of the element (a message will ask you to apply to one or all).

    Let me know if you need more info for this.

    Hope this helps!

  • ParasofttoudayaParasofttoudaya Posts: 200 ✭✭

    We have parameter which tells whether it needs to check or not.

    Based on the availability flag we need to check, how to do that?

  • Thomas MooreThomas Moore Posts: 64 admin

    What kind of flag are you using for this? In the example above it only looks like the list of items from product one through product five are shown.

    Is it an element that states the number of products in this list or a true/false element or a variable set within the test? I'll need a bit more context before I can make a recommendation :smile:

  • ParasofttoudayaParasofttoudaya Posts: 200 ✭✭
    edited March 23

    Lets take an example i have 10 element to be checked, all has been controlled by an excel parameter. In that situation i have to check should i make validation? that flag comes from an excel

  • Thomas MooreThomas Moore Posts: 64 admin

    So if I have the scenario correct, it's something like this:

    You have an excel sheet that makes calls to a system and adds products into your data. at some point, you exit this loop with a number of products, say 5 from the example you provided in the original post. What we want to do is verify that 5 products have indeed been added by getting the values later.

    If this is correct, the number of elements does not matter, as we only need to add a single assertion. What we do need though is to make sure that assertion is able to change based on the number of values we expect to get back. What you can do to solve this is have a simple script that will increment the value of a test suite variable each time a new instance of the element is added. In my test, that Groovy script looks like this:

    import com.parasoft.api.*;
    public void increment(Object input, ExtensionToolContext context)
    {   
        int num = Integer.parseInt(context.getValue("x")) + 1;
        context.setValue("x", num.toString());
    }
    

    Where "x" is the name of the variable that is storing the number of elements that should exist in my response later.

    Then, on the call that I am making to verify the correct number of elements, the Occurrence Assertion looks like this:

    If I misunderstood your use case, please let me know!

Sign In or Register to comment.