API Automation with Robot Framework: A Step-by-Step Guide

API Automation with Robot Framework: A Step-by-Step Guide person Vishnu Hari | Published: Tue Oct 2023

API automation with Robot Framework is like having a reliable assistant for your testing tasks. This framework makes it super easy to automate the testing of APIs, even if you're not a coding expert. With its simple syntax and user-friendly approach, you can create tests and perform validations effortlessly. Whether you're testing RESTful APIs, SOAP services, or any other type of web service, Robot Framework has got you covered. Say goodbye to manual testing drudgery and hello to streamlined, error-free automation. If you want to simplify your API testing process and save time, Robot Framework is the way to go. It's your shortcut to efficient and effective API automation.

Before we dive in, it's important to make sure you've read my previous blog, "Setting Up Robot Framework for Test Automation" and have all the necessary plugins installed.

I'll be working on this project using Visual Studio Code, but feel free to use any IDE you prefer, such as PyCharm or Eclipse.

Now, simply open Visual Studio Code and choose a folder where you'd like to save your project.

To start, let's create a robot file for our initial script. I'll name the file 'ApiTest.robot'.

I'm going to write code that will perform a GET and POST request on TinkerBridge Fake REST-API Service For testing. You can find the code snippet below.

*** Settings ***
Library    Collections
Library    RequestsLibrary

Suite Setup    Create Session  base  https://www.tinkerbridge.com/api

*** Variables ***
${UserID}         1
${headers}    Create Dictionary    Content-Type=application/json

*** Test Cases ***
Get User List
    [Documentation]    Retrieve user list using GET request
    ${response}    GET On Session    base    /users-list
    Should Be Equal As Strings    ${response.status_code}    200
    Should Contain     ${response.text}    User List

Get User Details
    [Documentation]    Retrieve user data using GET request
    ${response}    GET On Session    base    /user/${UserID}
    Should Be Equal As Strings    ${response.status_code}    200
    Should Contain     ${response.text}    User details

Add User
    [Documentation]    Add user using Post request
    ${data}=    Create dictionary  name=Vishnu Hari  email=vishnu@example.com
    ${response}=    POST On Session    base  /add-user  json=${data}
    Should Be Equal As Strings    ${response.status_code}    200
    Should Contain     ${response.text}    User created successfully

To execute the script, open the command prompt and navigate to the project folder. Alternatively, you can simply press 'Ctrl + `' keys to open the command line in VS Code, then enter the following command and press 'Enter'.

robot ApiTest.robot

After running the script, it will generate an HTML report in the same folder.

Console Output

HTML Output