【发布时间】:2018-02-17 06:05:16
【问题描述】:
我的应用程序是一个 RESTful API,仅当会话 cookie 存在时才有效。 不幸的是,我总是需要在 web 登录 中进行身份验证以获取 cookie 并将 session cookie 传递给 API 以建立会话。
我能够找出解决方案来进行身份验证并将会话 cookie 传递给 API,并使用机器人框架编写测试用例。在此之前的所有内容都可以在单个测试套件文件中正常运行。
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 可用于机器人文件中的所有测试套件。
例如,如果我有另一个名为 update-article.robot 的测试套件文件。如何将 SESSION_COOKIE 传递给 /api/articles/update API。请告诉我测试基于身份验证的 API 的更好方法。
我是否需要将 cookie 存储在 sqlite db 或将其保存在 yml 文件或任何更好的方法中,否则我做错了。
解决方案:
__init__.robot
*** Settings ***
Documentation Suite description
Resource ../keywords/session-cookie.robot
Variables ../articles-config.py
Suite Setup Get Authn Session ${ARTICLE_PREPROD} username password
【问题讨论】:
标签: python robotframework