如何在robot Framework中只读取一次JSON并在同一个robot文件中多次使用它



我正在使用robot框架实现一个测试自动化框架。但我无法管理一些JSON内容。。我有一个机器人文件,里面装满了关键字(没有测试(,我试图在测试设置部分读取json文件(里面装满了xpath(,但失败了。

是否可以读取robot文件开头的文件,并将该对象用于该列表中的每个关键字?

当前结构:

keyword 1
read json
do sth
keyword 2
read json
do sth
keyword 3
read json
do sth

我想要什么?

read json
keyword 1
do sth
keyword 2
do sth
keyword 3
do sth

当前实施情况:

*** 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"]}

是否可以读取robot文件开头的文件,并将该对象用于该列表中的每个关键字?

是。您可以读取中的数据,然后使用Set suite变量来创建一个在套件中随处可用的变量。

以下示例定义了可以在套件设置中运行的关键字。它将读取一个json文件(存储在robot变量${data_file}中(,并将数据保存到名为${data}的套件变量中。然后,您可以在套件中的任何测试中使用${data}

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

您可以使用Suite Setup关键字设置套件级变量,也可以通过创建自定义Python脚本来使用变量文件,该脚本读取JSON文件并返回它。

*** Settings ***
Variables    read_json.py

最新更新