我正在使用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-3.x
设置为一个问号,所以我假设您正在计算机上运行Python3.x。 HttpLibrary
(robotframework-httplibrary
软件包)是为Python 2.x制作的,因此与您的版本不兼容。该库中完成了许多重命名的函数和旧的语法异常处理,因此您无法使用Python 3运行它。
[您可能会寻求不同的方法,或者将计算机的Python&Robot Framework安装降级为Python 2.X兼容版本。
我看到您想呼叫Should Be Valid JSON
和Get 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