如何将 json 数据文件加载到机器人框架中的变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24734629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:25:11  来源:igfitidea点击:

How to load a json data file into a variable in robot framework?

jsonrobotframework

提问by Vikraant Singh Sanwal

I Am trying to load a json data file into a variable directly in Robot Framework. Can anyone please elaborate with an e.g. giving the exact syntax as to how to do it? Thanks in advance :)

我正在尝试将 json 数据文件直接加载到 Robot Framework 中的变量中。任何人都可以详细说明例如给出如何做到这一点的确切语法吗?提前致谢 :)

回答by Bryan Oakley

One way would be to use the Get Filekeyword from the OperatingSystemlibrary, and then use the built-in Evaluatekeyword to convert it to a python object.

一种方法是使用OperatingSystem库中的Get File关键字,然后使用内置的Evaluate关键字将其转换为 python 对象。

For example, consider a file named example.json with the following contents:

例如,考虑一个名为 example.json 的文件,其内容如下:

{
    "firstname": "Inigo",
    "lastname": "Montoya"
}

You can log the name with something like this:

您可以使用以下内容记录名称:

*** Settings ***
| Library | OperatingSystem

*** Test Cases ***
| Example of how to load JSON
| | # read the raw data
| | ${json}= | Get file | example.json
| | 
| | # convert the data to a python object
| | ${object}= | Evaluate | json.loads('''${json}''') | json
| | 
| | # log the data
| | log | Hello, my name is ${object["firstname"]} ${object["lastname"]} | WARN

Of course, you could also write your own library in python to create a keyword that does the same thing.

当然,你也可以用python编写自己的库来创建一个做同样事情的关键字。

回答by Vinaykumar Patel

There is a library available for this: HttpLibrary.HTTP

有一个可用的库:HttpLibrary.HTTP

${json}= | Get file | example.json
${port}  | HttpLibrary.HTTP.Get Json Value | ${json} | /port
log      | ${port}

API Document is available here: http://peritus.github.io/robotframework-httplibrary/HttpLibrary.html

API 文档可在此处获得:http: //peritus.github.io/robotframework-httplibrary/HttpLibrary.html

回答by José Andias

A common use is passingthe json data to another library like Http Library Requests. You could do:

一个常见的用途是json 数据传递给另一个库,如Http Library Requests。你可以这样做:

*** Settings ***
Library        OperatingSystem
Library        RequestsLibrary

*** Test Cases ****
Create User
         #...
   ${file_data}= 
    ...  Get Binary File    ${RESOURCES}${/}normal_user.json
         Post Request       example_session    /user    data=${file_data} 
         #...

No direct python involved and no intermediary json object.

不涉及直接的 python,也没有中间的 json 对象。

回答by MD5

Thanks Vinay .. that helped now we can retrieve data from json file in robot framework as well

*** Settings ***
Library           HttpLibrary.HTTP
Library           OperatingSystem

*** Test Cases ***
Login_to_SalesForce_Json
    ${jsonfile}    Get File    c:/pathtojason/Data/testsuite.json
    ${username}    Get Json Value    ${jsonfile}    /test_case1/username
    log    ${username}

Below is the json file structure

{
    "test_case1": 
        {
            "username":"User1",
            "password":"Pass1"
        }
    ,
    "test_case2":
        {
            "username1":"User2",
            "password1":"Pass2"
        }


}

Prerequiste is:pip install --trusted-host pypi.python.org robotframework-httplibrary

先决条件是:pip install --trusted-host pypi.python.org robotsframework-httplibrary

回答by idomoni

I had similar issue and this work fine with me:
${json} Get Binary File${json_path}nameOfJsonFile.json

我有类似的问题,这对我来说很好用:
${json} Get Binary File${json_path}nameOfJsonFile.json

It works for me on API testing, to read .json and POST, like here

它适用于我的 API 测试,读取 .json 和 POST,就像这里

*** Settings ***
Library    Collections
Library    ExtendedRequestsLibrary 
Library    OperatingSystem
*** Variables ***  
${uri}    https://blabla.com/service/
${json_path}    C:/home/user/project/src/json/
*** Test Cases ***
Name of Robot Test Case  
    Create Session    alias    ${uri}    
    &{headers}  Create Dictionary  Content-Type=application/json; charset=utf-8
    ${json}  Get Binary File  ${json_path}nameOfJsonFile.json
    ${resp}    Post Request    alias    data=${shiftB}    headers=${headers}
    Should Be Equal As Strings    ${resp.status_code}    200

There are also cases when you will need to transform read binary file (in my case ${json} to a dictionary but first try this simple solution.

在某些情况下,您需要将读取的二进制文件(在我的情况下为 ${json} 转换为字典,但首先尝试这个简单的解决方案。