Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 946
Vikraant Singh Sanwal Avatar asked Jul 14 '14 10:07

Vikraant Singh Sanwal


1 Answers

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.

like image 180
Bryan Oakley Avatar answered Oct 31 '22 07:10

Bryan Oakley