json - 如何在 Robot Framework 中的同一个机器人文件中仅读取一次 JSON 并多次使用它

标签 json selenium testing automated-tests robotframework

我正在使用机器人框架实现一个测试自动化框架。但我无法管理一些 JSON 的东西..我有一个充满关键字的机器人文件(没有测试),我尝试在测试设置部分读取 json 文件(充满 xpath),但失败了。

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

当前结构:

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

最佳答案

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

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

以下示例定义了可以在套件设置中运行的关键字。它将读取一个 json 文件(存储在机器人变量 ${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}

关于json - 如何在 Robot Framework 中的同一个机器人文件中仅读取一次 JSON 并多次使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67168628/

相关文章:

javascript - 无法从 json 填充 html 表

用长数字解码的 JSON 给出 float

json - 返回 JSON 对象

node.js - 使用 sinon 和 mocha 测试 node.js http.get

testing - Jasmine spy 示例如何工作

c# - 自动调用 Controller 方法并生成局部 View 。如何?

python - 从环境变量复制文本并粘贴到 Selenium (Python)

java - Selenium WebDriver - 尝试使用 "java.lang.NullPointerException"获取属性值时获取 "getAttribute"

python - 如何在 Python Webdriver 中运行 1 个以上的测试用例。我的测试用例类中只有 1 个运行

python - 从 Tkinter 运行线程并等待它完成