bob lichtenfels
bob lichtenfels

Reputation: 3

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

Using Windows 7 with Soap 5.2.0 freeware.

I also asked about this in the Smart Bear community and was only given recommended posts to read. The posts didn’t relate to this problem.

I have a REST project that has one test suite with one test case containing two test steps. The first step is a groovy step with a groovy script that calls the second test step. The second test step is a REST GET request that sends a string to our API server and receives a response back in JSON format. The second test step has a script assertion that does "log.info Test Is Run", so I can see when the second test is run.

When the groovy script calls the second test step it reads the second test step’s JSON response in the groovy script like this:

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

I can also use this for getting JSON response:

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

The project runs as expected when run through the Soap UI but when I run the project with test runner, the response from the groovy script call to get the JSON response is empty, using either of the methods shown above. When run from testrunner, I know the second test step is being called because I see the log.info result in the script log.

This is part of the DOS log that shows the second test step is running and it seems there are no errors for the second test step run.

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

This is the testrunner call I use in DOS command line:

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

When the groovy script is run through test runner I get the project using ProjectFactoryRegistry and WsdlProjectFactory. Any advice on why I can’t read JSON response when using testrunner would be appreciated.

I can provide more info/code if needed.

Thanks.

Upvotes: 0

Views: 2333

Answers (2)

bob lichtenfels
bob lichtenfels

Reputation: 3

Thank you Rao! Your suggestion worked when I used it as shown below. The DOS window showed the response text:

// setup stepName as variable for name of test step to run.

def stepName = "PingTest"

// use stepName to get the test step for calling the test step (testCase is a string variable of the test case name).

def step = context.testCase.getTestStepByName(stepName)

// call the test step.

step.run(testRunner, context)

// show the results.

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

log.info response // this response shows correctly in the DOS window

The json slurper also works. At your convenience, if you have any suggested links or books describing the technique(s) you used here please let me know of them.

Thanks.

Upvotes: 0

Rao
Rao

Reputation: 21349

Please try the below script:

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)

Upvotes: 2

Related Questions