3
votes

I am learning robot-framework, API automation:

*** 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

I am getting this error in log.html

Documentation: Send a GET request on the session object found using the given alias

Start / End / Elapsed: 20181209 18:43:04.159 / 20181209 18:43:04.175 / 00:00:00.016 18:43:04.175 FAIL AttributeError: 'str' object has no attribute 'items'

3
Can you add which line it generates the error. This is not clear to me. Are you following a tutorial? - A. Kootstra

3 Answers

4
votes

I think only create dictionary object needs to be changed. You should pass key and value to it. Refer link 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
0
votes

The issue comes from the way you are creating the headers dictionary - in the Variables section of a suite file one cannot use keywords, it's pure assignment there. Thus with the way you've defined the variable there, "Create Dictionary" was taken literally - it ended up as part of the string that's the variable's value.

The syntax to create a dict in the Variables section is like this:

*** Variables ***
&{headers}       Authorization=Bearer abcde

Note how is the variable declared - its prefix is not the usual dollar char, but an ampersand (&); thus you are instructing Robotframework the value of the var will be a dictionary.
The key-value pairs in the dict are separated by the equal char, the format is the_key=the_value. You do not have to put the value in quotes (single, or double) - on the contrary, if you do that, the quotes will be stored as part of the value; e.g. they are not any kind of separator.
Finally, if the value is a string and needs to have more than one consecutive whitespace char in it, use the system variable ${SPACE}; for example:

*** Variables ***
&{my dict}       myKey=text with ${SPACE} 3 spaces   other=value
0
votes

I am able to get successful response, but the token should be passed without double quotes. Ex :

${bearerToken}=  Bearere sdkdjflk234jrlksdklflksf