質問

Soap UI (Free version)

I have a test case that is a POST that accepts XML as the payload. I wrote a groovy script in the test case that will calculate the readingDate field.

Groovy Script:

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>

Request Shown:

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>
役に立ちましたか?

解決

You can achieve that in two ways:
Provided you have the Groovy Script test step invoked before the REST Call. Follow the below modifications to the Groovy Script.

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

In the former case the xml would look like

<readingDate>${readingDate}</readingDate>

In the later case, the property should be a Project property, so

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top