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>
我會建議使用TestCase來避免不必要的財產污染。 – Pafjo
感謝它的工作! :) – ColinMc