NinjaGaiden
NinjaGaiden

Reputation: 3146

generating items from json file in robot framework

At the moment I have a custom library to read a json file and output a list. For example

def get(f):
  with open(f) as fd:
    data=json.load(fd)
  return [ k for k in data['d']['a'] ]

Then in RF, I call it like this

@{items}=    get     "f.json"

Is there a way I can do this natively in Robotframework without my custom function? I looked thru HttpLibrary but couldn't find anything relevant.

Upvotes: 0

Views: 1587

Answers (1)

Laurent Bristiel
Laurent Bristiel

Reputation: 6935

yes, it is possible. Here is how to do it without HttpLibrary, but using OperatingSystem Robot Framework Library (to open the file), and json Python library (to load the JSON):

*** Settings ***
# Import Robot Framework Libraries
Library  OperatingSystem
# Import Python Library
Library  json

*** test cases ***
mytest
    # no need for double quote around file name. Variables are string by default
    @{item} =    get_in_robot    f.json

*** Keywords ***    
get_in_robot
    [Arguments]    ${file_path}
    ${data_as_string} =    Get File    ${file_path}
    ${data_as_json} =    json.loads    ${data_as_string}
    # looking into the dict at ["d"]["a"] will return the list
    [Return]    ${data_as_json["d"]["a"]}

Hope this helps

Upvotes: 1

Related Questions