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
@dionatasmuniz
Copy link

dionatasmuniz commented Jun 30, 2022

Hi @Julian88Tex , nice code mate!
Quick question, do you know how can I call a cumulusCI keyword after this Post Request? Looks like it gets some conflicts with the cumulus session and return an error like "TypeError: You must provide login information or an instance and token".
Do you have any idea how can I solve this?

I just added an second test cases with the following:
${contact id}= Salesforce Insert Contact
... FirstName=Eleanor
... LastName=Rigby

@Julian88Tex
Copy link
Author

Julian88Tex commented Jun 30, 2022

@dionatasmuniz hey thanks so much! So if I understand the question correctly, you get an error when you run any CumulusCI keywords after you've run the gist code in full.

I think I've actually run into other versions of this error and I suspect it's due to my use of Pop From Dictionary: https://gist.github.com/Julian88Tex/a31ca502a29fa7f4bf89bf50885a174e#file-requestlibrarysuccessexample-robot-L51-L52

I think I figured out later that it would be better to use Get From Dictionary doing something like this:

${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}

If you could give that a try and let me know if it helps :)

@dionatasmuniz
Copy link

Hi @Julian88Tex,
Thanks a lot, It just works perfectly now.
I really appreciate it and thanks for your reply back.

@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