如何使用 RequestsLibrary 在 Robot Framework 中传递会话 Cookie
How to pass session Cookies in Robot Framework using RequestsLibrary
我一直在寻找有关会话 Cookie 传递的解决方案。以下是我在 Robot Framework
中的代码
设置
Library String
Library OperatingSystem
Library Collections
Library RequestsLibrary
Library requests
***变量 ***
${base_url} https://api.company.net
${email} operator1@datavalidation.org
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** 测试用例 ***
Session | Login
Login
Session | My
My_Profile
*** 关键字 ***
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()}
如您所见,我在 (Session | Login) 中提取了 Cookie 并尝试将其传递到以下 test cast (会话 | 我的)。虽然,我尝试遵循语法,但我仍然无法使用会话 Cookie 访问 API。我期待开放的建议。
谢谢。
我得到了以下输出-
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": "operator1@company.org", "password": "Company2021"}'
20210225 18:40:51.355 : INFO :
POST Response : url=https://api-stg.hyris.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-stg.hyris.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
我能够通过在第一个 Test Case
中创建一个会话并将 alias
传递给下面的 Test Case
来解决它(而不是提取 Cookies 并将它们传递给下一个案例。)
这是我的做法-
*** 变量 ***
${base_url} https://api.company.net
${email} operator1@datavalidation.org
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** 测试用例 ***
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}
我一直在寻找有关会话 Cookie 传递的解决方案。以下是我在 Robot Framework
中的代码设置
Library String
Library OperatingSystem
Library Collections
Library RequestsLibrary
Library requests
***变量 ***
${base_url} https://api.company.net
${email} operator1@datavalidation.org
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** 测试用例 ***
Session | Login
Login
Session | My
My_Profile
*** 关键字 ***
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()}
如您所见,我在 (Session | Login) 中提取了 Cookie 并尝试将其传递到以下 test cast (会话 | 我的)。虽然,我尝试遵循语法,但我仍然无法使用会话 Cookie 访问 API。我期待开放的建议。
谢谢。
我得到了以下输出-
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": "operator1@company.org", "password": "Company2021"}'
20210225 18:40:51.355 : INFO :
POST Response : url=https://api-stg.hyris.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-stg.hyris.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
我能够通过在第一个 Test Case
中创建一个会话并将 alias
传递给下面的 Test Case
来解决它(而不是提取 Cookies 并将它们传递给下一个案例。)
这是我的做法-
*** 变量 ***
${base_url} https://api.company.net
${email} operator1@datavalidation.org
${password} Company2021
&{headers} Content-Type=application/json X-Requested-With=XMLHttpRequest
*** 测试用例 ***
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}