robotframework接口自動化之get/post請求

*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           String
Library           HttpLibrary.HTTP

*** Keywords ***
Request_Post
    [Documentation]    通用post請求
    [Arguments]    ${host}    ${path}    ${datas}    ${params}    ${headers}=None    ${cookies}=None    ${timeout}=30
     
    # 處理請求header
    ${header_dict}    Create Dictionary    Content-Type=application/json
    Log    ${header_dict}    
    Run Keyword If    ${headers}==${None}    Log    沒有添加自定義header
    ...    ELSE    Run Keyword    add_header    ${headers}    ${header_dict}

    # 處理cookies
    ${cookies_dict}    Create Dictionary    
    Run Keyword If    ${cookies}==${None}    Log    沒有添加cookies信息         
    ...    ELSE    Run Keyword    add_cookies    ${cookies}     ${cookies_dict} 
 
    #    創建session
    Create Session    TZ_robotframework    ${host}    timeout=${timeout}    cookies=${cookies_dict}    verify=False 
 
    #    發起post請求
    ${resp}    Post Request    TZ_robotframework        ${path}    data=${datas}    headers=${header_dict}    params=${params}
    [Return]    ${resp} 
Request_Get
    [Documentation]    通用get請求
    [Arguments]    ${host}          ${path}    ${datas}    ${params}    ${headers}    ${cookies}=None    ${timeout}=30
  
    # 處理請求header
    ${header_dict}    Create Dictionary    Content-Type=application/json
    Run Keyword If    ${headers}==${None}    Log    沒有添加自定義header
    ...    ELSE    Run Keyword    add_header    ${headers}    ${header_dict}

    # 處理cookies
    ${cookies_dict}    Create Dictionary    
    Run Keyword If    ${cookies}==${None}    Log    沒有添加cookies信息         
    ...    ELSE    Run Keyword    add_cookies    ${cookies}     ${cookies_dict} 
 
    #    創建session
    Create Session    TZ_robotframework    ${host}    timeout=${timeout}    cookies=${cookies_dict}     verify=False
 
    #    發起get請求
    ${resp}    Get Request    TZ_robotframework    ${path}    headers=${header_dict}    params=${params}
    ${resp_code}    Set Variable    ${resp.status_code}
    Should Be True    ${resp_code}==200  
    [Return]    ${resp} 

這裏我們把get請求和post請求封裝成通用關鍵字並增加了添加header和cookies的處理,首先我們定義的傳入參數,

${host}爲請求域名,

${path}爲請求連接路徑,

${data}和${params}分別爲請求體,

${headers}爲請求頭,

${cookies}爲cookies,

請求頭和cookies默認設置爲空,

通過create session創建一個session實例,

再通過Post Request或者Get Request發起請求,

唯一要注意有區分的是,post請求參數data和get請求參數params的不同,但都是通過字典類型傳入。

下面是具體實例:

POST請求:

*** Settings ***
Resource    ../http通用請求/http_requests.robot
Library    Collections    
Library    HttpLibrary.HTTP    

*** Keywords ***
sso_login_token
    [Arguments]    ${account}    ${password}    ${fromType}    ${error_info}    
    ${host}    Set Variable    http://testpassport.ra******.com
    ${path}    Set Variable    /api/sso/login
    ${datas}    Create Dictionary
    Set To Dictionary    ${datas}    account    ${account}
    Set To Dictionary    ${datas}    fromType    ${fromType}
    Set To Dictionary    ${datas}    password    ${password}
    ${params}    Set Variable  
    ${res}    requests_post    ${host}    ${path}    ${datas}    ${params}       
    # log    ${content.content}
    # Log    ${content.status_code}
    ${cookies}     Set Variable      ${res.cookies}    
    Log Json    ${res.content}
    
    #提取json數據方式一:
    ${info}    Get Json Value       ${res.content}     /msg    #填寫數據所在層級路徑提取json信息 
    # 返回token  
    ${res_headers}    Set Variable       ${res.headers} 
    ${res_token}    get_cookies    ${res_headers}    
    [Return]    ${res_token}
    #提取返回數據方式二:
    ${data}    Parse Json    ${res.content}
    # ${datas}    Get From Dictionary    ${data}    data
    # ${code}    Get From Dictionary    ${data}    code
    ${msg}    Get From Dictionary    ${data}    msg
    
    # # 斷言驗證
    Should Be Equal As Strings    ${msg}    ${error_info}

上面是一個登錄接口的例子,

先定義了必傳參數${account}、${password}、${fromtype}和斷言判斷變量${error_info},

設置了接口域名${host}、接口請求路徑${path},創建字典${datas}設置傳入參數,

設置完畢通過二次封裝的requests_post關鍵字發起請求,實際同關鍵字 :Post Request請求一致,只是這裏我們多做了一層封裝;

關於提取返回數據,關鍵字get json vaule是在不轉換返回數據類型的情況下提示json數據,或者可以通過parse json關鍵字解析json,再通過get from Dictionary或者get from list獲取我們預期數據,

最後通過關鍵字should be equal as strings斷言驗證接口

 

GET請求:

*** Settings ***
Resource    ../../../../Resources/Business/http通用請求/http_requests.robot
Library    Collections    
Library    HttpLibrary.HTTP   
*** Variables ***
${host}        https://testpassport.ra******.com
${path}        /api/sso/getUserInfoByD*********
*** Test Cases ***
getUserInfoByDisposableCode_Get
    ${disposableCode}    get_code    15********    111111    pc
    ${res}    通過臨時code獲取用戶信息    ${disposableCode}
    Log Json    ${res.content} 
    ${msg}    Get Json Value    ${res.content}    /msg
    ${msg}    Parse Json    ${msg}
    Should Be Equal    ${msg}    ok    
    
getUserInfoByDisposableCode_Exception
    ${res}    通過臨時code獲取用戶信息    ${EMPTY}
    Log Json    ${res.content} 
    ${msg}    Get Json Value    ${res.content}    /msg
    Should Contain     ${msg}    一次性code沒有找到或已取出用戶數據

getUserInfoByDisposableCode_DataVerify
    ${disposableCode}    get_code    158*********    111111    pc
    ${res}    通過臨時code獲取用戶信息    ${disposableCode}
    Log Json    ${res.content} 
    ${msg}    Get Json Value    ${res.content}    /msg
    ${uId}     Get Json Value    ${res.content}    /data/uId
    ${account}    Get Json Value    ${res.content}    /data/account
    ${mobile}    Get Json Value    ${res.content}    /data/mobile
    #斷言
    Should Not Be Empty    ${msg}  
    Should Not Be Empty    ${uId}
    Should Not Be Empty    ${account}
    Should Not Be Empty    ${mobile}  
    


*** Keywords ***
通過臨時code獲取用戶信息
    [Arguments]    ${disposableCode}
    ${params}    Create Dictionary
    ${datas}    Create Dictionary 
    Set To Dictionary    ${params}    disposableCode=${disposableCode}       
    ${res}    requests_get    ${host}    ${path}    ${datas}    ${params}
    [Return]    ${res}

首先通過封裝的用戶關鍵字:get_code,獲取請求接口需要參數${disposableCode},

同樣我們把get請求接口的公共部分抽離,分裝成用戶關鍵字:通過臨時code獲取用戶信息,

這裏get請求接口的入參${params}同樣是通過字典的形式傳入,

最後獲取返回數據,提取預期字段斷言驗證如同上文POST接口所示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章