RF-RequestsLibrary


一、定義

接口測試:接口測試通常是系統之間交互的接口,或者某個系統對外提供的一些接口服務

分類:RESTful、webservice接口

二、安裝

進入C:\Pyhon27\scripts

先要安裝requests,再安裝requestsLibrary

安裝requests:pip install requests,官方下載地址:https://pypi.python.org/pypi/requests

安裝requestsLibrary:pip install robotframework-requests,官方下載地址:https://pypi.python.org/pypi/robotframework-requests/

在testsuit中導入RequestsLibrary  requests和selenium2Library(注意大小寫),若沒有報錯(報錯為紅色顯示),則安裝成功

三、requestsLibrary關鍵字

關鍵字 備注
Create Session

創建一個HTTP會話:

alias, url, headers={}, cookies=None, auth=None, timeout=None, proxies=None, verify=False

url:即服務器的url

alias:使用Robot Framework的別名表示當前會話。

header:使用默認的headers字段

auth:NTLM鑒權需用username&password格式

timeout:會話超時時長

proxies:代理服務器的url

verify:如果需要證書請求則置為true

Create Ntlm Session

創建一個HTTP會話:

alias, url, auth, headers={}, cookies=None, timeout=None, proxies=None, verify=False

url:即服務器的url

alias:使用Robot Framework的別名表示當前會話。

header:使用默認的headers字段

auth:NTLM鑒權需用[‘domain’,’username’,’password’]格式

timeout:會話超時時長

proxies:代理服務器的url

verify:如果需要證書請求則置為true

Delete 棄用,查看delete request
Delete All Sessions 刪除全部的會話
Delete Request

alias, uri, data=(), headers=None, allow_redirects=None

使用別名刪除會話

Alias:需要刪除的會話的別名

Get 棄用:使用Get Request
Get Request

根據提供的別名查找會話並在會話中發送GET請求

alias, uri, headers=None, params={}, allow_redirects=None

Head 棄用:使用Head Request
Head Request

根據提供的別名查找會話並在會話中發送HEAD請求

alias, uri, headers=None, allow_redirects=None

Options 棄用:使用Options Request
Options Request

根據提供的別名查找會話並在會話中發送Options請求

alias, uri, headers=None, allow_redirects=None

Post

棄用:使用Post Request

Post Request

根據提供的別名查找會話並在會話中發送POST請求

alias, uri, data={}, headers=None, files={}, allow_redirects=None

To Json 將文本轉換成json對象


 

四、Restful api測試案例一

Create Session api http://api.jisuapi.com   創建一個連接到服務的host
${addr} Get Request api /driverexam/query  發送一個get請求附上api+路徑,返回結果到對象addr
Should Be Equal As Strings ${addr.status_code} 200   返回結果的狀態碼是否為200 
Log ${addr.content}     日志打印返回結果的內容 
Log ${addr.json()}     日志打印返回結果的json格式 
${responsedata} To Json ${addr.content}   返回結果轉化成json格式賦值給變量responsedata 
${keys} Get Dictionary Keys ${responsedata}  

Get Dictionary Keys關鍵字需要導入Collections庫

取出json串的keys

${items} Get Dictionary Items ${responsedata}   取出json串的items 
${values} Get Dictionary Values ${responsedata}   取出json串的values 
${str} Get From Dictionary ${responsedata} status 從json串中獲取的值是否有status 
${addr}        第二段測試
Should Be Equal As Strings       ... 
Log       ... 
${responsedata}       ... 
${keys}       ... 
${items}       ... 
${values}       ... 
${str}       ... 
Delete All Sessions       刪除所有sessions 

 接口情況

測試log

二、返回XML格式的,通常Web Service都是返回一個xml格式的內容

#xml方式        
Create Session api http://localhost:8000   創建一個連接到服務器的host
${dict} Create Dictionary accept=application/xml   在Request的essay-header里加上一個accept=application/xml
${addr} Get Request api /hello/qitao 發送一個get請求附上api+路徑,返回結果到對象addr
Comment Should Be Equal As Strings ${addr.status_code} 200 備注
Log ${addr.content}     打印對象內容
${responsedata} Set Variable ${addr.content}   設置變量,結果對象內容賦值給responsedata
${body} Get Element Text ${responsedata} hello

XML庫的關鍵字Get Element Text來獲取xml的hello節點的文本

此時變量hello中的內容為qitao

${hello} Get Element ${responsedata} hello 用Get Element獲取到一個Element對象${hello}
Log ${hello.text}     對象${hello}的屬性值,qitao
${responsedata} Add Element ${responsedata} <new id="3">test</new>

XML庫的關鍵字新增Element   <new id="3">test</new>

${new} Get Element Attribute ${responsedata} id 取值新增element的id值賦給new變量
Log ${new}     打印new變量的值
${a} Element To String ${responsedata}   <response><hello>qitao</hello><new id="3">test</new></response>
Delete All Sessions        

接口情況

 

三、有用戶名和密碼的情況下

四、Post請求方式

例子二

第5行也可以這么寫

五、其他方式

第1行:獲取cookie

第2行:創建一個請求header信息(可以根據自己的情況添加所需的值,類似於jmeter的頭文件,不一定是必須的)

第3行:創建session。【注】:這里的api是指alias,即對當前創建的HTTP會話指定的別名。在get request時,可識別session。

第4行:發起請求。返回對象${addr}

第5行:判斷請求響應是否為200:200就繼續執行用例,否就失敗終止

第7行:轉成json串格式

第8行:獲取json中的count值

第9行:獲取json中的data值

第12行:判斷count值是否與“預期值:7”一致。一致則通過。

備注:

返回值為編碼格式,想變成中文,將參數${data}變為@{data}。

post數據

${data} set Variable { "version": "1.0"}

${uri} set variable /xxxx/xxxx/query

${dict} create Dictionary Host=xxx Content-Type=application/json

create session query http://api.xxxx.com ${dict}

${response} post request query ${uri} ${data} headers=${dict}

${res} To Json ${response.content}

log ${res["result_msg"]}

請求數據設置成變量${data}

${uri} 參數設置

構造請求頭字典${dict} 創建一個query session

${response} 接收請求變量

${response.content} 轉成json 對象

打印請求結果中的內容

資料地址:

 

 

http://blog.sina.com.cn/s/blog_7447a0160102wi53.html

http://blog.csdn.net/jasonliujintao/article/details/69524559


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM