75332

Groovy Script assert on Key/Value pairs in SOAP UI XML response

I am using Ready API/SOAP UI. I added a SOAP request and I get a SOAP response XML. My response object has up to 40 Key/Value pairs.

I have functional tests to specifically test each.

    <li>Loop through the whole ArrayOfObjects and Assert if the Key exists and if it exists assert the value.</li> </ul>

    Can I get a working solution for this scenario. I am unable to do assert on the output object.

    SOAP structure looks like this:

    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <ArrayOfallObjects> <ArrayOfObjects> <Key>Key1</Key> <Value>Value1</Value> </ArrayOfObjects> <ArrayOfObjects> <Key>Key2</Key> <Value>Value2</Value> </ArrayOfObjects> --------------- <ArrayOfObjects> <Key>Key40</Key> <Value>Value40</Value> </ArrayOfObjects> </ArrayOfallObjects> </soap:Body> </soap:Envelope>

    And I am using groovy script snippet as below

    //Code Snippet Starts// def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def request = context.testCase.getTestStepByName("RequestName") def responseCurrentHolder = groovyUtils.getXmlHolder( request.name +"#Response") responseCurrentHolder.namespaces["ns1"] = "https://test.com" def nodes = responseCurrentHolder.getDomNodes( "//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject/*" ) def nodeCount = responseCurrentHolder.getNodeValues("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject/ns1:Key").length def object = [:] for (def nodeIndex = 1; nodeIndex <= nodeCount; nodeIndex++) { def nodeKey = responseCurrentHolder.getNodeValues("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject[$nodeIndex]/ns1:Key/text()") def nodeValue = responseCurrentHolder.getNodeValue("//ns1:Response/ns1:Result/ns1:ArrayOfallObjects/ns1:ArrayOfObject[$nodeIndex]/ns1:Value/text()") object.put( nodeKey,nodeValue) } log.info "Object =" +object // Code snippet ends//

    And the object looks like:

    Object =[[Key1]:Value1, [Key2]:Value2, and so on upto --,[Key40]:Value40]

    Answer1:

    You can use Script Assertion for the same soap request test step to verify the same without adding additional Groovy Script test step.

    Not sure if above sample has the same xml structure (if not exact). Otherwise, adopt the changes to suit your need.

    Here is the approach:

      <li>Since there is complex data to be verified, and there is key, value pattern; so define a map as expected data like you pointed in the last.</li> <li>Read the xml response, and create similar map out of it. At times, it may require to sort the retrieved data by key (or other criteria based on the data).</li> <li>Then check if both expected and actual data.</li> </ul>

      <strong>Script Assertion:</strong>

      //Define expected data; using few elements as sample def expectedMap = [Key1: 'Value1', Key2: 'Value2', Key40: 'Value40'] //Check if there is response assert context.response, 'Response is empty or null' //Parse the response def xml = new XmlSlurper().parseText(context.response) //Extract the data, create actual map and sort by key def actualMap = xml.'**'.findAll {it.name() == 'ArrayOfObjects' }.collectEntries {[(it.Key.text()): it.Value.text()]}​?.sort {it.key} log.info actualMap assert expectedMap == actualMap

      You can quickly try it online demo</kbd>

      Answer2:

      The below code first get all keys in an array(x) and all values in another array y(x). You may add them in a map if not you can directly assert values

      The below is one of the easiest and simpler way of validating key value pair

      Just replace the 'First Step' with the name of the step where response is generated

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def response = groovyUtils.getXmlHolder("First Step#Response") def x = response.getNodeValues("//*[local-name()='ArrayOfObjects']//*[local-name()='Key']") for ( def keys in x) { log.info keys } def y = response.getNodeValues("//*[local-name()='ArrayOfObjects']//*[local-name()='Value']") for ( def values in y) { log.info values } log.info "total keys are " + x.size() for(int i = 0 ; i < x.size() ; i++) { log.info " key = " + x[i] + " Value = " + y[i] }

      Here is the result when i ran the above script

      Sat Oct 28 14:41:57 IST 2017:INFO:Key1

      Sat Oct 28 14:41:57 IST 2017:INFO:Key2

      Sat Oct 28 14:41:57 IST 2017:INFO:Key40

      Sat Oct 28 14:41:57 IST 2017:INFO:Value1

      Sat Oct 28 14:41:57 IST 2017:INFO:Value2

      Sat Oct 28 14:41:57 IST 2017:INFO:Value40

      Sat Oct 28 14:41:57 IST 2017:INFO:total keys are 3

      Sat Oct 28 14:41:57 IST 2017:INFO: key = Key1 Value = Value1

      Sat Oct 28 14:41:57 IST 2017:INFO: key = Key2 Value = Value2

      Sat Oct 28 14:41:57 IST 2017:INFO: key = Key40 Value = Value40

Recommend