0

I am new to SoapUI and writing groovy scripts. In my request parameters, I have two fields "from and to". from is the current date and to is one year later date. so I have written a groovy script to add one year to the current date. and the same output I am passing to the request. Please help me in correcting the mistake.

I want the one year later date in "to" parameter of the request. Please elaborate as I am new to groovy and soapUI. I have gone through several answers. Thank you.

use(groovy.time.TimeCategory)
{
def addYear = new Date() + 367.days
log.info addYear.format("yyyy-MM-dd") 
}

And this is my request in SoapUI: from : ${TestSuite#bt} (Its a senML request) to : ${#TestCase#addYear}

Nomi Ali
  • 2,165
  • 3
  • 27
  • 48
dp1005
  • 1
  • 1
  • Is this resolved ? or are you still on the lookout for answers ? – Wilfred Clement Apr 22 '19 at 10:17
  • Since you can add a script basically anywhere in SoapUI (Project/TestSuite/TestCase setup/teardown, test case step, test step assertion), can you specify where exactly are you trying to run that script? Regarding the property expansions, you can find more info on the soapui [documentation page:](https://www.soapui.org/scripting-properties/property-expansion.html) – Cartitza Apr 23 '19 at 14:27

1 Answers1

0

If you're using a groovy step script, you can do it like this:

import groovy.json.JsonSlurper

testStep = testRunner.testCase.testSteps["YourApiRequestStep"]

def Response = testStep.getProperty("response").value;

def someFieldYouWantToSave = ""


if (Response == null) {
    log.error('No Response found.');
}
else {
    def jSlurper = new JsonSlurper();
    def json = jSlurper.parseText(Response);
    if (json.get("theFieldFromTheResponse") == null){
        log.error "TheFieldFromTheResponse not found in response. Please execute the teststep and try again"
    } else {
        someFieldYouWantToSave = json.get("theFieldFromTheResponse").toString()

        // YOUR LOGIC HERE FOR MODIFYING THE "someFieldYouWantToSave" value

        //SAVE THE FIELD
        testRunner.testCase.setPropertyValue("someFieldYouWantToSave", someFieldYouWantToSave)
    }
}

Keep in mind that you can always see what context variables you can use by looking in the upper - right location of the script window. For example, if you're using a Groovy Script step, the variables are: log, context and testRunner. If you try to use the above example somewhere else, like in the test case assertion script, it won't work since that one is invoked with log, context and messageExchange. You can find out how to grab the values from various places in your project by taking a look at the examples from the documentation

With these 3 pieces of information, you should be able to achieve your goal, regardless of the place you're using it.

Cartitza
  • 96
  • 4