如何在机器人框架中将json数据文件加载到变量中? [英] How to load a json data file into a variable in robot framework?

查看:207
本文介绍了如何在机器人框架中将json数据文件加载到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试直接在Robot Framework中将json数据文件加载到变量中.任何人都可以用例如提供有关如何执行的确切语法? 在此先感谢:)

I Am trying to load a json data file into a variable directly in Robot Framework. Can anyone please elaborate with an e.g. giving the exact syntax as to how to do it? Thanks in advance :)

推荐答案

一种方法是使用 OperatingSystem 库中获取关键字,然后使用内置的<使用href ="http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Evaluate">评估关键字将其转换为python对象.

One way would be to use the Get File keyword from the OperatingSystem library, and then use the built-in Evaluate keyword to convert it to a python object.

例如,考虑一个名为example.json的文件,其内容如下:

For example, consider a file named example.json with the following contents:

{
    "firstname": "Inigo",
    "lastname": "Montoya"
}

您可以使用以下方式记录名称:

You can log the name with something like this:

*** Settings ***
| Library | OperatingSystem

*** Test Cases ***
| Example of how to load JSON
| | # read the raw data
| | ${json}= | Get file | example.json
| | 
| | # convert the data to a python object
| | ${object}= | Evaluate | json.loads('''${json}''') | json
| | 
| | # log the data
| | log | Hello, my name is ${object["firstname"]} ${object["lastname"]} | WARN

当然,您也可以使用python编写自己的库来创建执行相同操作的关键字.

Of course, you could also write your own library in python to create a keyword that does the same thing.

这篇关于如何在机器人框架中将json数据文件加载到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