lokeshchitturi
lokeshchitturi

Reputation: 21

Modifying Soap UI request using groovy

we have a requirement of finding the number of dealers in current country.In the below xml request key-value pair will be varied for every request. Inputs to the soap request will be given in .txt file.Based on the number of inputs in .txt file i need to generate key-value pair xml tags dynamically.

  **Format of Input.txt**

   1.key1=Fruit,value1=Carrot,Key2=Vegetable,value2=Carrot
   2.key1=Vegetable,value1=Potato
   3.key1=Fruit,value1=Apple,key2=Fruit,value2=Orange,key3=Fruit,value3=Mango

SoapUI Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
           <soapenv:Header/>
           <soapenv:Body>
              <web:GetCitiesByCountry>
                 <web:CountryName>Ind</web:CountryName>
                 <web:queryParameters>
                    <web:key>Fruits</web:key>
                    <web:value>Mango</web:value>
                </web:queryParameters>
                <web:queryParameters>
                    <web:key>Vegetables</web:key>
                    <web:value>Carrot</web:value>
                </web:queryParameters>
              </web:GetCitiesByCountry>
           </soapenv:Body>
        </soapenv:Envelope>

Upvotes: 2

Views: 1982

Answers (3)

Chris Adams
Chris Adams

Reputation: 1454

There's some good technical solutions here, but if you buy a SoapUI license, you'll be able to access the data driven test functionality, which does exactly what you want right out of the box. No groovy scripts required.

Upvotes: 0

craigcaulfield
craigcaulfield

Reputation: 3538

You say you have the reading of the input records sorted, so I've just put some records in a map for demonstration purposes. Then, if your request payload starts off looking like:

<soap:Envelope xmlns:soap="w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body>
        <web:GetCitiesByCountry xmlns:web="webserviceX.NET">
            <web:CountryName>hello</web:CountryName>
        </web:GetCitiesByCountry>

    </soap:Body>
</soap:Envelope>

You can loop through your input records and append them to the request:

import groovy.xml.XmlUtil

// Assuming the input document has been read into a HashMap
def map = ['Fruits': 'Banana',
           'Vegetable': 'Potato'        
          ]

// Get testStep by it's name
def testStep = context.testCase.getTestStepByName('soapRequest')

// Parse the request
def xml = new XmlSlurper().parseText(testStep.getPropertyValue('request'))

// Loop through the map and append the key/value pairs
xml.Body.appendNode {
   map.each {k, v ->
      Parameters {      
        Key(k)
        Value(v)
     }
   }
}

// Check the output
log.info(XmlUtil.serialize( xml ))

// Update the request
testStep.setPropertyValue('request',groovy.xml.XmlUtil.serialize( xml ))

Afterwards, your request will look like:

<soap:Envelope xmlns:soap="w3.org/2003/05/soap-envelope">
    <soap:Header/>
    <soap:Body>
        <web:GetCitiesByCountry xmlns:web="webserviceX.NET">
            <web:CountryName>hello</web:CountryName>
        </web:GetCitiesByCountry>
        <Parameters>
            <Key>Fruits</Key>
            <Value>Banana</Value>
        </Parameters>
        <Parameters>
            <Key>Vegetable</Key>
            <Value>Potato</Value>
        </Parameters>
    </soap:Body>
</soap:Envelope>

Upvotes: 1

Gaurav Khurana
Gaurav Khurana

Reputation: 3936

As you said you need to add

 <Parameters> <Key>key1</Key> <Value>Value1</Value> </Parameters> 

dynamically while executing groovy script

So inside your groovy script in a variable

 xmlValue="<Parameters> <Key>key1</Key> <Value>Value1</Value> </Parameters>" 
 testRunner.testCase.setPropertyValue(xmlNodes,xmlValue)

So now your dynamic xml is in testCase property. So inside your xml where you want to place it

you can place this code

  <web:CountryName>${#TestCase#xmlValue}</web:CountryName>

This way it will get added. If you pass it null,then nothing will be added in your xml.

Upvotes: 0

Related Questions