使用 Testrunner 时,SoapUI Groovy 脚本的 JSON 响应为空

SoapUI Groovy Script’s JSON Responses Is Empty When Using Testrunner

将 Windows 7 与 Soap 5.2.0 免费软件一起使用。

我也在智能熊社区里问过这个问题,只给了推荐的帖子阅读。这些帖子与此问题无关。

我有一个 REST 项目,它有一个测试套件和一个包含两个测试步骤的测试用例。第一步是 groovy 步骤,带有调用第二个测试步骤的 groovy 脚本。第二个测试步骤是一个 REST GET 请求,它向我们的 API 服务器发送一个字符串并接收 JSON 格式的响应。第二个测试步骤有一个执行 "log.info Test Is Run" 的脚本断言,所以我可以看到第二个测试何时是 运行.

当 groovy 脚本调用第二个测试步骤时,它会读取 groovy 脚本中第二个测试步骤的 JSON 响应,如下所示:

def response = context.expand('${PingTest#Response}').toString()  // read results

我也可以用它来获得 JSON 响应:

def response = testRunner.testCase.getTestStepByName(testStepForPing).getPropertyValue("response") 

当 运行 通过 Soap UI 时,项目 运行 是预期的,但是当我 运行 项目与测试 运行ner 时,响应从 groovy 脚本调用获取 JSON 响应为空,使用上面显示的任一方法。当来自测试运行ner 的运行 时,我知道正在调用第二个测试步骤,因为我在脚本日志中看到了log.info 结果。

这是显示第二个测试步骤 运行ning 的 DOS 日志的一部分,似乎第二个测试步骤 运行.

没有错误
SoapUI 5.2.0 TestCase Runner
12:09:01,612 INFO  [WsdlProject] Loaded project from [file:/C:/LichPublic/_Soap/_EdPeterWorks/DemoPing.xml]
12:09:01,617 INFO  [SoapUITestCaseRunner] Running SoapUI tests in project [demo-procurement-api]
12:09:01,619 INFO  [SoapUITestCaseRunner] Running Project [demo-procurement-api], runType = SEQUENTIAL
12:09:01,628 INFO  [SoapUITestCaseRunner] Running SoapUI testcase [PingTestCase]
12:09:01,633 INFO  [SoapUITestCaseRunner] running step [GroovyScriptForPingtest]
12:09:01,932 INFO  [WsdlProject] Loaded project from [file:/C:/LichPublic/_Soap/_EdPeterWorks/DemoPing.xml]
12:09:02,110 DEBUG [HttpClientSupport$SoapUIHttpClient] Attempt 1 to execute request
12:09:02,111 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Sending request: GET /SomeLocation/ABC/ping?echoText=PingOne HTTP/1.1
12:09:02,977 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Receiving response: HTTP/1.1 200
12:09:02,982 DEBUG [HttpClientSupport$SoapUIHttpClient] Connection can be kept alive indefinitely
12:09:03,061 INFO  [log] **Test Is Run**

这是我在 DOS 命令行中使用的测试运行ner 调用:

“C:\Program Files\SmartBear\SoapUI-5.2.0\bin\testrunner.bat" DemoPing.xml

当 groovy 脚本通过测试 运行 时 运行ner 我使用 ProjectFactoryRegistry 和 WsdlProjectFactory 获取项目。任何关于为什么我在使用 test运行ner 时无法阅读 JSON 响应的建议将不胜感激。

如果需要,我可以提供更多info/code。

谢谢。

请尝试以下脚本:

import groovy.json.JsonSlurper
//provide the correct rest test step name
def stepName='testStepForPing'
def step = context.testCase.getTestStepByName(stepName)
def response = new String(step.testRequest.messageExchange.response.responseContent)
log.info response
def json = new JsonSlurper().parseText(response)

谢谢饶!当我使用它时,您的建议奏效了,如下所示。 DOS window 显示响应文本:

// 将 stepName 设置为测试步骤名称的变量 运行。

def stepName = "PingTest"

//使用stepName获取调用测试步骤的测试步骤(testCase是一个 测试用例名称的字符串变量)。

def 步骤 = context.testCase.getTestStepByName(步骤名称)

//调用测试步骤。

step.run(testRunner,上下文)

// 显示结果。

def response = new String(step.testRequest.messageExchange.response.responseContent)

log.info response // 此响应在 DOS 中正确显示 window

json slurper 也有效。在您方便的时候,如果您有任何描述您在此处使用的技术的建议链接或书籍,请告诉我。

谢谢。