Script Assertion to get token and set it as http header to the next test step

痴心易碎 提交于 2019-12-20 04:50:17

问题


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.


回答1:


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.




回答2:


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)
}


来源:https://stackoverflow.com/questions/37412281/script-assertion-to-get-token-and-set-it-as-http-header-to-the-next-test-step

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!