Reputation: 11
First of all, I read official documents and read other threads on here but I'm having trouble figuring this issue out. So please help me out :)
Now, I have ONE soapUI project and under that I have two different test suites. I create an account using one test suite (generate account API) and in JSON response I get an access token back.
there are bunch of APIs in the second test suite and all of this APIs use access token from first suite.
So what I essentially want to do is, save token from first suite and use it as a variable (or any other way) in the second suite for all subsequent API calls. Is it possible?
pay load looks like following
{
"accountName": "Ya-mmy",
"userEmail": "[email protected]",
"send_invitation_email": true,
}
Response looks like following
{
"primary_user_email": "[email protected]",
"access_token": "8cfe0670-897c-46d0-b0f6-c74519624ebf",
"tenant_name": "Ya-mmy"
}
Upvotes: 0
Views: 4025
Reputation: 123
I just solved similar problem using build in property transfer feature.
In the source, put "Response" as a Property and "JSONPath" as a language.
In the blank field add: $access_token
in your case.
Then just point to right target. It can be TestSuite or Global custom properties. You have to specify the name of custom property first, e.g. "access_token". It will be saved there.
Now, to use it anywhere in your tests, simply use: ${#TestSuite#access_token}
or accordingly if you saved it as a Global property.
Upvotes: 1
Reputation: 185
Without groovy also we can store this in property and use that property across the test Steps:
Steps Required for this:
Add a Property Transfer step after Rest request
Add a properties step and a property "SecurityToken" with empty value
In Property Transfer add new Transfer "TransferSecurityToken"
source: Select REST(Request) and In Property Select ResponseAsXml
Use xpath like //*:access_token in source and
in target Select Properties -- > SecurityToken
Now property SecurityToken have the Response value of 1 API response "access_token"
use ${Properties#SecurityToken}in your request
Additionally you can directly use following statement in other Test Request ${FirstTeststepName#ResponseAsXml#//*:acess_token}
Hope these two solution Helps
Upvotes: 1
Reputation: 21369
This can be achieved by multiple ways
Its again personal choice, and I prefer and providing here #2 way.
Add the below script in the script assertion for your rest request step:
import net.sf.json.groovy.JsonSlurper
def jsonResponse = new JsonSlurper().parseText(messageExchange.responseContent)
assert null != jsonResponse.access_token, "access_token of response either does not have value or null"
context.testCase.testSuite.project.setPropertyValue('ACCESS_TOKEN', jsonResponse.access_token)
Now, you can use ${#Project#ACCESS_TOKEN}
where ever you need the access token in your testSuite
Upvotes: 1