Script Assertion to get token and set it as http h

2019-02-28 15:51发布

How to write a script(assertion) in order to get the randomAccessToken as output from the given code.The code is in json format.

{
  "status": "Success",
  "code": 1000,
  "message": "Random access token generated",
  "randomAccessToken": "ef12286f-3263-4c3b-949a-3a43497254e2-20162124112138-1722093936686484240"
}

UPDATE from comment:

I need the header name as "randomAccesstoken" but for the next TestStep because to run that I need this info.

2条回答
smile是对你的礼貌
2楼-- · 2019-02-28 16:40

Have grrovy script assertion and paste the below code.

import groovy.json.JsonSlurper 
def jsponresponse = messageExchange.responseContent
def jsonSlurper = new JsonSlurper()
jsonParsed = jsonSlurper.parseText(jsponresponse)
actualValue = jsonParsed.randomAccessToken
log.info actualValue

assert 12 < actualValue.length

//here you need to use groovy regx or string length grater than as assert i used srting length

Hope this helps!! dont forget to click answered.. if it didn't work provide your finding we solve.

Now the refined script assertion would do ,store the randomacesskey in to a property ,that can be used in next HTTP REQUEST headers

import groovy.json.JsonSlurper 
def jsponresponse = messageExchange.responseContent
def jsonSlurper = new JsonSlurper()
jsonParsed = jsonSlurper.parseText(jsponresponse)
actualValue = jsonParsed.randomAccessToken
log.info actualValue    
assert 12 < actualValue.length
context.testCase.testSteps["requestProps"].setPropertyValue( "Tokenkey", actualValue )

what does it mean? the new line

context.testCase.testSteps["requestProps"].setPropertyValue( "Tokenkey", actualValue )

In order the above line to get execute , you need to add testStep properties and rename it as "requestProps" and add a entry "Tokenkey"

by the time the script assertion excuted successfully , script have extracted and stored randomAccessToken value in to Tokenkey refernce , for cross reference open properties step after the execution of first request successfully and you see a value that got extracted from randomAccessToken i.e., you see this after executing the firt request,

requestProps
Tokenkey = yettqutt-ajsfugau-uyatwdua

Now in the another any request of the same test case which requires this extracted Randomaccess token in header section ? How to do that ?

open that httpRequest -->headers--> add an entry that server accepts

if in your case server accepts name of the randomaccess key is "access-key"

then an add entry

access-key = ${requestProps#Tokenkey}

now fire the second or 3rd nth request you have set this header parameter in the request it will go through.

查看更多
The star\"
3楼-- · 2019-02-28 16:40

Here you go with the script assertion, comments in line explaining what it is doing in the each statement:

This script will fetch the value from json and set it has http header to the next test step automatically.

UPDATE from the comment: to add the header to next request

Make sure you have the right value for headerName variable, by default I have set it to randomAccesstoken as requested.

import net.sf.json.groovy.JsonSlurper
//Please edit the header name you wanted
def headerName = 'randomAccesstoken'
// get the next test step name automatically, set the name if you want it for different step which is not the immediate next step
def nStepName = context.testCase.testStepList[context.currentStepIndex + 1].name


//a method which sets the headers
def setHttpHeaders(String nextStepName, def headers) {
    def nextRequest = context.testCase.testSteps[nextStepName].httpRequest
    def existingHeaders = nextRequest.requestHeaders
    headers.each {
        existingHeaders[it.key] = it.value
    }
    nextRequest.requestHeaders = existingHeaders
}


//create parser and pass the response that you received
def json = new JsonSlurper().parseText(messageExchange.responseContent)
//read the token
def token = json.randomAccessToken
//assert the value of token if null or empty
assert token, "Response does not contain Token or null"
//UPDATE from the comment to add the header to next request
if (token) {
  log.info "next test step name is : ${nStepName}" 
  def headerValue = [(token)]
  def headers = [(headerName) : (headerValue)]
  setHttpHeaders(nStepName, headers)
}
查看更多
登录 后发表回答