Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to assert node rather than node text

Parasofttoudaya
Parasofttoudaya Posts: 232 ✭✭
edited May 2018 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 Moore
    Thomas Moore Posts: 82 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!

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭

    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 Moore
    Thomas Moore Posts: 82 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:

  • Parasofttoudaya
    Parasofttoudaya Posts: 232 ✭✭
    edited March 2018

    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 Moore
    Thomas Moore Posts: 82 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!