在 Robot 框架中的测试套件中设置会话 cookie [英] Set session cookie across the test suites in Robot framework
问题描述
我的应用程序是一个 RESTful API,它只有在会话 cookie 存在时才有效.不幸的是,我总是需要在网络登录中进行身份验证以获取 cookie 并将 会话 cookie 传递给 API 以建立会话.
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.
我能够找出解决方案来验证并将会话 cookie 传递给 API 并使用机器人框架编写测试用例.到这里为止的所有内容都可以在单个测试套件文件中正常工作.
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}
我的问题是我如何确保 SESSION_COOKIE 可用于机器人文件中的所有测试套件.
My question is that how do I make sure that SESSION_COOKIE is available to all the test suites across the robot files.
例如,如果我有另一个名为 update-article.robot 的测试套件文件.如何将 SESSION_COOKIE 传递给 /api/articles/update API.请让我知道测试基于身份验证的 API 的更好方法.
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.
我是否需要将 cookie 存储在 sqlite db 中或将其保存在 yml 文件中或任何更好的方法,否则我做错了一切.
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.
解决方案:
__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.
这篇关于在 Robot 框架中的测试套件中设置会话 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!