My app is a RESTful API which works only if the session cookie exists. Unfortunately, I always need to get authenticated in a web login to get the cookie and pass the session cookie to the API's to establish session.
I am able to figure out solution to authenticate and pass the session cookie to the API's and write test cases using robot framework. Everything until here works fine in a single test suite file.
articles-config.py
ARTICLE_PREPROD = 'http://10.122.123.124:3001'
ARTICLE_CREATION_UI_API = '/api/articles/create'
ARTICLE_UPDATE_UI_API = '/api/articles/update'
session-cookie.robot
*** Settings ***
Documentation Suite description
Library Selenium2Library
*** Keywords ***
Get Authn Session
[Arguments] ${url} ${username} ${password}
[Documentation] Login using Authn
Open browser ${url} chrome
Input Text id=j_username ${username}
Input Password id=j_password ${password}
Click Element name=submit
${cookie_value} Get Cookie Value SESSION
[Teardown] Close Browser
${session_cookie} Create Dictionary SESSION=${cookie_value}
Set Suite Variable ${SESSION_COOKIE} ${session_cookie}
[Return] ${session_cookie}
article-create.robot
*** Settings ***
Documentation Suite description
Test Teardown
Library Collections
Library RequestsLibrary
Library json
Resource ../keywords/session-cookie.robot
Variables ../variables/articlesCreationData.py
Variables ../articles-config.py
Suite Setup Get Authn Session ${ARTICLE_PREPROD} username password
*** Test Cases ***
Article creation API
[Tags] ArticleCreation
Article creation from UI
Artcile2 creation API
[Tags] ArticleCreation
Article2 creation from UI
*** Keywords ***
Article creation from UI
[Documentation] Creating Article
Create Session articleCreate ${ARTICLE_PREPROD} cookies=${SESSION_COOKIE}
${headers} Create Dictionary Content-Type=application/json
${response} Post Request articleCreate ${ARTICLE_CREATION_UI_API} data=${ARTICLE_CREATE} headers=${headers}
log ${response.text}
Article2 creation from UI
[Arguments]
[Documentation] Creating Article
Create Session articleCreate ${ARTICLE_PREPROD} cookies=${SESSION_COOKIE}
${headers} Create Dictionary Content-Type=application/json
${response} Post Request articleCreate ${ARTICLE_CREATION_UI_API} data=${ARTICLE_CREATE} headers=${headers}
log ${response.text}
My question is that how do I make sure that SESSION_COOKIE is available to all the test suites across the robot files.
For example, if I have another test suite file called update-article.robot. How do I pass the SESSION_COOKIE to /api/articles/update API. Please let me know the better approach to test the authenticated based API's.
Do I need to store the cookie in a sqlite db or save it in a yml file or any better approach or I am doing everything wrong.
Solution:
__init__.robot
*** Settings ***
Documentation Suite description
Resource ../keywords/session-cookie.robot
Variables ../articles-config.py
Suite Setup Get Authn Session ${ARTICLE_PREPROD} username password
You could use the Set Global Variable keyword. This will set your variable within the global scope, and make it available in every suite and test cases executed after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With