Dighate Dighate - 1 year ago 355
Groovy Question

How to use nested paramter in SoapUI context.expand expression?

My use case is that I want to do a bulk update of request bodies in multiple SoapUI projects.

Example of request body.

"name": "${#TestSuite#NameProperty}"
"id": "${#TestSuite#IdProperty}"

I want to expand the property ${#TestSuite#NameProperty} through Groovy, and get the value stored at TestSuite level then modify it as necessary.

Suppose I have 50 test steps in my test case and I want to expand the request for each one from Groovy script. To expand a specific test steps, I would pass the test Steps's name. For example:

expandedProperty = context.expand('${testStep1#Request}')

But, how can I achieve the same, if I wanted to iterate over all 50 test steps? I tried to used a nested parameter inside the context.expand expression, but it did not work. For Example:

currentTestStepName = "TestStep1"
expandedProperty = context.expand('${${currentTestStepName}#Request}')

This only returned me the expanded request from the test step right above it (where I am running the groovy script from) rather than the "TestStep1" step. ( Which is madness!)

Also, context.expand only seems to work while executing via Groovy script from the SoapUI workspace project. Is there and other way, or method similar to context.expand which can expand the properties like "${#TestSuite#NameProperty}" during headless execution? Eg: A groovy compiled jar file imported in SoapUI.

Thanks for any help in advance!

Rao Rao
Answer Source

You can use context.expand('${${currentTestStepName}#Request}') way to get it.

There are other approaches as well, which does not use context.expand.

In order to get single test step request of any given test step:

Here user passes step name to the variable stepName.

log.info context.testCase.testSteps[stepName].getPropertyValue('Request')

If you want to get all the requests of the test case, here is the simple way using the below script.

 * This script loops thru the tests steps of SOAP Request steps,
 * Adds the step name, and request to a map.
 * So, that one can query the map to get the request using step name any time later.
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
def requestsMap = [:]
context.testCase.testStepList.each { step ->
    log.info "Looking into soap request step: ${step.name}"
    if (step instanceof WsdlTestRequestStep) {
        log.info "Found a request step of required type "
        requestsMap[step.name] = context.expand(step.getPropertyValue('Request'))
log.info requestsMap['TestStep1']

Update : If the step that you are interested is REST step, use below condition instead of WsdlTestRequestStep in the above.

if (step instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) { //do the stuff }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download