脚本断言获取令牌并将其设置为 http header 到下一个测试步骤
Script Assertion to get token and set it as http header to the next test step
如何编写脚本(断言)以获得 randomAccessToken
作为给定 code.The 代码输出的 json 格式。
{
"status": "Success",
"code": 1000,
"message": "Random access token generated",
"randomAccessToken": "ef12286f-3263-4c3b-949a-3a43497254e2-20162124112138-1722093936686484240"
}
更新 来自评论:
我需要 header 名称作为 "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
//here you need to use groovy regx or string length grater than as assert i used srting length
希望对您有所帮助!!不要忘记点击回答.. 如果它不起作用提供您的发现我们解决。
现在改进的脚本断言可以,将随机访问密钥存储到 属性 中,可以在下一个 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 )
这是什么意思?新行
context.testCase.testSteps["requestProps"].setPropertyValue( "Tokenkey", actualValue )
为了让上面的行得到执行,您需要添加 testStep 属性并将其重命名为 "requestProps" 并添加一个条目 "Tokenkey"
当脚本断言成功执行时,脚本已提取 randomAccessToken 值并将其存储到 Tokenkey 引用中,以便在成功执行第一个请求后交叉引用打开属性步骤,您会看到从 randomAccessToken 中提取的值
也就是说,您在执行第一个请求后会看到这个,
requestProps
Tokenkey = yettqutt-ajsfugau-uyatwdua
现在在同一测试用例的另一个请求中需要在 header 部分中提取的随机访问令牌?
怎么做?
打开那个 httpRequest -->headers--> 添加一个服务器接受的条目
如果在您的情况下服务器接受随机访问密钥的名称是 "access-key"
然后添加条目
access-key = ${requestProps#Tokenkey}
现在触发您在请求中设置此 header 参数的第二个或第三个第 n 个请求,它将通过。
这里是 script assertion
,注释解释了它在每个语句中的作用:
此脚本将从 json 获取值并自动将其设置为具有 http header 的下一个测试步骤。
来自评论的更新:将header添加到下一个请求
确保 headerName
变量的值正确,默认情况下我已按要求将其设置为 randomAccesstoken
。
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)
}
如何编写脚本(断言)以获得 randomAccessToken
作为给定 code.The 代码输出的 json 格式。
{
"status": "Success",
"code": 1000,
"message": "Random access token generated",
"randomAccessToken": "ef12286f-3263-4c3b-949a-3a43497254e2-20162124112138-1722093936686484240"
}
更新 来自评论:
我需要 header 名称作为 "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
//here you need to use groovy regx or string length grater than as assert i used srting length
希望对您有所帮助!!不要忘记点击回答.. 如果它不起作用提供您的发现我们解决。
现在改进的脚本断言可以,将随机访问密钥存储到 属性 中,可以在下一个 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 )
这是什么意思?新行
context.testCase.testSteps["requestProps"].setPropertyValue( "Tokenkey", actualValue )
为了让上面的行得到执行,您需要添加 testStep 属性并将其重命名为 "requestProps" 并添加一个条目 "Tokenkey"
当脚本断言成功执行时,脚本已提取 randomAccessToken 值并将其存储到 Tokenkey 引用中,以便在成功执行第一个请求后交叉引用打开属性步骤,您会看到从 randomAccessToken 中提取的值 也就是说,您在执行第一个请求后会看到这个,
requestProps
Tokenkey = yettqutt-ajsfugau-uyatwdua
现在在同一测试用例的另一个请求中需要在 header 部分中提取的随机访问令牌? 怎么做?
打开那个 httpRequest -->headers--> 添加一个服务器接受的条目
如果在您的情况下服务器接受随机访问密钥的名称是 "access-key"
然后添加条目
access-key = ${requestProps#Tokenkey}
现在触发您在请求中设置此 header 参数的第二个或第三个第 n 个请求,它将通过。
这里是 script assertion
,注释解释了它在每个语句中的作用:
此脚本将从 json 获取值并自动将其设置为具有 http header 的下一个测试步骤。
来自评论的更新:将header添加到下一个请求
确保 headerName
变量的值正确,默认情况下我已按要求将其设置为 randomAccesstoken
。
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)
}