1
votes

I have a getToken request in a test case get_Admin_Token in PassToken test suite, where I have as a response following JSON:

{
   "access_token": "5701f536-0bd5-441f-a490-21aafeasdasdd",
   "token_type": "bearer",
   "refresh_token": "c53af657-8292-4aff-xxxx-xxxf0ffed310",
   "expires_in": 80208,
   "scope": "read write trust"
}

I need to use access_token value in uploadFile method, but I need to pass it in a header. I have a field Authorization with Bearer: $(access_token) value.

Using some google I found: https://community.smartbear.com/t5/SoapUI-Open-Source/How-do-I-do-a-property-transfer-with-multiple-source-responses/td-p/106456 question, which was looking similar. I started to create a GroovyScript test step, where I used code to pass it to the Properties table, but no success. I was also trying to put it to assertions for the get_Admin_Token, but I got a message about incorrect object types. I also tried to use def accessToken = jsonSlurper.access_token.toString() to use strings, but now I got an error `

No signature of method: 
com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase.setProperty() is 
applicable for argument types: (java.lang.String, java.lang.String) values: 
[AUTH_KEY, Bearer 5701f536-0bd5-441f-a490-21aafeasdasdd] Possible solutions: 
getProperty(java.lang.String), addProperty(java.lang.String), 
hasProperty(java.lang.String), hasProperty(java.lang.String), getProject(), 
getProperties()

My groovy code:

import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(response)
assert !(jsonSlurper.isEmpty())
def accessToken = jsonSlurper.access_token.toString()
assert null != accessToken, "access_token does not have a value"
def authorizationKey = "${accessToken}"
context.testCase.setProperty('AUTH_KEY',"Bearer " + authorizationKey)

Is this code valid? I'm not sure what to put in next method as authorization value in header, I tried with ${#get_Admin_Token#AUTH_KEY}, but it doesn't work

1

1 Answers

1
votes

EDIT: Easier way Just pass Token to properties using transfer action and set in header Bearer ${Properties#AdminToken}. That's all

===================

Old version:

The following answer is correct if anyone is looking for the Groovy script:

Ok, I think I spotted a workaround. Groovy code is as following:

import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(response)
assert !(jsonSlurper.isEmpty())
def accessToken = jsonSlurper.access_token.toString()
assert null != accessToken, "access_token does not have a value"
def authorizationKey = "${accessToken}"
context.testCase.testSuite.setPropertyValue("AUTH_KEY","Bearer " + authorizationKey)
log.info context.testCase.testSuite.getPropertyValue( "AUTH_KEY" )

And using answer presented here: How to transfer dynamic auth value in all requests instead of changing the value in every request's header in SOAPUI I created a new GroovyScript Test Case:

testRunner.testCase.testSteps.each{ name, testStep ->
    log.info name
    if(testStep.metaClass.getMetaMethod("getTestRequest")){
        if(name=="UploadScreenshot"){
        def request = testStep.getTestRequest()
        def headers = request.getRequestHeaders()
        headers.add('Authoritzation',context.testCase.testSuite.getPropertyValue( "AUTH_KEY" ))
        request.setRequestHeaders(headers)
        log.info "Added header to $name"
        }
    }
}

I know it's not a very good idea, to put an if to the loop instead if delete a loop, but I don't know yet how to do it and I need to proceed with work