使用groovy修改Soap UI请求

时间:2018-04-07 10:25:06

标签: groovy soapui

我们要求查找当前国家/地区的经销商数量。在下面的xml请求中,每个请求的键值对都会有所不同。 soap请求的输入将在.txt文件中给出。根据.txt文件中的输入数量,我需要动态生成键值对xml标签。

  **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请求

<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>

3 个答案:

答案 0 :(得分:0)

正如你所说,你需要添加

 <Parameters> <Key>key1</Key> <Value>Value1</Value> </Parameters> 
执行groovy脚本时动态

所以在变量

中的groovy脚本中
 xmlValue="<Parameters> <Key>key1</Key> <Value>Value1</Value> </Parameters>" 
 testRunner.testCase.setPropertyValue(xmlNodes,xmlValue)

所以现在你的动态xml在testCase属性中。所以在你想要放置它的xml里面

您可以放置​​此代码

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

这样它就会被添加。如果你传递null,那么你的xml中什么都不会添加。

答案 1 :(得分:0)

你说你已经对输入记录的读数进行了排序,所以我只是将一些记录放在地图中以供演示。然后,如果您的请求有效负载开始如下:

<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>

您可以遍历输入记录并将其附加到请求中:

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 ))

之后,您的请求将如下所示:

<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>

答案 2 :(得分:0)

这里有一些很好的技术解决方案,但如果您购买SoapUI许可证,您将能够访问数据驱动的测试功能,它可以完全满足您的需求。不需要groovy脚本。