没有名称为'Get Json Value'的关键字,发现错误或我在Robot Framework的HttpLibrary.HTTP中使用的任何关键字

问题描述 投票:0回答:1

我正在使用Robotframework-httplibrary自动执行API调用,在这种情况下,我想获取POST方法生成的令牌的值。这样我就可以在PUT方法上使用它来更改密码。但是很遗憾,我已经消除了这个错误,即使我已经声明了该库也找不到关键字。

这是一个简单的示例

*** Settings ***
Library  HttpLibrary.HTTP
Library  Collections
Library  JSONLibrary
Library  SeleniumLibrary



*** Variables ***
${web_service}=  http://10.0.50.168:18000

*** Test Cases ***

Create Large JSON Document
    ${document}=  Catenate
    ...  {
    ...  "token" : "oPVo3b3NdkW8uDL2tiyZii"
    ...  }
    Should Be Valid JSON    ${document}
    ${result}=       Get Json Value  ${document}  token
    Should Be Equal  ${result}       "oPVo3b3NdkW8uDL2tiyZii"
python python-3.x api robotframework web-api-testing
1个回答
0
投票

您已将python-3.x设置为一个问号,所以我假设您正在计算机上运行Python3.x。 HttpLibraryrobotframework-httplibrary软件包)是为Python 2.x制作的,因此与您的版本不兼容。该库中完成了许多重命名的函数和旧的语法异常处理,因此您无法使用Python 3运行它。

[您可能会寻求不同的方法,或者将计算机的Python&Robot Framework安装降级为Python 2.X兼容版本。

我看到您想呼叫Should Be Valid JSONGet Json Value。也可以不使用HttpLibrary来实现:

您可以使用Python的json库的json.loads()来验证JSON。如果它不是有效的json,它将引发异常。

代替Get Json Value,您只需将JSON存储到字典中并读取适当的字段。

这是验证JSON格式,从JSON提取token,然后断言其符合预期的示例:

*** Test Cases ***
Create Large JSON Document
    ${document}=  Catenate
    ...  {
    ...  "token" : "oPVo3b3NdkW8uDL2tiyZii"
    ...  }

    # Verify json is a valid format and set it to dictionary:
    &{JSON}=  Evaluate  json.loads('''${document}''')  json

    # Get token from JSON
    ${result}=  Set Variable  ${JSON['token']}
    Should Be Equal  ${result}       oPVo3b3NdkW8uDL2tiyZii
© www.soinside.com 2019 - 2024. All rights reserved.