How to handle negative scenarios 401, 400, 500?

I am trying to validate negative scenario for my REST api testing and after passing negative value my response status will come 401 but not able to access response data as robot framework will give: HTTPError: 401 Client Error: Unauthorized for url:

How to handle it and get the response body of rest api.

Appreciate, If you could help on this.

You could wrap in a try catch or use one of the builtin keywords “Run Keyword And….”

Built in keyword link:
https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Expect%20Error

Pretty sure the expect error will return the message, so you could possibly make a keyword that handles all status codes to reduce code duplication.

EDIT*
Link for examples of try catch

Thank you for your reply. I’m new to robot framework. can you correct me if i’m wrong? :slight_smile:

==========================

Common Keywords.robot file

*** Keywords ***

Verify Error Message
[Arguments] ${parsed_response} ${expected_status_code}
Error ‘${expected_status_code}’ == ‘401’
… Should Contain ${parsed_response[‘error_message’]} Unauthorized # Verify the ‘error_message’ field for 401
… ELSE IF ‘${expected_status_code}’ == ‘404’
… Should Contain ${parsed_response[‘error_message’]} Not Found # Verify the ‘error_message’ field for 404
… ELSE IF ‘${expected_status_code}’ == ‘500’
… Should Contain ${parsed_response[‘error_message’]} Internal Server Error # Verify the ‘error_message’ field for 500
… ELSE
… Log No specific error message verification for status code ${expected_status_code}

Calling_API_POST
[Arguments] ${method} ${endpoint} ${expected_status_code} ${data} ${headers} ${timeout}
${response}= Run Keyword RequestsLibrary.${method} ${endpoint} json=${data} headers=${headers} timeout=${TIMEOUT}
${parsed_response}= Set Variable ${response.json()}
Should Be Equal As Strings ${response.status_code} ${expected_status_code}
Run Keyword If ${response.status_code} != 200 Backend_CommonKeywords.Verify Error Message ${parsed_response} ${expected_status_code}

[Return] ${response}

==========================

testcase.robot file

*** Keywords ***

POST /api/auth/oauth/token
[Tags] Login Dashboard Regression
[Arguments] ${data} ${expected_status_code} ${parameter_name} ${expected_value} @{expected_values}

Run Keyword If “${DATA_NIC_VERIFY}” != “None” Set To Dictionary ${data} request_token=${DATA_NIC_VERIFY}
${request_headers}= Backend_CommonKeywords.Onboarding_Headers
${response}= Backend_CommonKeywords.Calling_API_POST POST ${BACKEND_URL}/api/auth/oauth/token ${expected_status_code} ${data} ${request_headers} ${TIMEOUT}

${length}=    Get Length    ${expected_values}

FOR ${index} IN RANGE 0 ${length} 2
${expected_param}= Set Variable ${expected_values}[${index}]
${expected_value}= Set Variable ${expected_values}[${index + 1}]
Backend_CommonKeywords.Validating_Response_Message ${response.content} ${expected_param} ${expected_value}
END

*** Test Cases ***

POST /api/auth/oauth/token - Fail
[Tags] Login Dashboard Regression
[Template] POST /api/auth/oauth/token

${LOGIN_ACCESS_TOKEN_INVALID_PIN} 401

Hi Nishan,

In the documentation fo POST you’ll see expected_status

By default this keyword fails if a status code with error values is returned in the response, this behavior can be modified using the expected_status and msg parameters, read more about it in Status Should Be keyword documentation. In order to disable this implicit assert mechanism you can pass as expected_status the values any or anything.

New requests keywords like GET or GET On Session (starting from 0.8 version) already have an implicit assert mechanism that, by default, verifies the response status code. Status Should Be keyword can be useful when you disable implicit assert using expected_status=anything.

You probably want something like this:

POST    ${endpoint}    json=${data}    expected_status=401    headers=${headers}    timeout=${TIMEOUT}

Then that keyword will pass and the following keywords will work as you want.

You can do the same with expected_status=400 and expected_status=500 as needed

Dave.

2 Likes

Thank you very much @_daryl . It’s working now.

*** Keywords ***
Calling_API_POST
[Arguments] ${method} ${endpoint} ${expected_status_code} ${data} ${headers} ${timeout}
${response}= Run Keyword RequestsLibrary.${method} ${endpoint} json=${data} expected_status=${expected_status_code} headers=${headers} timeout=${TIMEOUT}
${parsed_response}= Set Variable ${response.json()}
Should Be Equal As Strings ${response.status_code} ${expected_status_code}

[Return]    ${response}

1 Like

Was all @damies13 :wink:

2 Likes