Robot Framework接口測試demo大全

一批糟、需要導(dǎo)入的庫
*** Settings ***
Library Collections
Library String
Library ../src/RequestsLibrary/RequestsKeywords.py
Library OperatingSystem
Suite Teardown Delete All Sessions

二看铆、*** Test Cases ***
Get Requests
[Tags] get
Create Session google http://www.google.com
Create Session github https://api.github.com
${resp}= Get Request google /
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Get Request github /users/bulkan
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} Bulkan Evcimen

Get Requests with Url Parameters
[Tags] get
Create Session httpbin http://httpbin.org
&{params}= Create Dictionary key=value key2=value2
${resp}= Get Request httpbin /get params=${params}
Should Be Equal As Strings ${resp.status_code} 200
${jsondata}= To Json ${resp.content}
Should be Equal ${jsondata['args']} ${params}

Get Requests with Json Data
[Tags] get
Create Session httpbin http://httpbin.org
&{data}= Create Dictionary latitude=30.496346 longitude=-87.640356
${resp}= Get Request httpbin /get json=${data}
Should Be Equal As Strings ${resp.status_code} 200
${jsondata}= To Json ${resp.content}
Should Be Equal As Strings ${resp.status_code} 200

Get HTTPS & Verify Cert
[Tags] get get-cert
Create Session httpbin https://httpbin.org verify=True
${resp}= Get Request httpbin /get
Should Be Equal As Strings ${resp.status_code} 200

Get HTTPS & Verify Cert with a CA bundle
[Tags] get get-cert
Create Session httpbin https://httpbin.org verify=${CURDIR}${/}cacert.pem
${resp}= Get Request httpbin /get
Should Be Equal As Strings ${resp.status_code} 200

Get HTTPS with Client Side Certificates
[Tags] get get-cert
@{client_certs}= Create List ${CURDIR}${/}clientcert.pem ${CURDIR}${/}clientkey.pem
Create Client Cert Session crtsession https://server.cryptomix.com/secure client_certs=@{client_certs}
${resp}= Get Request crtsession /
Should Be Equal As Strings ${resp.status_code} 200

Get With Auth
[Tags] get get-cert
${auth}= Create List user passwd
Create Session httpbin https://httpbin.org auth=${auth}
${resp}= Get Request httpbin /basic-auth/user/passwd
Should Be Equal As Strings ${resp.status_code} 200
Should Be Equal As Strings ${resp.json()['authenticated']} True

Get With Digest Auth
[Tags] get get-cert
${auth}= Create List user pass
Create Digest Session httpbin https://httpbin.org auth=${auth} debug=3
${resp}= Get Request httpbin /digest-auth/auth/user/pass
Should Be Equal As Strings ${resp.status_code} 200
Should Be Equal As Strings ${resp.json()['authenticated']} True

Post Request With URL Params
[Tags] post
Create Session httpbin http://httpbin.org
&{params}= Create Dictionary key=value key2=value2
${resp}= Post Request httpbin /post params=${params}
Should Be Equal As Strings ${resp.status_code} 200

Post Request With No Data
[Tags] post
Create Session httpbin http://httpbin.org
${resp}= Post Request httpbin /post
Should Be Equal As Strings ${resp.status_code} 200

Put Request With No Data
[Tags] put
Create Session httpbin http://httpbin.org
${resp}= Put Request httpbin /put
Should Be Equal As Strings ${resp.status_code} 200

Post Request With No Dictionary
[Tags] post
Create Session httpbin http://httpbin.org debug=3
Set Test Variable ${data} some content
${resp}= Post Request httpbin /post data=${data}
Should Be Equal As Strings ${resp.status_code} 200
Should Contain ${resp.text} ${data}

Put Request With URL Params
[Tags] put
Create Session httpbin http://httpbin.org
&{params}= Create Dictionary key=value key2=value2
${resp}= Put Request httpbin /put params=${params}
Should Be Equal As Strings ${resp.status_code} 200

Put Request With No Dictionary
[Tags] put
Create Session httpbin http://httpbin.org
Set Test Variable ${data} some content
${resp}= Put Request httpbin /put data=${data}
Should Be Equal As Strings ${resp.status_code} 200
Should Contain ${resp.text} ${data}

Post Requests
[Tags] post
Create Session httpbin http://httpbin.org
&{data}= Create Dictionary name=bulkan surname=evcimen
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Post Request httpbin /post data=${data} headers=${headers}
Dictionary Should Contain Value ${resp.json()['form']} bulkan
Dictionary Should Contain Value ${resp.json()['form']} evcimen

Post With Unicode Data
[Tags] post
Create Session httpbin http://httpbin.org debug=3
&{data}= Create Dictionary name=度假村
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Post Request httpbin /post data=${data} headers=${headers}
Dictionary Should Contain Value ${resp.json()['form']} 度假村

--post請求對比返回關(guān)鍵字(返回字典型)
Post Request With Unicode Data
[Tags] post
Create Session httpbin http://httpbin.org debug=3
&{data}= Create Dictionary name=度假村
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Post Request httpbin /post data=${data} headers=${headers}
Dictionary Should Contain Value ${resp.json()['form']} 度假村

