1

I am creating API tests using Robot Framework. I use requests library, do GET requests, and want to validate responses. But it shows me an error:

AttributeError: 'dict' object has no attribute 'count'.

Are there any other ways to validate the response?

library code

import requests

def get_method(URL):
    try:
        response = requests.get(URL,timeout=3)
        response.raise_for_status()
    except requests.exceptions.HTTPError as error1:
        raise TypeError("Http Error:", error1)
    except requests.exceptions.Timeout as error2:
        raise TimeoutError("Timeout Error:", error2)
    except requests.exceptions.ConnectionError as error3:
        raise ConnectionError("Error Connecting:", error3)
    return response 

keywords file

from robot.api.deco import keyword
import jsonschema
import json


class keywords:
    ROBOT_LIBRARY_SCOPE = 'TESTS'
    schemaAllUsers = {
        "type": "array",
        "count": {"type": "number"},
        "results": [{
            "name": {"type": "string"},
            "height": {"type": "number"},
            "mass": {"type": "number"}
        }]
    }

    @keyword
    def get_request(self, URL):
        return restAPI_library.get_method(URL)

    @keyword
    def assert_users_response(self, response):
        assert response.status_code == 200

    @keyword
    def get_json_response(self, URL):
        return restAPI_library.get_method(URL).json()

    @keyword
    def validate_json_response(self, response):
        assert response.count == '82'

tests file*

Library  keywords.py

*** Test Cases ***
TC - Verify GET request for all users
    ${Users}  Get Request  ${people_endpoint}
    Assert Users Response  ${Users}
    ${response}  Get Json Response  ${people_endpoint}
    VALIDATE JSON RESPONSE  ${response}

*** Variables ***
${people_endpoint}  https://swapi.dev/api/people 

enter image description here

2 Answers 2

1

This should demonstrate the problem and solution as well:

my_dict = {
    "count": 5
}

print(my_dict["count"]) # 5
print(my_dict.count) # AttributeError: 'dict' object has no attribute 'count'

Sign up to request clarification or add additional context in comments.

Comments

0

Simple solution is as Pavel hinted

@keyword
def validate_json_response(self, response):
    assert response['count'] == '82

If you want to use thi repeatedly, than what you can do is find element in paht and evaluate that element. To your python library add keyword, something like this.

@keyword
def pathGet(self, dictionary , path):

    for item in path:
        dictionary = dictionary[item]
    return dictionary

usage in test will be as follows

*** Variables ***
@{DIR_COUNT}    count 

in the test you will add this block

${count}=    pathGet    ${response}    ${DIR_COUNT}
Should Be Equal    ${count}    ${82}

this way you can reuse the keyword, regardles of the elements path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.