Robot Framework API Testing GET Request
Step-1: we add the necessary libraries to our project
Step-2: Create a file its extension called .robot
Step-3: We add necessary library to our project
Library SeleniumLibrary
Library RequestsLibrary
Library Collections
Step-4:This is our base url for api testing
*** Variables ***
${baseUrl} https://jsonplaceholder.typicode.com
${endPoint} /todos/1
Step-5:creating a session and store request to ${response} variable
*** Test Cases ***
# name of test case
Get Request
create session mysesion ${baseUrl}
${response}= get request mysesion ${endPoint}
Step-6 log to console content,statuscode,headers of response
log to console ${response.content}
log to console ${response.status_code}
log to console ${response.headers}
Step-7: statuscode of response convert to string and store a variable
${code}= convert to string ${response.status_code}
Step-8: validation of status code equal 200
should be equal ${code} 200
Step-9: content of response convert to string and store a variable
${body}= convert to string ${response.content}
Step-10: validation of status content contain id word
should contain ${body} id
Step-11: grab response.headers values using get from dictionary
${contentTypeValue}= get from dictionary ${response.headers} Content-Type
Step-12:log to console value of Content-Type
log to console ${contentTypeValue}
Step-13: validation of Content-Type value equal application/json; charset=utf-8
should be equal ${contentTypeValue} application/json; charset=utf-8
Step-14:The content of our robot extension file should appear as follows.
Step15: All code of project
*** Settings ***
# we add necessary libray
Library SeleniumLibrary
Library RequestsLibrary
Library Collections
*** Variables ***
#This is our base url for api testing
${baseUrl} https://jsonplaceholder.typicode.com
# and this is end point too
${endPoint} /todos/1
*** Test Cases ***
# name of test case
Get Request
#creating a session
create session mysesion ${baseUrl}
# store request to ${response}
${response}= get request mysesion ${endPoint}
# log to console content,statuscode,headers of response
log to console ${response.content}
log to console ${response.status_code}
log to console ${response.headers}
# statuscode of response convert to string and store a variable
${code}= convert to string ${response.status_code}
# validation of status code equal 200
should be equal ${code} 200
# content of response convert to string and store a variable
${body}= convert to string ${response.content}
# validation of status content contain id word
should contain ${body} id
# grab response.headers values using get from dictionary
${contentTypeValue}= get from dictionary ${response.headers} Content-Type
#log to console value of Content-Type
log to console ${contentTypeValue}
# validation of Content-Type value equal application/json; charset=utf-8
should be equal ${contentTypeValue} application/json; charset=utf-8
Step-16: Run your code from terminal
İf you want to get all code, visit my git repository