存储来自 jsr223 响应的变量以在后续测试中使用

Storing a variable from jsr223 response to use in subsequent tests

您好,我正在尝试编写一个使用 Microsoft Graph Api 的金牛座测试。我的目标是在 POST 请求中使用来自两个 jsr223 响应的组 ID 和用户 ID 将用户添加到 Azure 组。问题是我不断得到

 Non HTTP response message: Illegal character in path at index 41: https://graph.microsoft.com/v1.0/groups/${AzureGroupId}/members/$ref

因为变量 AzureGroupId 没有正确设置。这是获取组的测试:

      # Microsoft Graph Get AD groups
      - url: ${MicrosoftGraph}/groups?$orderby=displayName
        label: 1302_FetchMicrosoftAADGroups
        method: GET
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        assert-httpcode:
          - 200
          - 202
        jsr223:
          - langauge: groovy
            execute: after
            script-text: |
              import groovy.json.JsonSlurper
              def slurperresponse = new JsonSlurper().parseText(prev.getResponseDataAsString())
              for (entry in slurperresponse){
                if(entry.displayName == "ACME"){
                   vars.put("AzureGroupId",  entry.id)
                   break;
                }
              }
            script-file: jsr223/logger.groovy
            parameters: fetch_microsoft_ad_groups

下面是获取用户的测试:

      # Microsoft Graph Get AD users
      - url: ${MicrosoftGraph}/users
        label: 1302_FetchMicrosoftAADUsers
        method: GET
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        assert-httpcode:
          - 200
          - 202
        jsr223:
          - langauge: groovy
            execute: after
            script-text: |
              import groovy.json.JsonSlurper
              def slurperresponse = new JsonSlurper().parseText(prev.getResponseDataAsString())
              for (entry in slurperresponse){
                if(entry.userPrincipalName == "jon.doe@gmail.com"){
                   vars.put("AzureUserId",  entry.id)
                   break;
                }
              }
            script-file: jsr223/logger.groovy
            parameters: fetch_microsoft_ad_users

最后,我将这两个测试的结果结合起来,在这个测试中提出 POST 请求:

      # Microsoft Graph Add a user
      - url: ${MicrosoftGraph}/groups/${AzureGroupId}/members/$ref
        label: 1324_AddUserToAzureADGroup
        method: POST
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        body:
          "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/${AzureUserId}"
        assert-httpcode:
          - 204
        jsr223:
          - langauge: groovy
            execute: after
            script-file: jsr223/logger.groovy
            parameters: add_user_to_active_directory_group

POST 请求失败,因为 url 格式错误。有人可以解释如何从响应中设置变量,以便我可以在后续请求中将其作为 url 的一部分使用吗?

谢谢!

变量的范围设置不正确。本节

scenarios:
  TestScenario:
    variables:
      AzureGroupId: 0
      AzureUserId: 0
      AzureUserName: ""

需要设置。