cancel
Showing results for 
Search instead for 
Did you mean: 

Need help re-using a response item in other tests

SOLVED
Highlighted
Occasional Contributor

Need help re-using a response item in other tests

I'm a new user that has been tasked with learning SoapUI Pro (ReadyAPI) to test our REST API.

 

We have used tools like Postman and Telerik to set variables that can be easily reused elsewhere, and I'm trying to do the same within SoapUI and not having any success.

 

I send a POST request with a username and password and get a response with an access_token, I am trying to capture and use this token to then use to pass in our other GET & POST requests. Some of the documentation I've found for this is both outdated and still confusing for the new user.

In JSON the response item I'm trying to use looks as below. I need to capture the access_token to use in other areas.

{
"access_token" : "r3@llyL0nGR@ndoMStUffH3r3",
"token_type" : "bearer",
"expires_in" : 1209599,
"userId" : "12345",
".issued" : "Fri, 10 Aug 2018 12:16:22 GMT",
".expires" : "Fri, 24 Aug 2018 12:16:22 GMT"
}

 

I'm pretty sure this involves the property transfer item, but I need some more detailed step by step information to make this work, as my attempts have failed.
in postman we have it setup to do this in the Header:

Authorization - Bearer {{access_token}}

4 REPLIES
New Contributor

Re: Need help re-using a response item in other tests

I Solved this by creating a groovy script that sets the payload for all teststeps. My script is as follows:

 

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper
import com.eviware.soapui.support.types.StringToStringMap 

//get response from "Get Token" step (token)
def token = context.expand( '${Get Token#Response}' ).toString()

def slurper = new JsonSlurper()
def json = slurper.parseText(token)
//log.info json.access_token

//construct accessToken
accessToken = json.token_type + " " + json.access_token

//set header for Login step
def header = new StringToStringMap()
header.put("authorization", accessToken)

//set accessToken in each rest call
testRunner.testCase.testSteps.each
{
	if (it.getValue().config.type.equals("restrequest") || it.getValue().config.type.equals("request"))
	{
		it.getValue().getHttpRequest().setRequestHeaders(header)
	}
}

//construct payload for Login step
def refresh_token = json.refresh_token
//log.info  json.refresh_token
def payload = '{"refreshToken":"' + json.refresh_token + '"}'

testRunner.testCase.getTestStepByName("Login").setPropertyValue('Request',payload)
Occasional Contributor

Re: Need help re-using a response item in other tests

I'm not familiar with groovy code so this doesn't really help me. And this seems excessive to simply take a response item and create something to then use in other tests, but it might just be my ignorance of SoapUI.

Is there not a more simple solution built into SoapUI already?

 

Here are a few items I was looking at initially:

https://www.soapui.org/docs/functional-testing/properties/transferring-properties.html

https://stackoverflow.com/questions/35499787/property-transfer-from-a-testcase-response-to-a-request...

Occasional Contributor

Re: Need help re-using a response item in other tests

Ok, I think I have actually found a solution.

 

To be clear, documentation on property transfer is outdated and not easy for the inexperienced user to make use of it. It seems intentionally complex when it could be a simplier process.

 

I was able to use the property tranfser to get this working, but it took a lot of trial and error, and probably could be done better.

 

For those that might stumble on this question looking for an answer for ReadyAPI:

In the section of my project I right clicked > Add Step > Properties

Green plus sign in the top left and added a name i.e. - UseToken

In the request I right clicked on the token value Transfer To > then in the lower box look for the property and name you just created, click on it

 

Then in my GET call I created a header:

Authorization - Bearer ${#[Project Name#Get Token#Access_Token]#UseToken}

 

Which then worked to complete the request

New Contributor

Re: Need help re-using a response item in other tests

Your solution should work. If you have multiple steps in your testcase it can be cumbersome to add the token for each step. My solution adds the token to each step so you don't have to take care of of anymore.

 

Use the solution that works best for you. Happy testing!

Watch a Video Interview
Top Kudoed Authors
Are You In?

Meet the Community Leaders!

SmartBear Connect is an excellent opportunity for training, in-person support, and networking.

GET YOUR TICKET WITH $100 OFF