Forum Discussion

Media01's avatar
Media01
New Contributor
13 years ago

[Res] Capture a Response from Groovy script to be used later

I am working on a Groovy Script that uses OAUTH for authentication to hit a web-service posting a json payload and then returns a response as a string. I am wondering how to capture the individual attributes for use later in the test. Either in a property or as a value that can be compared to a JDBC query run later to validate that the payload was loaded as we expect.
Steps
1) Groovy Script calls HttpUtils() groovy script added to SOAP UI /bin/scripts/ to handle oauth calls
2) This httpUtils() script takes the oauth Attributes and makes the postData call to the web-service returns a string of results posted
3) Use the results posted or the string returned later in the test case as attributes to verify data in the database is as expected

Script...

//imports
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
import com.eviware.soapui.support.XmlHolder;
import net.sf.*;
import net.sf.json.*;
import net.sf.json.groovy.*;
import groovy.json.JsonBuilder;
import groovy.json.JsonOutput;
import groovy.*;
import groovy.lang.GroovyObjectSupport
import com.eviware.soapui.report.JUnitReport

//Define Json Builder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def builder = new JsonGroovyBuilder()
def slurper = new JsonSlurper();

//Define json Payload for request
builder.json(){
body = "{JSON Payload}"
}
//Content Type
def contentType = "application/json"
//accept variable for return object
def accept = "application/json"

//Make call to web-service using httpUtils() for oauth
def g = HttpUtils()
response = g.postData(URL,body,contentType,accept,consumerKey,consumerSecret,tokenKey,tokenSecret)

//debug logging variables
log.info("response=" + response);
campaignResult = groovyUtils.context.expand(response);
log.info("campaignResult: " + campaignResult);


Script log.info of course this comes back with out the \n
Wed Aug 22 08:07:09 EDT 2012:INFO:request={"result":{
"id":1000,
"sfId":"23412351243",
"gpId":"afgasgfda",
"adUnit":"Ex Low",
"startDate":"2012-08-21T04:00:00Z",
"endDate":"2012-08-30T22:59:59Z",
"comment":"",
"listPrice":0.0,
"unitPrice":1.0,
"budget":250.0,
"currency":"USD",
"quantity":250,
"discount":0.0,
"thirdPartyBilled":false,
"pricingMetric":"CPC",
"name":"Test02",
"parentCampaignId":7766666333,
"salesPlanId":778877788,
"payTypeId":100333,
"creativeUrl":"",
"links":[{"rel":"parentcampaign","href":"http://localhost:8080/campaign","method":"GET"},
{"rel":"salesplan","href":"http://localhost:8080/campaign","method":"GET"}]}}

So I can see the result from the log.info("response=" + response); and log.info("campaignResult: " + campaignResult); but can not get these values transfered to another step. I have tried using slurper and parser but im not sure if that is the right way to go when attempting to use this data later for verification.

Any help would be appreciated.
  • Hi,

    If you want to expose this response for property-transfers, etc - you will need to convert it to XML and return it from the script teststep itself (exposed as the Script Result property). Have a look at how soapUI does this conversion at http://www.soapui.org/xref/com/eviware/ ... ndler.html, lines 48 to 55, you can basically do corresponding calls in groovy and then just end the script with a return statement that returns the generated XML, which should then show up in other places in soapUI where you specify a response property (for example property transfers, assertions, etc)

    Does that help?

    regards,

    /Ole
    SmartBear Software
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    If you want to expose this response for property-transfers, etc - you will need to convert it to XML and return it from the script teststep itself (exposed as the Script Result property). Have a look at how soapUI does this conversion at http://www.soapui.org/xref/com/eviware/ ... ndler.html, lines 48 to 55, you can basically do corresponding calls in groovy and then just end the script with a return statement that returns the generated XML, which should then show up in other places in soapUI where you specify a response property (for example property transfers, assertions, etc)

    Does that help?

    regards,

    /Ole
    SmartBear Software
    • maxrussell's avatar
      maxrussell
      Contributor

      i,

       

      I am looking to work with a JSON response and followed the link in that response, but got a 404 - where would that documentation be located now, please?

      • nmrao's avatar
        nmrao
        Champion Level 3
        It is recommended that not to cross post. Suggest you to create a new topic with the details of the problem.
  • Media01's avatar
    Media01
    New Contributor
    Yes, Thanks I was able to parse the request and response by using JsonSlurper().parseText and then saving the individual values off as test case ans test suite properties

    Regards,

    M