RF文件
1 *** setting *** #设置 2 Library Selenium2Library #引入selenium2library 库 3 Test Setup log ${n} #case test setup 4 Test Teardown log ${n} #测试结束之后执行关键字 5 Suite Setup log ${n} #suite test setup 6 Suite Teardown log ${n} #suite test teardown 7 Resource resource.txt #引入外部文件· 8 9 *** Variables *** #变量,$表示字符串 @表示列表 10 ${LOGIN URL} http://127.0.0.1:8086/ 11 ${BROWSER} Chrome 12 ${n} cc 13 ${case1} case1 14 ${case2} case2 15 @{list} list1 list2 16 17 *** Test Cases *** #用例 18 19 Case1 20 Open1 21 Close Browser 22 Case2 23 log ${case2} 24 print1 ceshi 25 26 27 *** Keywords *** #创建关键字,print1中是要带参数的,如果是2个参数就是带2个文件 28 print1 29 [arguments] ${args} 30 log ${args} 31 32 Open1 33 Open Browser ${LOGIN URL} Chrome
地址:
Build-in Library(注意版本)
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html
Collections
http://robotframework.org/robotframework/latest/libraries/Collections.html
Selenium2Library
http://robotframework.org/Selenium2Library/Selenium2Library.html
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html
Collections
http://robotframework.org/robotframework/latest/libraries/Collections.html
Selenium2Library
http://robotframework.org/Selenium2Library/Selenium2Library.html
RF引入自定义库:(可以引入方法和类)
1、直接引入方法:引入py文件
py文件如下:
2、引入pkg模块:
pkg模块:
__init__.py (注意,这里的类名要和文件夹的名称一样)
1 from mylib import mylib 2 3 class ApiKeywordModule(mylib): 4 ROBOT_LIBRARY_SCOPE = 'GLOBAL' #设置为全局变量 5 6 def user_print(self, information): 7 print(information)
mylib.py
1 class mylib(object): 2 3 def __init__(self): 4 self.req = requests.Session() 5 self._get_token() 6 7 def _get_token(self): 8 url = "http://127.0.0.1:8086/en-gb/" 9 response = self.req.get(url) 10 if response.status_code == 200: 11 pattern = r"name='csrfmiddlewaretoken' value='(w+)'" 12 reg = re.compile(pattern) 13 result = reg.search(response.text) 14 self.token = result.groups()[0] 15 16 def login(self, username=None, password=None): 17 self._get_token() 18 url = "http://127.0.0.1:8086/en-gb/accounts/login/" 19 20 post_data = {"csrfmiddlewaretoken": self.token, 21 "login-username": username, 22 "login-password": password, 23 "login-redirect_url": "", 24 "login_submit": "Log In"} 25 res = self.req.post(url=url, data=post_data) 26 if res.status_code == 200: 27 if 'Account' in res.text: 28 return True 29 else: 30 return False 31 else: 32 return False 33 34 def hello(self): 35 print('hello') 36 37 def log_out(self): 38 log_out_url = "http://127.0.0.1:8086/en-gb/accounts/logout/" 39 res = self.req.get(url=log_out_url) 40 41 42 if __name__ == '__main__': 43 mylib_obj = mylib() 44 print(mylib_obj.login("test@163.com", "thisisatest1234"))