【问题标题】:How to use fully functional JsonPath in RobotFramework?如何在 RobotFramework 中使用功能齐全的 JsonPath?
【发布时间】:2017-06-18 03:37:43
【问题描述】:

能否请您推荐如何在 Robot Framework 中使用 JsonPath? 它应该支持多级查询,如下所示:

$.items[?(@.status.name="closed")].name

我正在寻找完成以下工作的方法:

*** Variables ***
${json}         {"items":[{"name":"item1","status":{"id":1,"name":"opened"}},{"name":"item2","status":{"id":2,"name":"closed"}}]}
${json_path}    $.items[?(@.status.name="closed")].name
*** Test Cases ***
Get closed item
    ${names}=    Get Json Items    ${json}    ${json_path}
    Should be equal    ${names[0]}    item2

像这样的

【问题讨论】:

  • 什么是$.items?它看起来不像机器人变量。
  • 是的。它不是。它是 jsonpath 查询的一部分,指向 json 根目录下的 items 数组
  • 请展示一个机器人测试示例,您将在其中创建和使用这些对象之一。
  • 用一个例子更新了问题
  • 关键字Get Json Items是从哪里来的,还是你写的关键字?你是问怎么写那个关键字?

标签: json robotframework jsonpath


【解决方案1】:

此 RF 代码可以满足您的需求,但仅适用于提供的 json 结构。从我的角度来看,您应该使用Get Json Value 并准备您需要的关键字。顺便说一句,很明显,RF 中的这种 json/string 处理有点复杂,所以我宁愿用你需要的关键字编写小型 Python 库。

*** Settings ***
Library            Collections
Library            HttpLibrary.HTTP

*** Variables ***
${json}         {"items":[{"name":"item1","status":{"id":1,"name":"opened"}},{"name":"item2","status":{"id":2,"name":"closed"}}]}
${name}    ${EMPTY}
${result}    ${EMPTY}

*** Test Cases ***
Get Closed Item
    ${name}    Get Name By Status    ${json}    closed
    Should Be Equal As Strings    ${name}    item2
    ${name}    Get Name By Status    ${json}    opened
    Should Be Equal As Strings    ${name}    item1

*** Keywords ***
Get Name By Status
    [Arguments]    ${json}    ${status}
    [Return]    ${result}
    ${json}    Parse Json    ${json}
    :FOR    ${item}    IN    @{json["items"]}
    \    ${item}    Stringify Json    ${item}
    \    ${name}    Get Json Value    ${item}    /name
    \    ${name}    Parse Json    ${name}
    \    ${status_name}    Get Json Value    ${item}    /status/name
    \    ${status_name}    Parse Json    ${status_name}
    \    ${result}    Set Variable If    '${status_name}' == '${status}'    ${name}    ${result}

编辑:根据下面的评论,我会选择这种基于 python 的代码。

JsonpathLibrary.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import jsonpath

class JsonpathLibrary(object):

    def get_items_by_path(self, json_string, json_path):
        json_object = json.loads(json_string)
        match_object = jsonpath.jsonpath(json_object, json_path)
        match_string = json.dumps(match_object[0])
        return match_string

射频代码:

*** Settings ***
Library            JsonpathLibrary.py

*** Variables ***
${json}         {"items":[{"name":"item1","status":{"id":1,"name":"opened"}},{"name":"item2","status":{"id":2,"name":"closed"}}]}
${json_path}    $.items[?(@.status.name=="closed")].name

*** Test Cases ***
Some Descriptive Name Here
    ${name}    Get Items By Path    ${json}    ${json_path}
    Should Be Equal As Strings    ${name}    "item2"

【讨论】:

  • 感谢您的回答,但这不是我需要的。结构可以改变(它关于集成测试),这样的解决方案是不合适的。所以我需要使用 jsonpath/objectpath
  • 好的,我试过用python的jsonpath来做。 pip install jsonpath
  • 谢谢!那行得通!我做了类似的事情,但我的错误是 - 我使用 jsonpath_rw 而不是 jsonpath(谷歌对我撒谎) - 并且 jsonpath_rw 不支持我需要的“?(”结构!
  • 那我很幸运。在您提出问题之前,我不知道 jsonpath 是什么,我已经在几分钟内搜索了解决方案。我只是在为那个 jsonpath 变量苦苦挣扎——你肯定注意到了双 = 符号。一些随机的 jsonpath 在线验证器有所帮助。无论如何,我很高兴它有效。
  • 我几天前发现了它:) 但似乎它不支持组合条件:(
猜你喜欢
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 2018-05-27
  • 2017-08-22
  • 2016-01-24
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多