Forum Discussion
electric_Insura
15 years agoContributor
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:
Create a new XmlSlurper object that contains the Body node.
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.
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.)
More on XmlSlurper here, with quite a few helpful examples: http://groovy.codehaus.org/Reading+XML+ ... XmlSlurper
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