2017-09-08 91 views
2

我的應用程序是一個RESTful API,它僅在會話cookie存在時才起作用。 不幸的是,我總是需要在Web登錄獲得認證,以獲得Cookie以及會話cookie傳遞到API的建立會話。在Robot框架中的測試套件中設置會話cookie

我能想出的解決方案進行認證和會話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' 

會話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} 

物品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} 

我的問題是,我該如何確保會話cookie可以跨越機器人文件中的所有測試套件。

例如,如果我有另一個測試套件文件,稱爲update-article.robot。如何通過對會話cookie /API /用品/更新 API。請讓我知道更好的方法來測試基於身份驗證的API。

我需要到cookie存儲在sqlite db或將其保存在一個YML文件或任何更好的方法或我所做的一切錯誤。

解決方案:

__init __機器人

*** Settings *** 

Documentation Suite description 

Resource ../keywords/session-cookie.robot 

Variables ../articles-config.py 

Suite Setup Get Authn Session ${ARTICLE_PREPROD} username password 

回答

3

你可以使用Set Global Variable關鍵字。這將在全局範圍內設置您的變量,並使其在每個套件和測試用例之後執行。

+1

你是正確的,我甚至碰到這個解決方案來。我可能會愚蠢地忽視這一點。通過設置全局變量,我甚至可以通過__init __。robot使用Suite Setup關鍵字觸發所有測試套件的身份驗證關鍵字。 –

相關問題