0

After I get a token from a Post request as shown below:

{ "access_token": "12345", "expires_in": 3600, "token_type": "Bearer" } 

I want to use this token in different TestSteps Headers values.

For example I have to make a GET request after I received this token and it have in the header -> Authentification : Bearer + token_value.

So can I write a GroovyScript or something to make this automatically? I'm using ReadyApi.

Regards, Adrian

Rao
  • 20,781
  • 11
  • 57
  • 77
Adrian
  • 3
  • 1
  • 4

2 Answers2

1

Add Script Assertion for the same step where you receive the mentioned response:

Script Assertion this fetches the values from response and creates a project property and set the retrieved value.

//Check if the response is empty or null
assert context.response, "Response is null or empty"
def json = new groovy.json.JsonSlurper().parseText(context.response)
def token =  "${json.token_type} ${json.access_token}" as String
log.info "Token will be: ${token}"
//Assing the value at project level property TOKEN
context.testCase.testSuite.project.setPropertyValue('TOKEN', token)

Now the value needs to be set as header to each outgoing request dynamically. i.e., Add Authorization header and its value for the SOAP or REST request type steps. For this, Events feature is going to be used. Add a SubmitListener.beforeSubmit event and add the below script into it. Please follow the comments inline.

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep

//Please edit the header name as you wanted
def headerName = 'Authorization'


//a method which sets the headers
def setHttpHeaders(def headers) {
    def step = context.getProperty("wsdlRequest").testStep
    if (step instanceof RestTestRequestStep || step instanceof WsdlTestRequestStep) {
    def currentRequest = step.httpRequest
    def existingHeaders = currentRequest.requestHeaders
    headers.each {
           existingHeaders[it.key] = it.value
        }
        currentRequest.requestHeaders = existingHeaders
    } else {
      log.info 'not adding headers to the current step as it is not request type step'
    }
}

//read the token from project properties
def token = context.expand('${#Project#TOKEN}')
//assert the value of token
assert token, "Token is null or empty"

//UPDATE from the comment to add the header to next request
if (token) {
  def headerValue = [(token)]
  def headers = [(headerName) : (headerValue)]
  setHttpHeaders(headers)
}
Rao
  • 20,781
  • 11
  • 57
  • 77
  • So I create a Script Assertion from Get.Authentification TestStep and it works. I create SubmitListener.beforeSubmit event I put this code there. I don't have to change header name because it's the same in my steps (GetABC,PutABC etc.) but it's not work. I have to make another modifications? For example I have to introduce in : setHttpHeaders(nStepName, headers) the names of steps where I want to add this header? I don't have to change the "wsdlRequest" ? – Adrian Mar 02 '17 at 07:28
  • I have fixed the `setHttpHeaders(nStepName, headers)` to `setHttpHeaders(headers)` . step name is not needed because you want it for every step. Please take the updated script from above answer. By the way I could not try before giving it to you. – Rao Mar 02 '17 at 08:05
  • And It is a way to use the token for individual steps? Why because in the same testSuite I have requests with anothers authorizations acces.... – Adrian Mar 02 '17 at 09:48
  • To be more clear: In the same TestSuite I have GetAuthorization token and this token I used for example in 3 TestSteps (Headers) ; In the same TestSuite I have another requests whith anothers authorization from another api. – Adrian Mar 02 '17 at 09:51
  • I am sorry, did not get you. Have you tried with updated answer? Has it worked for the intended steps at least? – Rao Mar 02 '17 at 10:35
  • I tried but it's not working... So : I have a project -> TestSuite (With a lot of TestSteps). The first step In the TestSuite is: GetAuthentification (that token) this token I will use in next 3 TestSteps (In their headers) ; after this teststeps I have anothers two tesStetps that has a unique token from another api (I don't want to change it like the others). You get me? – Adrian Mar 02 '17 at 11:01
  • First try for only the one test (for now leave it about other test cases), can show what is being sent in the raw request? Can you notice if the same token is sent? – Rao Mar 02 '17 at 11:08
  • I tried but I don't receive a response. I wait for getting the response but nothing happened . – Adrian Mar 02 '17 at 12:33
0
 import groovy.json.JsonSlurper
import groovy.json.*

def tokens=testRunner.runStepByname("Token")
def response = context.expand( '${Token#Response}' )
def JsonSlurperjsonSlurper = newJsonSlurper()
def Objectresult = jsonSlurper.parseText(response)
def access_token= result.access_token

def authorization = "Bearer "+access_token
testRunner.testCase.setPropertyValue("access_token", authorization)
SQA
  • 39
  • 11