json - 如何只读取一次 JSON 并在 Robot Framework 的同一个机器人文件中多次使用它
问题描述
我正在使用机器人框架实现测试自动化框架。但我无法管理一些 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"]}
解决方案
您可以使用Suite Setup
关键字设置套件级别变量,也可以通过创建读取 JSON 文件并返回它的自定义 Python 脚本来使用变量文件。
*** Settings ***
Variables read_json.py
推荐阅读
- react-native - 任务 :app:processDebugResources 失败 - REACT-NATIVE - ANDROID-STUDIO
- python - 在 Python postrgres 查询字符串中循环一周的日期并将这些结果添加到 csv 数据帧列表
- android - Android 构建 Gradle
- javascript - Javascript - 加载父窗口后在 iFrame 中运行函数
- android-studio - 如何重新设计 facebook_audience_network 插件以匹配我的 Flutter 应用 UI?
- javascript - 元素的结果在通过脚本获取时发生变化
- html - 鼠标悬停时未打开子菜单
- python - 如何按匹配列匹配数据框
- c# - 为 mvc 按字段局部视图动态设置 tr 背景颜色
- angular - 如何使用 highcharts 在 X 轴上为条形图 onclick 事件添加图标/svg/图像?