【问题标题】:can we pass multiple args in Get Json Value key?我们可以在 Get Json Value 键中传递多个参数吗?
【发布时间】:2020-03-04 21:29:30
【问题描述】:

我现在通过 Get Json Value 键获取一个 arg 值

Create Session  Get_Inventory_Details   ${Base_URL}                 
${Headers}= Create Dictionary   Content-Type=application/json   Authorization=bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6InByaXRpc3dhaW4iLCJpYXQiOjE1Njc1MTQ2NDd9.o3qTPbj2KH6AgHpIf1BLV5nOlGL-fR83wSGAYyuWsBQ              
${Response}=    Get Request Get_Inventory_Details   inventorybyOutlet/7 headers=${Headers}          
Log To Console  ${Response.status_code}                     
Log Many    ${Response.content}                     
${actual_response}= Convert To String   ${Response.status_code}                 
Should Be Equal ${actual_response}  200 
${quantity}=    Get Json Value  ${Response.content} /0/currentInventory             
Run Keyword If  ${quantity}>10  Log Threshold Condition Pass    ELSE    Log Threshold Condition Fail

如果我想在 Get Json Value

中传递多个参数,可能的出路是什么

我试过了

${quantity}=    Get Json Value  ${Response.content} /0/currentInventory/0/itemid

Reference to Json Data


[ 
   { 
      "isInventoryOperationEnable":1,
      "itemId":1,
      "name":"Afghani Chicken Tikka Biryani (Heavy Eater)",
      "posName":"Afghani Chicken Tikka Biryani (Heavy Eater)",
      "image":"https://d30mle0t4iy75h.cloudfront.net/websiteV2/images/menuItems/AfghaniTikkaBiryani.jpg",
      "typeid":2,
      "isCombo":0,
      "currentInventory":100,
      "categoryId":1,
      "categoryname":"Biryani",
      "subCategoryId":1,
      "sequenceInCategory":1
   },
   { 
      "isInventoryOperationEnable":1,
      "itemId":3,
      "name":"Chicken Tikka Biryani (Heavy Eater)",
      "posName":"Chicken Tikka Biryani (Heavy Eater)",
      "image":"https://d30mle0t4iy75h.cloudfront.net/websiteV2/images/menuItems/ChickenTikkaBiryani.jpg",
      "typeid":2,
      "isCombo":0,
      "currentInventory":100,
      "categoryId":1,
      "categoryname":"Biryani",
      "subCategoryId":1,
      "sequenceInCategory":14
   }
      ]

但是 RobotFramework 会抛出错误:- JsonPointerException: Document '' 不支持索引,必须是映射/序列或支持 __getitem__

Error Message on RobotFramework

请任何人帮助找出在这种情况下如何传递多个 arg 的方法

谢谢

【问题讨论】:

  • 如果有 JSON 数据以便可以复制粘贴(即不是图像格式)会很好。
  • 请创建一个minimal reproducible example,其中包含一小块硬编码的 json 数据,模拟真实数据,但尽可能小以说明问题。
  • 感谢您的建议,我已经添加了一小块Json数据供参考。

标签: python-2.7 http robotframework


【解决方案1】:
Create Session  Get_Inventory_Details   ${Base_URL}                 
${Headers}= Create Dictionary   Content-Type=application/json   Authorization=bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6InByaXRpc3dhaW4iLCJpYXQiOjE1Njc1MTQ2NDd9.o3qTPbj2KH6AgHpIf1BLV5nOlGL-fR83wSGAYyuWsBQ              
${Response}=    Get Request Get_Inventory_Details   inventorybyOutlet/7 headers=${Headers}          
Log To Console  ${Response.status_code}                     
Log Many    ${Response.content}                     
${actual_response}= Convert To String   ${Response.status_code}                 
Should Be Equal ${actual_response}  200 
${response_content_json}=   To Json    ${Response.content}    #To Json is from RequestsLibrary
${quantity}=    Set Variable    ${response_content_json}[0][currentInventory]
Run Keyword If  ${quantity}>10  Log Threshold Condition Pass    ELSE    Log Threshold Condition Fail

在这种情况下,${quantity}100

注意两点:

  • 转成JSON后,变量值可以像dictionary类型变量一样访问
  • To JSON 关键字来自RequestsLibrary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多