1

肥皂用户界面(免费版)

我有一个测试用例,它是一个接受 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 负载:

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

1 回答 1

2

您可以通过两种方式实现这一点:
前提是您在 REST 调用之前调用了 Groovy 脚本测试步骤。按照以下对 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>

在后一种情况下,该属性应该是一个项目属性,所以

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

于 2013-06-05T21:19:13.887 回答