我正在学习机器人框架和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请求 给定的
alias
开始/结束/经过时间:20181209 18:43:04.159 / 20181209 18:43:04.175 / 00:00:00.016 18:43:04.175失败AttributeError:'str'对象没有 属性“项目”
答案 0 :(得分:4)
我认为只需要更改创建字典对象。您应该将键和值传递给它。 请参阅链接BuildIn(create Dictionary)
*** 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
答案 1 :(得分:0)
问题出在您创建headers
字典的方式上-在套件文件的Variables(变量)部分中,不能使用关键字,这是纯分配。因此,按照您在此处定义变量的方式,实际上是采用了“创建字典”-它最终作为变量值的字符串的一部分。
在“变量”部分中创建字典的语法如下:
*** Variables ***
&{headers} Authorization=Bearer abcde
请注意该变量是如何声明的-其前缀不是通常的美元char,而是与号(&
);因此,您正在指示Robotframework var的值将是一个字典。
字典中的键/值对以等号分隔,格式为the_key=the_value
。您不必将值放在引号中(单引号或双引号)-相反,如果这样做,引号将作为值的一部分存储;例如它们不是任何分隔符。
最后,如果该值是一个字符串并且需要包含多个连续的空格字符,请使用系统变量${SPACE}
;例如:
*** Variables ***
&{my dict} myKey=text with ${SPACE} 3 spaces other=value
答案 2 :(得分:0)
我能够得到成功的响应,但是应该不带双引号传递令牌。例如:
${bearerToken}= Bearere sdkdjflk234jrlksdklflksf