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

问题描述

嗨,我正在尝试编写使用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,*/*"
        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,*/*"
        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

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

谢谢!

解决方法

变量的范围设置不正确。该部分

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

需要设置。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...