如何从SOPAUI响应中获取特定属性值?

时间:2016-12-20 13:22:34

标签: xml api groovy soapui

enter image description here每当我执行测试用例时,我都会将响应中的属性值作为myscore Arun / ten2016-12-20

此处的关键是myscore,值为Arun/ten2016-12-20。日期将动态变化,因为今天日期附加文本'Arun / ten'

我尝试添加以下groovy脚本代码,但它不起作用,

today = new Date().format("yyyy-MM-dd")
log.info today

def s=log.info("Arun/ten" + today);

assert messageExchange.responseHeaders["my_client_update"] == ["s"]

我希望传递这个脚本。

我已经通过了下面的脚本断言,

断言messageExchange.responseHeaders [“my_client_update”] == [“s”]

请提出解决方案。

1 个答案:

答案 0 :(得分:1)

您的Script Assertion几乎接近,只有一条陈述有轻微的问题。

以下是您需要脚本的内容,并注意下面的脚本使用简单的正则表达式作为日期,因为它是动态的。当然,您也可以使用方便的方式使用。

/**
* Below is the Script Assertion
* Retrieves the specified header and asserts against the expected value
**/

//Change the header key as needed.
def requiredHeaderKey = 'my_client_update'

//Change the expected value for the above Header key
//Note that below one is using the regular expression to accommodate dynamic date in the header value
def expectedRequiredHeaderValue = "Arun/ten\\d{4}-\\d{2}-\\d{2}"

//You may not be required to change beyond this point of the script.

//Check if the response has headers
assert messageExchange.responseHeaders, "There are no headers in the response"

//Get & Check if the response has required Header and its value is not empty
def requiredHeaderValue = messageExchange.responseHeaders[requiredHeaderKey]
assert requiredHeaderValue, "Value of the response header ${requiredHeaderKey} is empty or null"

if (requiredHeaderValue instanceof List) {
  log.info "Requested header value is ${requiredHeaderValue[0]}"
  assert requiredHeaderValue[0] ==~ expectedRequiredHeaderValue, "Response header value is not matching with the expected value"
}