使用Testrunner时SoapUI Groovy Script的JSON响应为空

使用Testrunner时SoapUI Groovy Script的JSON响应为空

问题描述:

使用带有Soap 5.2.0免费软件的Windows 7。使用Testrunner时SoapUI Groovy Script的JSON响应为空

我也在智能熊社区询问过这个问题,只给了推荐文章阅读。这些帖子并没有涉及到这个问题。

我有一个REST项目有一个测试套件,其中一个测试案例包含两个测试步骤。第一步是通过一个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时按预期运行,但是当我使用测试运行器运行项目时,使用上述任一方法调用groovy脚本获取JSON响应的响应为空。当从testrunner运行时,我知道正在调用第二个测试步骤,因为我在脚本日志中看到了log.info结果。

这是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** 

这是TestRunner的电话我在DOS命令行中使用:

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

当Groovy脚本通过测试跑步跑我得到使用ProjectFactoryRegistry和WsdlProjectFactory项目。任何意见,为什么我不能读取使用testrunner时的JSON响应,将不胜感激。

如果需要,我可以提供更多信息/代码。

谢谢。

+0

你的意思是说你不能在cmd控制台中看到它? –

+0

拉奥给了我一个解决方案,我在下面展示。谢谢你看这个。我只显示了DOS窗口的一部分,应该说我做了这个 - log.info响应 - 并且在响应变量的DOS窗口中没有看到任何东西。 –

请尝试以下脚本:

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窗口显示响应文本:

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

DEF stepName =“PingTest”

//使用stepName获得用于调用测试步骤测试步骤(测试用例是测试用例名称的字符串 变量)。

DEF步骤= context.testCase.getTestStepByName(stepName)

//调用测试步骤。

步骤。运行(testRunner,上下文)

//显示结果。

DEF响应=新字符串(step.testRequest.messageExchange.response.responseContent)

log.info响应//该响应正确地显示在DOS窗口

的JSON slurper也有效。在您方便时,如果您有任何建议的链接或书籍描述您在此使用的技术,请让我知道它们。

谢谢。

+0

如果您接受答案,将不胜感激。 – Rao

+0

对不起,我刚刚发现如何做到这一点。我想我得到了这个确定。 –