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

后端 未结 5 1692
庸人自扰
庸人自扰 2021-01-02 18:16

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

5条回答
  •  孤独总比滥情好
    2021-01-02 19:03

    One way would be to use the Get File keyword from the OperatingSystem library, and then use the built-in Evaluate keyword to convert it to a python object.

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

    {
        "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.

提交回复
热议问题