Verifying Status Code and Status Line in Robot Framework

HOME

In this article, we will discuss how to verify status code and status line in Robot Framework. 

HTTP/1.1 200 OK

RequestLibrary is a Robot Framework library aimed to provide HTTP API testing functionalities by wrapping the well-known Python Requests Library.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework RequestLibrary
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Create API tests to verify status code

*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           BuiltIn

*** Variables ***
${BASE_URL}          https://reqres.in/api/users/2

*** Test Cases ***
Validate HTTP Response Details
    [Documentation]   This test case validates the HTTP response status code, line, body, headers, and content type

    # Create session
    Create Session    api    ${BASE_URL}

    # Create headers dictionary in the test case
    ${HEADERS}=       Create Dictionary      Content-Type=application/json

    # Send GET request
    ${response}=      GET On Session    api    url=${BASE_URL}    headers=${HEADERS}

    # Validate Status Code
    Should Be Equal As Numbers    ${response.status_code}    200
    Log To Console    Status Code: ${response.status_code}

   # Construct expected status line dynamically
    ${expected_status_line}=    Set Variable    HTTP/1.1 ${response.status_code} OK
    Log To Console    Expected Status Line: ${expected_status_line}

    # Validate Status Line
    ${actual_status_line}=   Evaluate    f"HTTP/1.1 ${response.status_code} OK"
    Should Be Equal    ${actual_status_line}    ${expected_status_line}
    Log To Console    Status Line: ${actual_status_line}

Create Session    api    ${BASE_URL} 
 ${HEADERS}=       Create Dictionary      Content-Type=application/json
  ${response}=      GET On Session    api    url=${BASE_URL}    headers=${HEADERS}

${response}= We are saving the response of the GET operation in the ${response} variable.

4. Asserts that the response status code is 200, indicating a successful request. We have logged the Status Code too

Should Be Equal As Numbers    ${response.status_code}    200
Log To Console    Status Code: ${response.status_code}

5. Set Variable to construct expected Status Line. It dynamically constructs the expected status line using ${response.status_code}.

${expected_status_line}=    Set Variable    HTTP/1.1 ${response.status_code} OK

${actual_status_line}=   Evaluate    f"HTTP/1.1 ${response.status_code} OK"

Execute the tests

We need the below command to run the Robot Framework script.

robot ValidateStatusCode.robot

The output of the above program is

View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
Page Object Model in the Robot Framework
Parallel Testing in Robot Framework
How to load data from CSV files in the Robot Framework?

Leave a comment