我正在学习机器人框架,API自动化:
*** Settings ***
Library RequestsLibrary
Library Collections
Library String
*** Variables ***
${headers} Create Dictionary Authorization Bearer abcde
*** Test Cases ***
Make a simple REST API call
[Tags] API
Create Session my_json http://localhost:3000
Log ${headers}
${response} = Get Request my_json /posts headers=${headers}
Log ${response}
# Check the Response status
Should Be Equal As Strings ${response.status_code} 403
# ${response} = Get Request my_json /posts
${json} = Set Variable ${response.json()}
Log ${json}
Log len(${json})
Should Be Equal As Strings ${json['name']} rajesh
我在log.html中收到此错误
文档:使用给定别名发现的会话对象发送GET请求开始/结束/经过:20181209 18:43:04.159 / 20181209 18:43:04.175 / 00:00:00.016 18:43:04.175 FAIL AttributeError:' str'对象没有属性'items'
2 回答
我认为只需要更改创建字典对象 . 您应该将密钥和值传递给它 . 参考链接BuildIn(create Dictionary)
问题来自于你创建
headers
字典的方式 - 在套件文件的Variables部分中,一个人不能使用关键字,'s pure assignment there. Thus with the way you' ve定义了那里的变量,字面意思是"Create Dictionary" - 它最终作为字符串的一部分's the variable' s值 .在Variables部分中创建dict的语法如下:
注意如何声明变量 - 它的前缀不是通常的美元字符,而是一个&符号(
&
);因此,您正在指示Robotframework,var的值将是一个字典 .dict中的键值对由相等的char分隔,格式为
the_key=the_value
. 您不必将值放在引号(单引号或双引号)中 - 相反,如果您这样做,引号将存储为值的一部分;例如它们是 not 任何一种分隔符 .最后,如果值是一个字符串并且需要在其中包含多个连续的空白字符,请使用系统变量
${SPACE}
;例如: