Forum Discussion

stega's avatar
stega
Contributor
13 years ago

SoapUI Groovy TestStep

Hi All,
I have a problem with the groovy scripts in SoapUI TestSteps.
I tried to write a script which returns with the number of the elements in the response.
The script works fine in the request assertion, but returns with an error running it in a TestSuit TestStep(groovy script).
I want to count the elements, insert a new element with a request and than run again the script and compare the results.
The groovy script i've tried is this:
import groovy.json.JsonSlurper

def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response

println json.size()

And i got the following error:
groovy.lang.MissingPropertyException: No such property: messageExchange for class: Script20 error at line: 3

2 Replies

  • The second groovy script i tried is the following, but when i run it, nothing happens. If i run the whole testCase it returns with FAILED:
    TestCase failed [java.lang.NullPointerException:java.lang.NullPointerException], time taken = 148

    import groovy.json.JsonSlurper

    def props = new java.util.Properties();
    props = testRunner.testCase.getTestStepByName("Properties");

    def result = testRunner.testCase.testSuite.getTestCaseByName("TestCase1").getTestStepByName("getTransactionTypeList1").run(testRunner, context);

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    //def response = messageExchange.response.responseContent
    def response =groovyUtils.getXmlHolder(testRunner.testCase.testSteps["getTransactionTypeList1"].testRequest.response.getContentAsXml());

    def slurper = new JsonSlurper()
    def json = slurper.parseText response

    def propValue = props.setPropertyValue("count", json.size());
  • messageExchange does not exist in a Groovy script step. It works in Script assertion because the step the assertion is attached to does have request/responses. But in a Groovy script step, there is no request/response associated with the Groovy script itself.

    You second script looks better where you look at the response of a particular steps. But I think you are creating too many objects. I'd put in a lot of "log.info" callouts to confirm on what happens. And by briefly looking at your script, you should at least change a line to:

    def json = slurper.parseText response.xml

    As in your case "response" is a XML placeholder object and not a string.

    Thanks,
    Michael Giller
    SmartBear Software