脚本断言获取令牌并将其设置为http标头,以进行下一个测试步骤 [英] Script Assertion to get token and set it as http header to the next test step

查看:85
本文介绍了脚本断言获取令牌并将其设置为http标头,以进行下一个测试步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个脚本(断言)以获取 randomAccessToken 作为给定代码的输出。代码采用json格式。

  {
status:Success,
code:1000,
消息:随机存取令牌生成,
randomAccessToken:ef12286f-3263-4c3b-949a-3a43497254e2-20162124112138-1722093936686484240
}




更新来自评论:

名称为randomAccesstoken,但对于下一个TestStep,因为需要运行该信息。

解决方案

有grrovy脚本断言和粘贴下面的代码。

  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

//这里你需要使用groovy regx或者字符串长度比作为断言我用srting长度

希望这有助于!不要忘记点击答案..如果它没有工作提供您的发现,我们解决。

现在精炼的脚本断言将做,将randomacesskey存储到一个属性,可以在下一个HTTP REQUEST头文件中使用

  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)

这是什么意思?新行

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

为了获得执行的上述行,您需要添加testStep属性并将其重命名为requestProps并添加一个输入Tokenkey

在脚本声明执行成功之前,脚本已经提取并将randomAccessToken值存储到Tokenkey refernce中,用于交叉引用打开属性步骤执行后第一次请求成功,你会看到一个从randomAccessToken提取的值
,即你在执行完firt请求后看到这个值,

  requestProps 
Tokenkey = yettqutt-ajsfugau-uyatwdua

现在在另一个请求需要在报头部分提取Randomaccess标记的相同测试用例?
如何做到这一点?



打开httpRequest - > headers - >添加服务器接受的条目

如果您的服务器接受了randomaccess密钥的名称是access-key,那么

然后是一个添加条目

  access-key = $ {requestProps#Tokenkey} 

现在触发第二个或第三个请求,您在请求中设置了此标头参数。


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.

解决方案

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.

这篇关于脚本断言获取令牌并将其设置为http标头,以进行下一个测试步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