SoapUI:从数组中提取响应值并传递给下一个请求

SoapUI: Extract Response Value from an Array and Pass Along to Next Request

希望大家能再次帮助我!

我想出了如何在一个简单的场景中从 REST 响应中提取一个值并将该值传递给前面的测试步骤,但是,现在我对如何从一个数组中提取一个值感到困惑在前面的测试步骤中使用的 REST 响应(如下所示)。

更具体地说,我正在尝试提取 answerChoices 中的 "id" 值之一。

响应负载:

(GET) localhost/ws/portal/survey/question/${Create UX Survey Question#ResponseAsXml#//*:id}   

{
       "id": 589,
       "isEnabled": true,
       "type": "RADIO",
       "wording": "SoapUI Test UX Survey Question",
       "answerChoices":    [
                {
             "id": 1546,
             "order": 4,
             "text": "Choice4"
          },
                {
             "id": 1543,
             "order": 2,
             "text": "Choice2"
          },
                {
             "id": 1544,
             "order": 3,
             "text": "Choice3"
          },
                {
             "id": 1545,
             "order": 1,
             "text": "Choice1"
          }
       ]
    }

然后我尝试将 answerChoices:id 之一传递给下一个请求中的 questionChoiceId 值(如下所示)。

(POST) localhost/ws/portal/survey/alert

{
  "questionChoiceId" : ${},
  "restaurantId" : ${Create New Restaurant#ResponseAsXml#//*:id},
  "alertProfileId" : ${#Project#emailAlertProfileId}
}

我就是想不通,我试过在 属性 传输中声明命名空间,这让我很困惑。如果您能指出我的任何方向,我们将不胜感激!

我想通了!所以,我想确保我发布了答案以防其他人 运行 遇到同样的问题!

我刚刚编写了一个 groovy 脚本并执行了以下操作:

  1. 我创建了两个项目自定义属性(negativeSurveyAnswerpositiveSurveyAnswer
  2. 添加一个Groovy测试步骤并添加以下代码:

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def questionResponse = context.expand('${Get UX Survey Question Details by questionId#ResponseAsXml}')
    def idVals = questionResponse.split("<id>", -1)
    def Choice1Id = idVals[1].split("</id>", -1)[0]
    def Choice4Id = idVals[2].split("</id>", -1)[0]
    def prop2 = testRunner.testCase.testSuite.project.getProperty("positiveSurveyAnswer")
    prop2.setValue(Choice1Id)
    def prop3 = testRunner.testCase.testSuite.project.getProperty("negativeSurveyAnswer")
    prop3.setValue(Choice4Id)
    
  3. 获取值并运行将其传递给项目的属性后,接下来的测试步骤是:

     {
      "questionChoiceId" : ${#Project#negativeSurveyAnswer},
      "restaurantId" : ${Create New Restaurant#ResponseAsXml#//*:id},
      "alertProfileId" : ${#Project#emailAlertProfileId}
     }
    

希望这对其他人有帮助!