问题描述
我是SOAPUI的新手。我有一个场景,比如我必须传递作为对测试套件下的所有请求的响应的访问令牌值。该访问令牌类型是"承载"。我将这个令牌值添加到下一个名为"Authorization"的请求头字段中,它正在工作,但我的查询是不是有任何方法或groovy脚本可以添加,可以应用于所有的SOAP请求,而不是每次都更改所有请求的头的值。如何自动执行此操作?请指导我。
推荐答案
我不明白您到底想要实现什么,我猜您想要为Authoritzation
添加一个http-Header,并将令牌作为testCase
中每个请求的值,为此,您可以将groovy script
TestStep放在获取令牌的TestStep请求下面。在这个groovy脚本中,您可以放入以下代码,该代码为此测试用例中的每个测试步骤设置一个http-Header:
// testSteps is a map where keys are request names and values are the instance
// of the testStep
testRunner.testCase.testSteps.each{ name, testStep ->
log.info name
// check if the testStep has required methods (to avoid error
// trying to add header on groovy script testSteps for example)
if(testStep.metaClass.getMetaMethod("getTestRequest")){
def request = testStep.getTestRequest()
def headers = request.getRequestHeaders()
headers.add('Authoritzation','yourToken')
request.setRequestHeaders(headers)
log.info "Added header to $name"
}
}
此脚本为您的测试用例中的每个测试步骤添加所需的http标头。
编辑
另一种可能的方法是添加一个TestCase属性作为http-Header值,然后在需要刷新此值时设置该属性的值。要在TestStep请求中执行此操作,请单击Headers()
选项卡,并添加名为Authoritzation
、值为${#TestCase#Authoritzation}
的http-Header,如下图所示:
然后,每次您想要设置该属性的值时,您可以使用不同的方法(我没有关于您的案例的足够详细信息,所以我给您提供了不同的可能的解决方案)、使用testRunner.testCase.setPropertyValue('Authoritzation',yourToken)
的属性转移TestStep或groovy脚本TestStep。
希望它能有所帮助