2013-06-05 60 views
1

肥皂UI(免費版)Soap UI Post Payload Groovy變量替換

我有一個測試用例,它接受XML作爲有效負載。我在測試用例中編寫了一個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> 

回答

2

可以實現在兩個方面:
只要你有Groovy腳本在REST調用之前調用的測試步驟。按照以下對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>

+0

我會建議使用TestCase來避免不必要的財產污染。 – Pafjo

+0

感謝它的工作! :) – ColinMc