【问题标题】:How to read a JSON for just one time and use it many time in the same robot file in Robot Framework如何只读取一次 JSON 并在 Robot Framework 的同一个机器人文件中多次使用它
【发布时间】:2021-07-14 01:00:11
【问题描述】:

我正在使用机器人框架实现测试自动化框架。但我无法管理一些 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"]}

【问题讨论】:

  • 当我修复错误并添加调用 Read Object 的测试时,您的代码对我来说工作正常。

标签: json selenium testing automated-tests robotframework


【解决方案1】:

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

是的。您可以读入数据,然后使用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}

【讨论】:

  • 您好,谢谢您的评论。我已经尝试过了,但没有用。它显示“未找到变量 ${object}”我正在使用现有代码编辑问题
  • @mert:你问题中的代码没有给我那个错误。
【解决方案2】:

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

*** Settings ***
Variables    read_json.py

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    相关资源
    最近更新 更多