简体   繁体   中英

Trasfer properties from API responses in SOAPUI

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?

Transfer JSON properties

pay load looks like following

{
   "accountName": "Ya-mmy",
   "userEmail": "yashxcgdtuyihxfaap7@gmail.com",
   "send_invitation_email": true,
}

Response looks like following

{
   "primary_user_email": "yashxcgdtuyihxfaap7@gmail.com",
   "access_token": "8cfe0670-897c-46d0-b0f6-c74519624ebf",
   "tenant_name": "Ya-mmy"
}

This can be achieved by multiple ways

  1. adding a groovy script test step after the rest request step where you are currently getting access token.
  2. without using additional step, just use script assertion for the rest request step where you are getting access token. this way, you can even check that there is some value you received in the response.

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

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, eg "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.

Without groovy also we can store this in property and use that property across the test Steps:

Steps Required for this:

  1. Add a Property Transfer step after Rest request

  2. 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

  • Internally your JSON Respose will converted to XML.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM