【问题标题】:How to take specific users from JSON file?如何从 JSON 文件中获取特定用户?
【发布时间】:2019-10-09 19:53:02
【问题描述】:

我有一个 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 执行登录操作,但它通过从 users.how 获取数据来使用所有用户执行登录操作通过从 JSON 获取特定用户进行登录操作?

【问题讨论】:

  • 你能分享一下你到目前为止尝试过的事情以及阻碍你的事情吗?
  • 我正在尝试使用特定用户的 JSON 数据执行登录操作,但它正在获取所有用户并执行登录,但我想使用特定用户执行登录。如何使它只需要上述测试用例中的特定用户?

标签: json python-2.7 robotframework


【解决方案1】:

添加到循环开始:

Continue For Loop If    $key != 'ABC'

如果用户不是 ABC,这将跳过“登录”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多