SOAP UI变量(例如,日期)

时间:2015-03-10 17:27:12

标签: soap groovy soapui

我正在使用SOAP UI Pro。我有一个请求,这是一个摘录:

               <ns2:OfficeType>
                   <ns2:OfficeTypeCode>M</ns2:OfficeTypeCode>
                   <ns2:EffectiveDate>2000-01-01</ns2:EffectiveDate>
                   <ns2:TerminationDate>9999-12-31</ns2:TerminationDate>
                   <ns2:IsPrimary>true</ns2:IsPrimary>
                </ns2:OfficeType>

而不是2000-01-01,我想今天使用(因此,我写这篇文章的那天将是2015-03-10。你可能会在3月12日阅读,在这种情况下它会是2015-03-12等)。理论上,我每次发送SOAP请求时都可以编辑SOAP请求,并用当前日期替换字段,但这应该是自动的。我想说一句话,而不是2010-01-01使用像$(今天&#39; YYYY-MM-DD&#39;)这样的东西(这只是一个可能与实际语法无关的例子)。

有没有办法将当前日期放入SOAP UI Pro的SOAP UI消息中?

3 个答案:

答案 0 :(得分:3)

在SOAPUI中,您可以使用以下符号${=groovy expression}直接在SOAP请求中使用groovy代码,因此在您的情况下,您可以使用${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())}yyyy-MM-dd格式获取今天的日期。< / p>

直接在您的测试请求中,您可以使用:

<ns2:OfficeType>
    <ns2:OfficeTypeCode>M</ns2:OfficeTypeCode>
    <ns2:EffectiveDate>${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date())}</ns2:EffectiveDate>
    <ns2:TerminationDate>9999-12-31</ns2:TerminationDate>
    <ns2:IsPrimary>true</ns2:IsPrimary>
</ns2:OfficeType>

希望这有帮助,

答案 1 :(得分:0)

如果您使用的是SoapUI Pro,那么您可能在 TestRunListener.beforeRun 事件

中有以下脚本

import java.text.SimpleDateFormat
def today = new Date()
def dateFormat = new SimpleDateFormat("yyyy-MM-dd")
def date = dateFormat.format(today)
log.info date
testRunner.testCase.setPropertyValue('DATE', date)

并且在请求中

<ns2:EffectiveDate>${#TestCase#DATE}</ns2:EffectiveDate> 即,使用在beforeRun事件中设置的值DATE

注意:您可以随意更改日期格式,在此处使用问题中提到的yyyy-MM-dd格式。

答案 2 :(得分:0)

这将为您提供一个很好的UTC格式的日期/时间:

<ns2:EffectiveDate>${=java.time.Instant.now().truncatedTo(java.time.temporal.ChronoUnit.SECONDS)}</ns2:EffectiveDate>