如何从JSON文件中获取特定用户?

时间:2019-05-23 09:12:58

标签: json python-2.7 robotframework

我有一个JSON文件,我在其中获取用户数据并执行操作。如何从JSON文件中获取特定用户?

这是JSON文件:

    "users" : {
            "ABC": {
                "ip": "10.50.10.XXXX:5555",
                "username": "user1@gmail.com",
                "password": "xyz123",
                "displayname": "user 1",
                "Mode": "Personal",
                "phonenumber": "14084xxxx",
                "pstndisplay": "+1 408-xxx-xxx"
            }
            "EFG": {
                "ip": "10.50.10.XXXX:5555",
                "username": "user2@gmail.com",
                "password": "xyz123",
                "displayname": "user 2",
                "Mode": "Personal",
                "phonenumber": "14084xxxx",
                "pstndisplay": "+1 408-xxx-xxx"
            }

             "XYZ": {
                "ip": "10.50.10.XXXX:5555",
                "username": "user3@gmail.com",
                "password": "xyz123",
                "displayname": "user 3",
                "Mode": "Personal",
                "phonenumber": "14084xxxx",
                "pstndisplay": "+1 408-xxx-xxx"
            }

这里是我如何解析JSON数据:

  ${the file as string}=    Get File    ${users_json_path}
      ${parsed}=    Evaluate  json.loads("""${the file as string}""", object_pairs_hook=collections.OrderedDict)     json, collections
      ${properties}=  Set Variable  ${parsed["users"]}
      Log  ${properties}
      :FOR    ${key}    IN    @{properties}
      \  ${sub dict}=    Get From Dictionary    ${properties}    ${key}
      \  Log  ${sub dict}
      \  Signin  ${sub dict}[ip]   ${sub dict}[username]   ${sub dict}[password]  ${sub dict}[Mode]
      \  Log  ${key} is successfully signed in.

这是测试用例:

Given Number of users signs in to their respective devices  ${ABC} 
      Then ABC sees that Sign in is successful
      And No action is taken for 30 seconds
      When ABC signs out from the device
      Then ABC sees that sign out was successful

我期望当我想使用诸如ABC之类的特定用户执行登录时,它将仅通过ABC执行登录操作,但是它正在通过从用户获取数据来使用所有用户执行登录操作。通过从JSON中获取特定用户来登录操作?

1 个答案:

答案 0 :(得分:0)

添加到循环开始:

Continue For Loop If    $key != 'ABC'

如果用户不是ABC,则会跳过“登录”。