API Testing using Robot Framework
Welcome to Phase 5 of Robot Framework Tutorial Series. In this tutorial, we will learn API Automation using Robot Framework with detailed practical examples.
API Automation is one of the most important skills in modern automation testing. Many companies use Robot Framework for both UI and API automation.
Topics Covered
- What is API Testing?
- Install RequestsLibrary
- GET API Request
- POST API Request
- Headers & Authentication
- Status Code Validation
- Response Validation
- JSON Handling
- Nested JSON Validation
19. API Testing using Robot Framework
What is API Testing?
API Testing is used to validate backend services without using UI. Instead of clicking buttons on browser, API automation directly sends requests to server and validates response.
Advantages of API Automation
- Faster execution
- No browser dependency
- Easy backend validation
- Supports CI/CD pipelines
- Better test coverage
Common HTTP Methods
| Method | Purpose |
|---|---|
| GET | Fetch data from server |
| POST | Create new data |
| PUT | Update existing data |
| DELETE | Delete data |
Install RequestsLibrary
Robot Framework uses RequestsLibrary for API automation.
Installation Command
pip install robotframework-requests
Import RequestsLibrary
*** Settings *** Library RequestsLibrary
GET API Request Example
GET request is used to fetch data from API server.
Sample GET API Test
*** Settings ***
Library RequestsLibrary
*** Variables ***
${BASE_URL} https://jsonplaceholder.typicode.com
*** Test Cases ***
Get User Details
Create Session mysession ${BASE_URL}
${response}= GET On Session mysession /users/1
Log ${response.status_code}
Log ${response.text}
Explanation
| Keyword | Purpose |
|---|---|
| Create Session | Create API session |
| GET On Session | Send GET request |
| response.status_code | Fetch status code |
| response.text | Fetch response body |
Validate Status Code
Status code validation is one of the most important validations in API testing.
Status Code Validation Example
Should Be Equal As Strings ${response.status_code} 200
Common Status Codes
| Status Code | Meaning |
|---|---|
| 200 | Success |
| 201 | Data Created |
| 400 | Bad Request |
| 401 | Unauthorized |
| 404 | Resource Not Found |
| 500 | Server Error |
POST API Request Example
POST request is used to create new data in server.
POST Request Example
*** Settings ***
Library RequestsLibrary
*** Variables ***
${BASE_URL} https://jsonplaceholder.typicode.com
*** Test Cases ***
Create User
Create Session mysession ${BASE_URL}
${body}= Create Dictionary
... name=Chandan
... job=Automation Tester
${headers}= Create Dictionary
... Content-Type=application/json
${response}= POST On Session
... mysession
... /posts
... json=${body}
... headers=${headers}
Log ${response.text}
Should Be Equal As Strings ${response.status_code} 201
Explanation of POST Request
- Create request body using dictionary
- Pass headers
- Send POST request
- Validate response
Headers in API Testing
Headers provide additional information to API server.
Common Headers
| Header | Purpose |
|---|---|
| Content-Type | Data format type |
| Authorization | Authentication token |
| Accept | Expected response format |
Authentication Example
Many APIs require authentication token.
Bearer Token Example
${headers}= Create Dictionary
... Authorization=Bearer mytoken123
... Content-Type=application/json
20. JSON Handling in Robot Framework
Most modern APIs return response in JSON format. Therefore JSON handling is extremely important in API automation.
Sample JSON Response
{
"id": 1,
"name": "Leanne Graham",
"email": "leanne@example.com"
}
Convert Response to JSON
${json_response}= Set Variable ${response.json()}
Validate JSON Values
Example
Should Be Equal As Strings
... ${json_response}[name]
... Leanne Graham
Validate Email
Should Contain
... ${json_response}[email]
... example.com
Complete JSON Validation Example
*** Settings ***
Library RequestsLibrary
*** Variables ***
${BASE_URL} https://jsonplaceholder.typicode.com
*** Test Cases ***
Validate User API
Create Session mysession ${BASE_URL}
${response}= GET On Session mysession /users/1
Should Be Equal As Strings
... ${response.status_code}
... 200
${json_data}= Set Variable ${response.json()}
Log ${json_data}
Should Be Equal
... ${json_data}[id]
... ${1}
Should Be Equal As Strings
... ${json_data}[name]
... Leanne Graham
Should Contain
... ${json_data}[email]
... @
Nested JSON Handling
Many APIs return nested JSON structures.
Nested JSON Example
{
"company": {
"name": "Way2Testing",
"city": "Noida"
}
}
Validate Nested JSON
Should Be Equal As Strings
... ${json_data}[company][name]
... Way2Testing
JSON Array Example
[
{
"id": 1,
"name": "Chandan"
},
{
"id": 2,
"name": "Amit"
}
]
Access JSON Array Data
${json_data}= Set Variable ${response.json()}
Log ${json_data}[0][name]
Response Validation Techniques
- Status code validation
- Response body validation
- JSON key validation
- Schema validation
- Response time validation
Validate Response Time
Should Be True ${response.elapsed.total_seconds()} < 5
Real-Time API Automation Flow
Send Request
↓
Receive Response
↓
Validate Status Code
↓
Validate JSON Data
↓
Generate Report
Best Practices for API Automation
- Use reusable API keywords
- Store base URLs separately
- Validate status codes properly
- Avoid hardcoded tokens
- Handle dynamic JSON responses
Advantages of API Automation
- Fast execution
- Backend validation
- Stable automation
- Supports CI/CD
- Easy integration with frameworks
Conclusion
In this tutorial, we learned:
- API Testing using Robot Framework
- RequestsLibrary
- GET and POST requests
- Headers and Authentication
- JSON Handling
- Nested JSON Validation
API automation is one of the most important skills for automation testers. Combining UI and API automation makes your framework more powerful and industry-ready.
Frequently Asked Questions (FAQ)
Which library is used for API testing in Robot Framework?
RequestsLibrary is commonly used for API automation in Robot Framework.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data format used in APIs.
Why is API automation important?
API automation is faster, stable and validates backend functionality directly.
Can Robot Framework validate JSON responses?
Yes, Robot Framework can easily validate JSON keys and values.
No comments:
Post a Comment