Post Request With Binary Data in Dictionary
[Tags] post
Create Session httpbin http://httpbin.org debug=3
${file_data}= Get Binary File ${CURDIR}${/}data.json
&{data}= Create Dictionary name=${file_data.strip()}
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Post Request httpbin /post data=${data} headers=${headers}
Log ${resp.json()['form']}
Should Contain ${resp.json()['form']['name']} \u5ea6\u5047\u6751

--post請求返回對比(返回字符串型)
Post Request With Binary Data
[Tags] post
Create Session httpbin http://httpbin.org debug=3
${data}= Get Binary File ${CURDIR}${/}data.json
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Post Request httpbin /post data=${data} headers=${headers}
Log ${resp.json()['form']}
${value}= evaluate list(${resp.json()}['form'].keys())[0]
Should Contain ${value} 度假村

Post Request With Arbitrary Binary Data
[Tags] post
Create Session httpbin http://httpbin.org debug=3
${data}= Get Binary File ${CURDIR}${/}randombytes.bin
&{headers}= Create Dictionary Content-Type=application/octet-stream Accept=application/octet-stream
${resp}= Post Request httpbin /post data=${data} headers=&{headers}
# TODO Compare binaries. Content is json with base64 encoded data
Log "Success"

Post With File
[Tags] post
Create Session httpbin http://httpbin.org
${file_data}= Get Binary File ${CURDIR}${/}data.json
&{files}= Create Dictionary file=${file_data}
${resp}= Post Request httpbin /post files=${files}
${file}= To Json ${resp.json()['files']['file']}
Dictionary Should Contain Key ${file} one
Dictionary Should Contain Key ${file} two

Post Request With File
[Tags] post
Create Session httpbin http://httpbin.org
${file_data}= Get Binary File ${CURDIR}${/}data.json
&{files}= Create Dictionary file=${file_data}
${resp}= Post Request httpbin /post files=${files}
${file}= To Json ${resp.json()['files']['file']}
Dictionary Should Contain Key ${file} one
Dictionary Should Contain Key ${file} two

Post Request With Data and File
[Tags] post
Create Session httpbin http://httpbin.org
&{data}= Create Dictionary name=mallikarjunarao surname=kosuri
Create File foobar.txt content=foobar
${file_data}= Get File foobar.txt
&{files}= Create Dictionary file=${file_data}
${resp}= Post Request httpbin /post files=${files} data=${data}
Should Be Equal As Strings ${resp.status_code} 200

Put Requests
[Tags] put
Create Session httpbin http://httpbin.org
&{data}= Create Dictionary name=bulkan surname=evcimen
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Put Request httpbin /put data=${data} headers=${headers}
Dictionary Should Contain Value ${resp.json()['form']} bulkan
Dictionary Should Contain Value ${resp.json()['form']} evcimen

Head Request
[Tags] head
Create Session httpbin http://httpbin.org
${resp}= Head Request httpbin /headers
Should Be Equal As Strings ${resp.status_code} 200

Options Request
[Tags] options
Create Session httpbin http://httpbin.org
${resp}= Options Request httpbin /headers
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Key ${resp.headers} allow

Delete Request With URL Params
[Tags] delete
Create Session httpbin http://httpbin.org
&{params}= Create Dictionary key=value key2=value2
${resp}= Delete Request httpbin /delete ${params}
Should Be Equal As Strings ${resp.status_code} 200

Delete Request With No Data
[Tags] delete
Create Session httpbin http://httpbin.org
${resp}= Delete Request httpbin /delete
Should Be Equal As Strings ${resp.status_code} 200

Delete Request With Data
[Tags] delete
Create Session httpbin http://httpbin.org debug=3
&{data}= Create Dictionary name=bulkan surname=evcimen
${resp}= Delete Request httpbin /delete data=${data}
Should Be Equal As Strings ${resp.status_code} 200
Log ${resp.content}
Comment Dictionary Should Contain Value ${resp.json()['data']} bulkan
Comment Dictionary Should Contain Value ${resp.json()['data']} evcimen

Patch Requests
[Tags] patch
Create Session httpbin http://httpbin.org
&{data}= Create Dictionary name=bulkan surname=evcimen
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= Patch Request httpbin /patch data=${data} headers=${headers}
Dictionary Should Contain Value ${resp.json()['form']} bulkan
Dictionary Should Contain Value ${resp.json()['form']} evcimen

Get Request With Redirection
[Tags] get
Create Session httpbin http://httpbin.org debug=3
${resp}= Get Request httpbin /redirect/1
Should Be Equal As Strings ${resp.status_code} 200

${resp}=  Get Request  httpbin  /redirect/1  allow_redirects=${true}
Should Be Equal As Strings  ${resp.status_code}  200

Get Request Without Redirection
[Tags] get
Create Session httpbin http://httpbin.org
${resp}= Get Request httpbin /redirect/1 allow_redirects=${false}
${status}= Convert To String ${resp.status_code}
Should Start With ${status} 30

Options Request With Redirection
[Tags] options
Create Session httpbin http://httpbin.org
${resp}= Options Request httpbin /redirect/1
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Options Request httpbin /redirect/1 allow_redirects=${true}
Should Be Equal As Strings ${resp.status_code} 200

