I have been looking for solution regarding passing of session Cookies. Below are my codes in Robot Framework
Settings
Library String
Library OperatingSystem
Library Collections
Library RequestsLibrary
Library requests
***Variables ***
${base_url} https://api.company.net
${email} [email protected]
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** Test Cases ***
Session | Login
Login
Session | My
My_Profile
*** Keywords ***
Login
&{auth_dict}= Create Dictionary email ${email} password ${password}
Create Session loginsession ${base_url} verify=True
${response}= POST On Session loginsession /session/login json=${auth_dict}
Log ${response.json()}
#VALIDATION
${status}= Convert To String ${response.status_code}
Should Be Equal ${status} 200
${body}= Convert To String ${response.content}
Should Contain ${body} true #"status"
Should Contain ${body} abf60345-4043-4f1e-94b0-a307f9209beb #"uuid"
#GET SESSION COOKIE VALUE
${Session_cookie}= Get From Dictionary ${response.cookies} PHPSESSID
&{Cookie_value}= Create Dictionary PHPSESSID ${Session_cookie}
Set Suite Variable &{Cookie_value}
My_Profile
Create Session mysession ${base_url} cookies=&{Cookie_value} verify=True
${response}= GET On Session mysession /session/me
Log ${response.json()}
As you can see, I extracted the Cookie in (Session | Login) and tried to pass it onto the following test cast (Session | My). Although, I tried to follow the syntax, but still I'm unable to access the API with the Session Cookie. I look forward for open suggestions.
Thank you.
I got the following output-
20210225 18:40:51.355 : INFO :
POST Request : url=https://api.company.net/session/login
path_url=/session/login
headers={'User-Agent': 'python-requests/2.25.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '68', 'Content-Type': 'application/json'}
body=b'{"email": "[email protected]", "password": "Company2021"}'
20210225 18:40:51.355 : INFO :
POST Response : url=https://api.company.net/session/login
status=200, reason=OK
headers={'Date': 'Thu, 25 Feb 2021 17:40:50 GMT', 'Content-Type': 'application/json,...'PHP/7.2.34', 'Set-Cookie': 'PHPSESSID=567ta9n28tudq00f09r9g33qfc; path=/',, PUT, POST, DELETE, OPTIONS', 'Access....'Strict-Transport-Security': 'max-age=31536000;'}
body={"ok":true,"status":200,"data":{"uuid":"abf60345-4043-4f1e-94b0-a307f9209beb",...}
20210225 18:40:51.355 : INFO : ${response} = <Response [200]>
20210225 18:40:51.355 : INFO : {'ok': True, 'status': 200, 'data': {'uuid': 'abf60345-4043-4f1e-94b0-a307f9209beb',...}
20210225 18:40:51.355 : INFO : ${status} = 200
20210225 18:40:51.355 : INFO : ${body} = {"ok":true,"status":200,"data":{"uuid":"abf60345-4043-4f1e-94b0-a307f9209beb",...}
20210225 18:40:51.355 : INFO : ${Session_cookie} = 567ta9n28tudq00f09r9g33qfc
20210225 18:40:51.355 : INFO : &{Cookie_value} = { PHPSESSID=567ta9n28tudq00f09r9g33qfc }
20210225 18:40:51.355 : INFO : &{Cookie_value} = { PHPSESSID=567ta9n28tudq00f09r9g33qfc }
Ending test: Operator.Session | Login
Starting test: Operator.Session | Me
20210225 18:40:51.355 : INFO : Creating Session using : alias=mesession, url=https://api.company.net, headers={}, cookies={'PHPSESSID': '567ta9n28tudq00f09r9g33qfc'}, auth=None, timeout=None, proxies=None, verify=True, debug=0
20210225 18:40:51.555 : INFO :
GET Request : url=https://api.company.net/session/me
path_url=/session/me
headers={'User-Agent': 'python-requests/2.25.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Cookie': 'PHPSESSID=567ta9n28tudq00f09r9g33qfc'}
body=None
20210225 18:40:51.555 : INFO :
GET Response : url=https://api.company.net/session/me
status=403, reason=FORBIDDEN
headers={'Date': 'Thu, 25 Feb 2021 17:40:50 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', .... *"}
body={"ok":false,"status":403,"data":null,"errors":"5.95.67.44 not allowed"}
20210225 18:40:51.586 : FAIL : HTTPError: 403 Client Error: FORBIDDEN for url: https://api.company.net/session/me
Ending test: Operator.Session | Me
I was able to solve it by Creating a session in the first Test Case
and the pass the alias
to the following Test Case
(Instead of extracting the Cookies and passing them in the next case.)
Here is the way I did it-
*** Variables ***
${base_url} https://api.company.net
${email} [email protected]
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** Test Cases ***
Session | Login
&{auth_dict}= Create Dictionary email ${email} password ${password}
Create Session api ${base_url} verify=True
${test_uri} Set Variable /session/login
${response} POST On Session api ${test_uri} json=${auth_dict} headers=${headers}
Session | Me
${response} GET On Session api /session/me headers=&{headers}