当前位置:网站首页>Two ways of using reuqests in RF

Two ways of using reuqests in RF

2022-06-11 23:13:00 InfoQ

Preface
I have a problem recently , Build and run regularly on the server UI When automating , Always meet in test case Of set up Stage , That is, the login phase , Page loading timeout , from log You can only see that this screenshot shows that the page has been loading , But what exactly is the timeout of loading , That is, which interface timed out , Cannot see ,  I'm going to increase the time of print request interface in the script

How to write interface related scripts here , There are two ways , One is to write a about the request interface python library ,RF To call it , The flexibility of writing your own code is relatively high .  There is also a direct use RF Third party libraries supported by the open source framework

1.  Use python requests library

use python Perform interface related operations , Will use requests library , There are also official documents about this library for reference https://cn.python-requests.org/zh_CN/latest/user/quickstart.html

install

pip install requests

Use

It supports request Requested 7 A way : get/post/options/head/put/patch/delete

The return of each method is a response object

There are two ways to call

  • (1) Respectively called requests Specific methods in

res = requests.get(url)
res = requests.post(url)
res = requests.put(url)
res = requests.patch(url)
res = requests.options(url)
res = requests.head(url)
res = requests.delete(url)

Detailed parameters , See the source code

  • (2) Use request Method

res = requests.request('GET', url)
res = requests.request('OPTIONS', url)
res = requests.request('HEAD', url)
res = requests.request('POST', url)
res = requests.request('PUT', url)
res = requests.request('PATCH', url)
res = requests.request('DELETE', url)

The above details and usage methods can refer to the official documents and source code , Today's focus is on RF How to use

give an example

For example, I was in my TestLib Create a new... In the library python Write the following code in the file , Time to get the demand request interface , Then return

def get_interface_request_duration(self, url_list, cookie_dict):
 
 url_duration_dict = {}
 for url in url_list:
 respones = requests.request('GET', url=url, headers=cookie_dict,allow_redirects=False)
 duration = respones.elapsed.total_seconds()
 url_duration_dict[url] = duration
 return url_duration_dict

stay RF In this way, I call

null
This python How files are like libraries , Can be imported ,
Please refer to my previous article # robot framework-- Extended keywords

1.  Use RF Third party library RequestsLibrary

install

pip install robotframework-requests

Use

This library also encapsulates many keywords , Refer to the documentation http://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html

example

I initiated get request , The detailed code is as follows ,  Let me introduce RequestsLibrary, Then encapsulate a user keyword to obtain the interface loading time , And then in testcase Call this user keyword in

*** Settings ***
Library RequestsLibrary

*** Test Cases ***

001_TC_test interface
 ${url_list} set variable url1 url2
 get_interface_request_duration ${url_list}
 



***Keywords***
 get_interface_request_duration
 [Arguments] @{url_list} ${mocker_cookie_dict}=${EMPTY}
 FOR ${url} IN ${url_list}
 ${RES} GET ${url} headers=${mocker_cookie_dict}
 log ${RES.elapsed.total_seconds()}

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112309461429.html