Head Request With Redirection
[Tags] head
Create Session httpbin http://httpbin.org
${resp}= Head Request httpbin /redirect/1 allow_redirects=${true}
Should Be Equal As Strings ${resp.status_code} 200

Head Request Without Redirection
[Tags] head
Create Session httpbin http://httpbin.org
${resp}= Head Request httpbin /redirect/1
${status}= Convert To String ${resp.status_code}
Should Start With ${status} 30
${resp}= Head Request httpbin /redirect/1 allow_redirects=${false}
${status}= Convert To String ${resp.status_code}
Should Start With ${status} 30

Post Request With Redirection
[Tags] post
Create Session jigsaw http://jigsaw.w3.org
${resp}= Post Request jigsaw /HTTP/300/302.html
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Post Request jigsaw /HTTP/300/302.html allow_redirects=${true}
Should Be Equal As Strings ${resp.status_code} 200

Post Request Without Redirection
[Tags] post
Create Session jigsaw http://jigsaw.w3.org debug=3
${resp}= Post Request jigsaw /HTTP/300/302.html allow_redirects=${false}
${status}= Convert To String ${resp.status_code}
Should Start With ${status} 30

Put Request With Redirection
[Tags] put
Create Session jigsaw http://jigsaw.w3.org debug=3
${resp}= Put Request jigsaw /HTTP/300/302.html
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Put Request jigsaw /HTTP/300/302.html allow_redirects=${true}
Should Be Equal As Strings ${resp.status_code} 200

Put Request Without Redirection
[Tags] put
Create Session jigsaw http://jigsaw.w3.org
${resp}= Put Request jigsaw /HTTP/300/302.html allow_redirects=${false}
${status}= Convert To String ${resp.status_code}
Should Start With ${status} 30

Do Not Pretty Print a JSON object
[Tags] json
Comment Define json variable.
&{var}= Create Dictionary key_one=true key_two=this is a test string
${resp}= Get Request httpbin /get params=${var}
Set Suite Variable ${resp}
Should Be Equal As Strings ${resp.status_code} 200
${jsondata}= To Json ${resp.content}
Dictionaries Should Be Equal ${jsondata['args']} ${var}

Pretty Print a JSON object
[Tags] json
Comment Define json variable.
Log ${resp}
${output}= To Json ${resp.content} pretty_print=True
Log ${output}
Should Contain ${output} "key_one": "true"
Should Contain ${output} "key_two": "this is a test string"
Should Not Contain ${output} {u'key_two': u'this is a test string', u'key_one': u'true'}

Set Pretty Print to non-Boolean value
[Tags] json
Comment Define json variable.
Log ${resp}
${output}= To Json ${resp.content} pretty_print="Hello"
Log ${output}
Should Contain ${output} "key_one": "true"
Should Contain ${output} "key_two": "this is a test string"
Should Not Contain ${output} {u'key_two': u'this is a test string', u'key_one': u'true'}

三侠讯、此為轉(zhuǎn)載暑刃,防止原文鏈接失效。
原文地址:https://github.com/bulkan/robotframework-requests/blob/master/tests/testcase.txt
再次感謝大佬溜嗜,有不懂的地方多跑跑這些demo,真的很管用辟躏。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捎琐,一起剝皮案震驚了整個濱河市裹匙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌籽御,老刑警劉巖惰匙,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件项鬼,死亡現(xiàn)場離奇詭異,居然都是意外死亡秃臣,警方通過查閱死者的電腦和手機(jī)奥此,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門雁比,熙熙樓的掌柜王于貴愁眉苦臉地迎上來偎捎,“玉大人,你說我怎么就攤上這事茴她≌衫危” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵慌核,是天一觀的道長。 經(jīng)常有香客問我垫桂,道長粟按,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任疼鸟,我火速辦了婚禮愚臀,結(jié)果婚禮上矾利,老公的妹妹穿的比我還像新娘。我一直安慰自己舶斧,他們只是感情好察皇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布什荣。 她就那樣靜靜地躺著,像睡著了一般嗜闻。 火紅的嫁衣襯著肌膚如雪桅锄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天翠肘,我揣著相機(jī)與錄音辫秧,去河邊找鬼。 笑死肌幽,一個胖子當(dāng)著我的面吹牛喂急,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播廊移,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼狡孔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了殃恒?” 一聲冷哼從身側(cè)響起辱揭,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤问窃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嵌戈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體听皿,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡尉姨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年啊送,在試婚紗的時候發(fā)現(xiàn)自己被綠了欣孤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡篷朵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出笔链,到底是詐尸還是另有隱情腮猖,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布坪创,位于F島的核電站莱预,受9級特大地震影響项滑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜危喉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一摘完、第九天 我趴在偏房一處隱蔽的房頂上張望孝治。 院中可真熱鬧,春花似錦谈飒、人聲如沸杭措。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泉懦。三九已至,卻和暖如春巡球,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背险胰。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工矿筝, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留跋涣,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓奖年,卻偏偏與公主長得像沛贪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子水评,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容