[英]I need to fetch value from a YAML file using Robot Framework
I am trying to substitute value inside my YAML file but unable to write a proper string using robot framework. 我试图在我的YAML文件中替换值,但无法使用机器人框架编写正确的字符串。
"patch_id_bw_data":
"pronghornResponseBody":
"shimBehaviourHeader":
"destinationURL": ""
"method": "PATCH"
"targetName": "naas"
"customHeaders": {}
"responseBody":
- "op": "replace"
"path": "/serviceCharacteristic"
"value":
- "name": "bandwidth"
"value": ""
Need to subsitute 200 in the last--> "value": "200"
Trying to do with the below syntax. 需要在
last--> "value": "200"
替换200 last--> "value": "200"
尝试使用以下语法。 But unable to subsitute: 但无法替代:
Set To Dictionary ${patch_id_bw_data.pronghornResponseBody.responseBody[0].value}
value=200M 设为字典
${patch_id_bw_data.pronghornResponseBody.responseBody[0].value}
value = 200M
First install the PyYAML python library via command line by running the following Python command: py -m pip install PyYAML
You may also want to check out the PyYAML
documentation: https://pyyaml.org/wiki/PyYAMLDocumentation 首先通过运行以下Python命令通过命令行安装PyYAML python库:
py -m pip install PyYAML
您可能还想查看PyYAML
文档: https : PyYAML
And here is an example .robot
file for doing what was requested: 这是一个示例
.robot
文件,用于执行所请求的内容:
*** Settings ***
Library yaml
Library OperatingSystem
Library Collections
*** Test Cases ***
Load YAML into Robot and edit it
${YAML}= Get File ${CURDIR}${/}data.yaml
${LOADED}= yaml.Safe Load ${YAML}
Set To Dictionary ${LOADED}[patch_id_bw_data][pronghornResponseBody][responseBody][0][value][0] value=200
${OUTPUT}= yaml.Dump ${LOADED}
Create File newdata.yaml ${OUTPUT}
${OUTPUT}
will contain the new edited yaml data already if you wish to use it inside of the test but here in the example we save it to a new file with the Create File
keyword. 如果您希望在测试中使用它,
${OUTPUT}
将包含新编辑的yaml数据,但在此示例中,我们使用Create File
关键字将其保存到新文件中。
Before running this Robot Framework test make sure you have a data.yaml
file included in the test folder containing the sample data you gave in the question. 在运行此Robot Framework测试之前,请确保测试文件夹中包含
data.yaml
文件,其中包含您在问题中提供的示例数据。 After running the Robot test there will be a new file newdata.yaml
in the folder containing 200
in the correct value field. 运行机器人测试后,在正确值字段中包含
200
的文件夹中将有一个新文件newdata.yaml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.