简体   繁体   中英

How to read a JSON for just one time and use it many time in the same robot file in Robot Framework

I am implementing a test automation framework with using robot framework. But i couldn't manage some JSON things.. I have a robot file full of keywords (no tests), i tried to read the json file (with full of xpaths) in test setup section, but it failed.

Is it possible to read the file at the beginning of a robot file and use that object for every keyword in that list?

Current structure:

keyword 1
  read json
  do sth

keyword 2
  read json
  do sth

keyword 3
  read json
  do sth

What i want?

read json

keyword 1
  do sth
keyword 2
  do sth
keyword 3
  do sth

Current İmplemantation:

*** Settings ***
    Library   SeleniumLibrary
    Library   JSONLibrary
    Library   OperatingSystem
    Resource  ../PageObjects/LoginPage.robot


Suite Setup  Read object repository locators
*** Keywords ***

Read object repository locators
        [Documentation]  For all elements on website, this keyword is returns an 
        xpath collection for other keywords
        # read the json data
        ${json}=  Get file  ../library/ObjectRepository.json
        # convert the data to a object
        ${object}=  Evaluate  json.loads(r'''${json}''')
        set suite variable  ${object}

Read object
       ${password}=  Set Variable  ${object["registerInfo"][0]["password"]}

Is it possible to read the file at the beginning of a robot file and use that object for every keyword in that list?

Yes. You can read the data in and then use Set suite variable to create a variable that is available everywhere in the suite.

The following example defines a keyword you can run in a suite setup. It will read in a json file (stored in the robot variable ${data_file} ) and save the data to a suite variable named ${data} . You can then use ${data} in any test in the suite.

*** Settings ***
Suite Setup    Load JSON data

*** Keywords***
Load JSON data
    ${data}=     evaluate  json.load(open($data_file, 'r'))
    set suite variable  ${data}

You can use Suite Setup keyword to set suite level variable or you can use variable files by creating custom Python script which reads JSON file and returns it.

*** Settings ***
Variables    read_json.py

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