cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a defined variable elsewhere in a Groovy script?

New Contributor

How to use a defined variable elsewhere in a Groovy script?

I am using Groovy scripting to create output files containing test execution results from SoapUI for a given test case. My test case contains several test steps, so I'm using the following logic to iterate through each test step and collect specific information (this is a snippet from the script):

 

...

for(stepResult in testRunner.getResults())
{
// Retrieve Test Suite name
def testSuite = testRunner.testCase.testSuite.name;
// Retrieve Test Case name
def testCase = testRunner.testCase.name;
// Retrieve Test Step
def testStep = stepResult.getTestStep();
// Retrieve Test Step name
def testStepName = testStep.name;
// Retrieve Test Step type
def type = testStep.config.type;
// Retrieve Test Step status
def status = stepResult.getStatus();

// Retrieve response time

def respTime = testRunner.testCase.testSteps["Request 1"].testRequest.response.timeTaken;

...

 

The question I have revolves around the line that I've highlighted in green (the last "def" statement). Since my script will process a number of test steps, I'd like to replace "Request 1" with a variable that reflects the test step name in order to get that particular test step's response time. So, I'm trying to use "$testStepName" (defined earlier in my snippet) in place of "Request 1", but I'm not getting anything as a result. Here's how I'm trying to apply it:

 

def respTime = testRunner.testCase.testSteps["$testStepName"].testRequest.response.timeTaken;

 

Am I referencing the variable correctly in my "respTime" definition? Or, is there a different approach I should take to get the response time for each of my steps?

 

Thank you!

2 REPLIES 2
Highlighted
Frequent Contributor

Re: How to use a defined variable elsewhere in a Groovy script?

Hi @beetlejuice ,

 

All of required information are in test results.

assume that you run a test case which contains several steps, then you can retrieve what you require from result:

import com.eviware.soapui.support.types.StringToObjectMap

try {
    def testResult = testRunner.testCase.run(new StringToObjectMap(), false)
    if (testResult) {
        // print test case status
        def testCaseStatus = testResult.getStatus().toString()
         log.info testCaseStatus
        testResult.results.eachWithIndex {stepResult, index ->
            if (stepResult.testStep.metaClass.respondsTo(stepResult.testStep, "getAssertionList")) {
               // print response time
               log.info stepResult.testStep.testRequest.response.timeTaken
            } else {
               // non-REST step
              }
        }
    } else {
       // no test result
    }
} catch (Exception e) {
log.error(e)
}

 

Thanks,

/Aaron

New Contributor

Re: How to use a defined variable elsewhere in a Groovy script?

Thank you for the reply, @aaronpliu. My Groovy code is iterating through my multiple test step results using...

 

 for(stepResults in testRunner.getResults())
{
// Retrieve Test Suite name
def testSuite = testRunner.testCase.testSuite.name;
// Retrieve Test Case name
def testCase = testRunner.testCase.name;
// Retrieve Test Step
def testStep = stepResults.getTestStep();
// Retrieve Test Step name
def testStepName = testStep.name
// Retrieve Test Step type
def type = testStep.config.type
// Retrieve Test Step status
def status = stepResults.getStatus()

...

 

I had noted in a previous post (https://community.smartbear.com/t5/SoapUI-Pro/Output-Response-time-via-groovy/td-p/3500) that the following could be used to get response time:

 

testRunner.testCase.testSteps["Request 1"].testRequest.response.timeTaken

 

Since I've identified the test step name in my Groovy code as a variable called "testStepName", I'd like to create a new definition that replaces "Request 1" with the testStepName variable. Something like...

 

def respTime = testRunner.testCase.testSteps["$testStepName"].testRequest.response.timeTaken

 

The script, however, doesn't like how I'm trying to use the variable in that line. How should I invoke that variable within the brackets?

 

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors