简体   繁体   English

我需要使用Robot Framework从YAML文件中获取值

[英]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

Here's a way to get it up and running: 这是一种启动和运行的方法:

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文档: httpsPyYAML

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.

相关问题 如何从 .yaml 文件访问变量到机器人框架脚本? - How to access variables from .yaml file to robot framework script? 我如何使用机器人框架滚动列表视图的末尾? - How do i need to scroll end of the listview using robot framework? 使用 Robot Framework 读取 CSV 文件并从第 n 列中查找值 - CSV file reading and find the value from nth Column using Robot Framework 将动态变量值从机器人框架传递到 python 文件 - Pass dynamic variable value from robot framework to python file 如何使用机器人框架从文件浏览器中选择PC中的文件? - How can I select files from PC in the file explorer using robot framework? 有没有办法访问作为参数传递给机器人框架的 YAML 变量文件的名称? - Is there a way to access the name of the YAML variable file passed as an argument to robot framework? 变量 Yaml 文件中的机器人框架系统变量 - Robot Framework System Variable inside Variable Yaml file 使用Robot Framework从Python中的方法返回值 - Return Value from Method in Python using Robot Framework 如何使用python(机器人框架)将.properties文件中的数据读取到.robot文件中 - how to read data from .properties file into .robot file using python(robot framework) 使用Robot Framework从Windows目录上载文件 - Uploading file from windows directory using Robot Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM