Soap UI Post Payload Groovy变量替换

时间:2013-06-05 19:09:42

标签: groovy soapui dynamic-variables

Soap UI(免费版)

我有一个测试用例,它是一个接受XML作为有效负载的POST。我在测试用例中编写了一个groovy脚本来计算readingDate字段。

Groovy脚本:

import java.text.SimpleDateFormat  
import java.util.Calendar  
import java.util.TimeZone  

Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
dateFormat.setTimeZone(currentTime.getTimeZone())
String readingDate = dateFormat.format(currentTime.getTime())
context.setProperty("readingDate", readingDate)

XML Payload:

<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns="http://www.foo.com/data">
  <foobar foobarId="1553310377_20">
    <foobarReading>
      <readingDate>${readingDate}</readingDate>
      <readingValue>451.045</readingValue>
    </foobarReading>
  </foobar>
</foo>

请求显示:

POST http://foo:8080/WebServices/rest/data HTTP/1.1  
Connection: close  
Accept-Encoding: gzip,deflate  
Content-Type: application/xml  
Authorization: Basic TlJDYW46TlJDYW4=  
Content-Length: 301  
Host: foo:8080  
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)  


    <?xml version="1.0" encoding="UTF-8"?>
    <foo xmlns="http://www.foo.com/data">
      <foobar foobarId="1553310377_20">
        <foobarReading>
          <!-- Should not be empty it should show the current time -->
          <readingDate></readingDate>
          <readingValue>451.045</readingValue>
        </foobarReading>
      </foobar>
    </foo>

1 个答案:

答案 0 :(得分:2)

您可以通过两种方式实现这一目标:
如果您在REST调用之前调用了Groovy Script测试步骤。按照以下对Groovy脚本的修改。

//Instead of setting the property to context
//context.setProperty("readingDate", readingDate)

//Approach 1:
//Set it as a global property for SoapUI
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue('readingDate', readingDate)

//Approach 2:
//Or if you do not want to set it globally and ruin other tests
//Set it as a project (SoapUI Project) property.
//testRunner.testCase.testSuite.project.setPropertyValue('readingDate', readingDate)

在前一种情况下,xml看起来像

<readingDate>${readingDate}</readingDate>

在后一种情况下,属性应该是Project属性,所以

<readingDate>${#Project#readingDate}</readingDate>