简体   繁体   English

如何将参数直接从json文件传递到机器人框架中的关键字?

[英]How to pass arguments directly from json file to keyword in robot framework?

I have a Json file like this.PFB the code: 我有一个像这样的Json文件.PFB代码:

"properties " : {
    "xyz" : {
        "username" : "abc@gmail.com",
        "password" : "abc@123",
        "phonenumber" : "1235",
    },
      "ABC" : {
        "username" : "abc@gmail.com",
        "password" : "abc@123",
        "phonenumber" : "1345",
    },

Keyword will be like : 关键字如下:

Do operation for properties
  [Arguments]  ${username}  ${password}  ${phonenumber}
  Log  ${username}
  Log  ${password} 
  Log  ${phonenumber}

My questions are : 我的问题是:

1) The json file contains so many things but i have to fetch only properties from the file.How i will take properties part from the entire json file and pass directly arguments like username , password , phonenumber into keyword mentioned above. 1)json文件包含很多东西,但我只需要从文件中获取属性。我将如何从整个json文件中获取属性,并将参数如username,password,phonenumber直接传递给上述关键字。

2) How to write keyword for this logic such that we change only json file for adding more properties like apart from xyz, abc we will add as many properties we want and it will automatically fetch and keyword give us the desired result for all properties whatever we are modifying in json file. 2)如何为此逻辑编写关键字,以便我们仅更改json文件以添加除xyz,abc之外的更多属性,我们将添加任意数量的属性,它将自动获取,并且关键字为所有属性提供所需的结果,无论如何我们正在json文件中进行修改。

If I understand you correctly, your question's summary is: a) how to read and parse a json file in Robotframework in this form, and b) pass each record's attributes to this keyword. 如果我正确理解您的问题,那么您的问题摘要是:a)如何以这种形式在Robotframework中读取和解析json文件,以及b)将每条记录的属性传递给此关键字。

A file can be read from the file system with Get File . 可以使用Get File从文件系统Get File

One can read a json file with the python's json module, more specifically the loads() method - it takes a string, and returns a python object. 可以使用python的json模块读取一个json文件,更具体地说是loads()方法-它需要一个字符串,并返回一个python对象。

Your "json" sample is quite invalid json, so let's imagine that "properties" is somewhere (3 levels deep) inside the file. 您的“ json”样本是完全无效的json,因此让我们假设“属性”位于文件内部某处(深度3级)。

${the file as string}=    Get File    c:\\the\\path\\to\\the\\file.json
${parsed}=    Evaluate    json.loads("""${the file as string}""")    json
${properties}=    Set Variable    ${parsed["top"]["child"]["properties"]}

And now the variable properties is a dictionary, with those two keys - "ABC" and "xyz"; 现在变量properties是一个字典,具有这两个键-“ ABC”和“ xyz”; you just iterate over it, and pass the subkeys of each of the sub-dictionaries to the keyword. 您只需对其进行迭代,然后将每个子词典的子关键字传递给关键字。

FOR    ${key}    IN    @{properties}
  ${sub dict}=    Get From Dictionary    ${properties}    ${key}
  Do operation for properties    ${sub dict}[username]    ${sub dict}[password]    ${sub dict}[phonenumber]
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM