如何将 groovy 脚本的输出传递给 soap 请求



我是SoapUI的新手,正在编写时髦的脚本。在我的请求参数中,我有两个字段"从和到"。从 是当前日期,到 是一年后的日期。所以我写了一个时髦的脚本,在当前日期上增加了一年。以及我传递给请求的相同输出。请帮助我纠正错误。

我想要请求的"to"参数中的一年后日期。请详细说明,因为我是时髦和肥皂UI的新手。我已经经历了几个答案。谢谢。

use(groovy.time.TimeCategory)
{
def addYear = new Date() + 367.days
log.info addYear.format("yyyy-MM-dd") 
}

And this is my request in SoapUI: from : ${TestSuite#bt} (Its a senML request) to : ${#TestCase#addYear}

如果您使用的是时髦的步骤脚本,则可以这样做:

import groovy.json.JsonSlurper
testStep = testRunner.testCase.testSteps["YourApiRequestStep"]
def Response = testStep.getProperty("response").value;
def someFieldYouWantToSave = ""

if (Response == null) {
    log.error('No Response found.');
}
else {
    def jSlurper = new JsonSlurper();
    def json = jSlurper.parseText(Response);
    if (json.get("theFieldFromTheResponse") == null){
        log.error "TheFieldFromTheResponse not found in response. Please execute the teststep and try again"
    } else {
        someFieldYouWantToSave = json.get("theFieldFromTheResponse").toString()
        // YOUR LOGIC HERE FOR MODIFYING THE "someFieldYouWantToSave" value
        //SAVE THE FIELD
        testRunner.testCase.setPropertyValue("someFieldYouWantToSave", someFieldYouWantToSave)
    }
}

请记住,您始终可以通过查看脚本窗口的右上角位置来查看可以使用的上下文变量。例如,如果您使用的是 Groovy 脚本步骤,则变量为:日志上下文testRunner。如果您尝试在其他地方使用上述示例,例如在测试用例断言脚本中,它将不起作用,因为该示例是使用 logcontextmessageExchange 调用的。您可以通过查看文档中的示例来了解如何从项目中的各个位置获取值

有了这 3 条信息,无论您在哪个地方使用它,都应该能够实现您的目标。

最新更新