【问题标题】:How can we set the value to the header dynamically in SOAPUi?我们如何在 SOAPUi 中动态地将值设置为标头?
【发布时间】:2015-07-17 12:44:31
【问题描述】:

我是 SoapUI 的新手。我想知道如何将 2 个属性值添加到一个 Header 值中。

例如,我收到了一些类似 XML 格式的响应:

<Response xmlns="Http://SomeUrl">
    <access_token>abc</access_token>
    <scope>scope1</scope>
    <token_type>Bearer</token_type>
</Response>

我想将 access_token 和令牌类型都发送到单个标头值,例如:

"Authorization":"Bearer abc"

我不知道如何使用属性转移步骤来做到这一点。

谁能帮帮我?

【问题讨论】:

    标签: xpath soapui


    【解决方案1】:

    您可以在属性转移步骤中使用 XPath concat 函数将两个值连接到一个变量中,在您的情况下,您可以使用以下 XPath:

    concat(//*:token_type," ",//*:access_token)
    

    concat 函数连接两个或多个字符串,//*:token_type 获取 Bearer 值,//*:access_token 获取 abc

    希望对你有帮助,

    【讨论】:

      【解决方案2】:

      在返回上述内容的步骤之后添加一个脚本步骤。

      def tokenType = context.expand('${STEP RETURNING STUFF#Response#//Response/token_type}');
      def token = context.expand('${STEP RETURNING STUFF#Response#//Response/access_token}');
      
      //add header to all steps
      for (def stepEntry : testRunner.testCase.testSteps) {
          if (!(stepEntry.value instanceof com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep)) {
              continue;
          }
          def headers = stepEntry.value.httpRequest.requestHeaders;
      
          headers.remove("Authorization");
          headers.put("Authorization", token_type + " " + token); 
          stepEntry.value.httpRequest.requestHeaders = headers;
      }
      

      【讨论】:

        【解决方案3】:

        这是另一种方式,不使用额外的属性转移步骤,而是使用脚本断言

        1. 为请求测试步骤添加脚本断言。
        2. 在该脚本中使用以下代码,需要修改元素 XPath
        def element1Xpath = '//*:token_type'
        def element2Xpath = '//*:access_token'
        def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
        def response = groovyUtils.getXmlHolder(messageExchange.responseContentAsXml)
        def field1 = response.getNodeValue(element1Xpath)
        def field2 = response.getNodeValue(element2Xpath)
        if (!field1) { throw new Error ("${element1Xpath} is either empty or null")  }
        if (!field1) { throw new Error ("${element2Xpath} is either empty or null")  }
        context.testCase.setPropertyValue('TEMP_PROPERTY', "${field1} ${field2}")
        
        1. 现在预期值(合并)在属性“TEMP_PROPERTY”中可用。您可以在代码的最后一行根据需要重命名属性名称。
        2. 您可以在测试用例中任何需要的地方使用新的。

        【讨论】:

          猜你喜欢
          • 2011-03-25
          • 2021-01-31
          • 1970-01-01
          • 1970-01-01
          • 2012-08-20
          • 2019-09-16
          • 2022-11-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多