简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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