如何在soapui

时间:2016-12-07 12:51:35

标签: web-services groovy soapui

我必须在Soap UI请求中添加3条记录,如下所示:

<soapenv:Envelope>
<soapenv:Header/>
<soapenv:Body>
<wor:AddValues>
<wor:User>
         <data:userid>${Properties1#UserID}</data:userid>
</wor:User>
<data:ValuesList>
<data:Value>
        <data:Desc>${Properties1#Desc1}</data:Desc>
        <data:Details>${Properties1#Detail1}</data:Details>
</data:Value>
<data:Value>
        <data:Desc>${Properties1#Desc2}</data:Desc>
        <data:Details>${Properties1#Detail2}</data:Details>
</data:Value>
<data:Value>
        <data:Desc>${Properties1#Desc3}</data:Desc>
        <data:Details>${Properties1#Detail3}</data:Details>
</data:Value>
</data:ValuesList>
</wor:AddValues>
</soapenv:Body>
</soapenv:Envelope>

完成后,这将为每个添加的值生成唯一ID。 回复如下所示:

<s:Envelop>
<s:Body>
<AddUserValueResult>
<a:ValuesList>
<a:Value>
        <a:Id>2501</a:Id>        
        <a:Desc>Desc1</a:Desc>
        <a:Details>Detail1</a:Details>
</a:Value>
<a:Value>
        <a:Id>2502</a:Id>        
        <a:Desc>Desc2</a:Desc>
        <a:Details>Detail2</a:Details>
</a:Value>
<a:Value>
        <a:Id>2503</a:Id>        
        <a:Desc>Desc3</a:Desc>
        <a:Details>Detail3</a:Details>
</a:Value>
</a:ValuesList>
</AddUserValueResult>
</s:Body>
</s:Envelop>

使用groovy脚本我想使用属性Desc1&amp;点评详情, 将其与响应进行比较,如果匹配,则获取该记录的ID,并将响应中生成的ID存储在另一个Property Id1中。 对其他两条记录继续相同。 你能帮助我实现这个目标吗? 在此先感谢!

1 个答案:

答案 0 :(得分:0)

以下是为了能够比较请求和响应中的数据而做的事情:

  • 创建一个对象模型,以便将请求和响应都转换为对象列表,然后进行比较。
  • 从请求/响应中提取所需数据并构建对象列表
  • 需要考虑一个事实,即请求和响应都包含数据/值列表,并且它可能以任何随机顺序出现。因此,该对象应具有可比性。

在这种情况下,Script Assertion可用于比较而无需任何额外的测试步骤。因此,将脚本断言添加到Soap Request测试步骤。

这是脚本:

/** 
* This is script assertion
* Reads both request and response
* Builds the object list, so that both can be compared
**/

//Object Model, req / resp is transformed into this model
@groovy.transform.Canonical
@groovy.transform.Sortable
class Model {
  String desc
  String details
}

//Closure to retrieve the data element of user choice from xml
def searchData = { data, element ->
  def parsedData = new XmlSlurper().parseText(data)
  parsedData.'**'.findAll{it.name() == element}
}

//Closure to build the object list of above provided Model class
def buildData = { list, desc, details ->
   def objects = []
   list.each { item ->
      objects << new Model(desc: item."$desc", details: item."$details")
   }
   objects
}

//Assert if the request and response are non-empty
assert context.rawRequest, "Request is empty or null"
assert context.response, "Response is empty or null"

//Get the Value elements from both request and response
def reqValues = searchData(context.rawRequest, 'Value')
def respValues = searchData(context.response, 'Value')

//Build the Model object list for both request and response and sort them to be able to compare
def reqObjectList = buildData(reqValues, 'Desc', 'Details' ).sort()
def resObjectList = buildData(respValues, 'Desc', 'Details' ).sort()

//Log the above
log.info "Value objects from request: ${reqObjectList.toString()}"
log.info "Value objects from response: ${resObjectList.toString()}"

//Finally, compare the data from both request and response
assert reqObjectList == resObjectList, "Data is not matching"

 //Now set the Ids at test case level custom properties
 respValues.eachWithIndex { value, index -> 
    log.info "Id${index+1} : ${value.Id as String}"
    context.testCase.setProperty("Id${index+1}", "${value.Id as String}")
 }

现在您可以使用${#TestCase#Id1}等..您需要这些值。 请注意:只有当两个ValuesList匹配时才会保存这些ID 您可以快速从 Demo 中试用,直到比较请求和响应valueslist