Skip to content

Instantly share code, notes, and snippets.

@Julian88Tex
Last active February 28, 2024 04:41
Show Gist options
  • Save Julian88Tex/a31ca502a29fa7f4bf89bf50885a174e to your computer and use it in GitHub Desktop.
Save Julian88Tex/a31ca502a29fa7f4bf89bf50885a174e to your computer and use it in GitHub Desktop.
A Robot Framework API test example using RequestLibrary
# Run Command: cci task run robot -o suites robot/NPSP/tests/api/success.robot -o vars BROWSER:headlesschrome
[Documentation] Success POST test.
*** Settings ***
Resource cumulusci/robotframework/Salesforce.robot
Resource robot/Cumulus/resources/NPSP.robot
Library DateTime
Library robot/NPSP/resources/RequestsLibrary/
Library cumulusci.robotframework.PageObjects
... robot/Cumulus/resources/NPSPSettingsPageObject.py
Suite Teardown Capture Screenshot and Delete Records and Close Browser
*** Variables ***
# Request Methods
${GET} = Get Request
${POST} = Post Request
${PUT} = Put Request
# Response Codes
${CODE_OK} = 200
${CODE_BAD_REQUEST} = 400
${CODE_UNAUTHORIZED} = 401
${CODE_NOT_FOUND} = 404
${CODE_SERVER_ERROR} = 500
# Tokens
${ACCESS_TOKEN} = will_be_replaced
# Bodies
${GOOD_BODY} = {"good":["body"]}
# URLs
${GOOD_BASE_URL} = /services/apexrest/ApexClassName
# Responses
${SUCCESS} = [{"result":true,"errors":[],"error":"values"}]
*** Keywords ***
Get Salesforce Org Access
[Documentation]
... Get Salesforce Org Access Token and Instance URL.
... Sets both as Global Variables.
${orginfo}= Get Org Info
${instance_url} = Get From Dictionary ${org_info}
... instance_url
${instance_url} = Set Suite Variable ${instance_url}
${access_token} = Get From Dictionary ${org_info}
... access_token
${access_token} = Set Suite Variable ${access_token}
Check For Access Token
[Documentation]
... Checks for existing Salesforce Org Access Token.
... If missing it runs Get Salesforce Org Access Token.
... Required parameters are:
...
... | access_token | access token for salesforce org |
[Arguments] ${access_token}
Run Keyword If '${access_token}' == 'will_be_replaced'
... Get Salesforce Org Access
Create Request
[Documentation]
... Creates a request an returns the response.
... Required parameters are:
...
... | method | request method Ex: GET,POST,PUT |
... | access_token | access token for salesforce org |
... | base_url | request url |
... | body | request body |
[Arguments] ${method} ${access_token} ${base_url} ${body}
Check For Access Token ${access_token}
Create Session Baseurl ${INSTANCE_URL}
${header}= Create Dictionary Authorization=Bearer ${ACCESS_TOKEN}
${response}= Run Keyword ${method} Baseurl
... ${base_url} data=${body} headers=${header}
Set Suite Variable ${response} ${response}
Log ${response} console=yes
Validate Response
[Documentation]
... Validates a request's response with a provided response code and body.
... Required parameters are:
...
... | response | request's response to be validated |
... | code | code to validate against Ex: 200,400,404 |
... | body | body to validate against |
[Arguments] ${response} ${code} ${body}
${res_body}= Convert To String ${response.content}
Log ${res_body} console=yes
Should Be Equal As Strings ${response.status_code} ${code}
Should Be Equal ${res_body} ${body}
POST Good Token, Good URL, and Good Body
Create Request ${POST} ${ACCESS_TOKEN} ${GOOD_BASE_URL} ${GOOD_BODY}
Verify Successful Response
Validate Response ${response} ${CODE_OK} ${SUCCESS}
*** Test Cases ***
1. POST - SUCCESS
POST Good Token, Good URL, and Good Body
Verify Successful Response
@Julian88Tex
Copy link
Author

@dionatasmuniz thanks for testing and great to hear! Just updated gist :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment