我从soapui测试请求REST,我收到一个包含一个令牌(userToken)的响应Json:
{
"status": 200,
"entity": {
"status": "SUCCESS",
"message": {
"defaultMsg": "Successfully logged in.",
"msgId": "controller.authorization.success.log.in"
},
"data": {
"userToken": "b57796e3-d9e4-49f2-9d46-481a2048ab65",
"userName": "operateur",
"userId": "operateur",
}
}
我在soapui中添加了这个断言,用于提取userToken的值并将其作为项目的变量:
import com.eviware.soapui.support.XmlHolder
import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
//def ResponseMessage = testRunner.testCase.testSteps["Recuperation Jeton"].testRequest.response.contentAsString
def ResponseMessage = messageExchange.response.contentAsString
log.info("OAUTH TOCKEN :"+ResponseMessage)
def object = new JsonSlurper().parseText(ResponseMessage)
log.info ("userToken :"+object.data.userToken)
messageExchange.modelItem.testStep.testCase.testSuite.project.setPropertyValue( "X-AUTH-TOKEN",object.data.userToken)
但它不起作用...你能告诉我你的建议吗?
先谢谢,
最诚挚的问候,
答案 0 :(得分:2)
您的代码存在一些问题。首先,您错过了JsonSlurper
- groovy.json.JsonSlurper
的正确导入。其次,您通往userToken
财产的路径不正确 - 您在开始时错过了entity
。您的脚本应该看起来或多或少:
import groovy.json.JsonSlurper
def json = messageExchange.response.contentAsString
def root = new JsonSlurper().parseText(json)
log.info ("userToken: " + root.entity.data.userToken)
messageExchange.modelItem.testStep.testCase.testSuite.project.setPropertyValue("X-AUTH-TOKEN", root.entity.data.userToken)
答案 1 :(得分:2)
似乎Script Assertion
用于 REST请求测试步骤。
为了获得回复,您可以简单地使用context.response
。
log.info context.response
要在项目级别设置属性,请使用context.testCase.testSuite.project.setPropertyValue('NAME', 'VALUE')
Script Assertion
的所有脚本都是:
//check if there is response
assert context.response, 'Response is empty or null'
def json = new groovy.json.JsonSlurper().parseText(context.response)
def token = json.data.userToken
//check if there is token
assert token, 'token is empty or null'
log.info token
//Set value at project level property
context.testCase.testSuite.project.setPropertyValue('X-AUTH-TOKEN', token)