Forum Discussion

ak0032803's avatar
ak0032803
Contributor
14 years ago

How to count number of specific child nodes from Respose?

Hi,

I am getting response some what similar to follwing format.

<Body>
<Customer1>
<Location1>
<Area1>
<Buildings>
</Buildings>
<Coverages>
</Coverages>
<Coverages>
<Coverages>
....
.....
<Coverages>
<Coverages>
</Area1>

<Area2>
</Area2>
<Location1>
<Location 2>
.........
..........
</Location 2>
</Customer1>

<Customer2>

.....
..........
</Customer2>
</Body>


Now I want check how may coverages are retuned in response for all customers. There might me multiple customers, multiple areas and multiple locations. It depends on request data.
After that i want to compare it with a specific value defined for that test request.e.g. x
If it count of coverages matches with x then the test should pass else it should fail

any ideas how to do this using assertions or a groovy script ?

6 Replies

  • Are the child nodes literally named Customer1, Location1, Area1, Customer2, etc.?

    Or does the response actually look more like:


    <Body>
    <Customer>
    <Location>
    <Area>
    ...
    </Area>
    <Area>
    ...
    </Area>
    </Location>
    </Customer>
    <Customer>
    ...
    </Customer>
    </Body>
  • I just tested an assertion very much like this one against one of our responses and it worked... hope this works for you.

    In the script assertion, right click and choose Get Data. Create a variable that contains ONLY the Body node of your response. The definition will look something like this:

    def response = context.expand( '${YourWebService#Response#declare namespace ns1=\'http://NameSpaceProvider.com/NameSpace\'; //ns1:YourResponse[1]/ns1:Body[1]/}')


    Create a new XmlSlurper object that contains the Body node.

    def bodyNode = new XmlSlurper().parseText(response)


    Create a variable that contains only the Coverages nodes within the Body node. You do this by naming each child node of the Body node in turn until you get to the Coverages node. It doesn't look like Coverages is a child of Buildings; if it is, put "Buildings." before "Coverages" below.

    If one of your node names collides with a Groovy keyword (the word will have a blue highlight if this is true), put the name of the node in double quotes. I don't think you'll run into that problem with this particular response, though.

    def coveragesNodes = bodyNode.Customer.Location.Area.Coverages


    Compare the number of Coverages nodes to 10; if true, pass the assertion. (I would manually count the number of Coverages nodes in one of your responses and put that value into the assertion just to test the assertion itself.)

    assert 10 == coveragesNodes.size()


    More on XmlSlurper here, with quite a few helpful examples: http://groovy.codehaus.org/Reading+XML+ ... XmlSlurper
  • Thank for your reply. It was really helpful.
    to get the body node the suggested option for get data will not work for me.

    This is available in soapUI -pro but i am using soap-UI only.is there any method to create it in soapUI ?
    I tried to use xpath for it but it doesn't seems to be working .
    Please suggest.
  • I Got Pro Now.
    NOw I am facing follwing problem
    - I am fetching data from database e.g Customer names for each orgnization
    Data looks Like
    Row 1 -Rahul
    Row 2 - Archana

    There might be multiple rows for this.
    Now in response I am getting something like -
    <A:ORG>
    <A:Cutomer>
    <A:CustomerName>Archana</A:CustomerName>
    </A:Cutomer>
    <A:Cutomer>
    <A:CustomerName>Rahul</A:CustomerName>
    </A:Cutomer>
    </A:ORG>
    Each time the order might be different. I want to validate like Customer name which is in response is same as database name which is fetched in datasource step from database.
    As the order might change; how we can validate this through assertions?