RobotFrameWork常用关键字学习
(1)log
log类似于python中的print:
![bc6d130e27272582f130d669da576e19.png](https://i-blog.csdnimg.cn/blog_migrate/5880a0d6ba5a1688dda84016602bc99e.png)
运行后的结果为:
![0ea4d37ddf10bc9cf788c3a403927057.png](https://i-blog.csdnimg.cn/blog_migrate/0bec104fce05062c5d9d7219f4d00706.png)
(2)定义变量Set Variable
Set Variable相当于定义一个变量,并可以对变量进行赋值,如下图,定义了一个test变量,并给变量赋值work:
![92b6c8b944612b7e4488b38fa5afd2d8.png](https://i-blog.csdnimg.cn/blog_migrate/3b0aa866cc7452f5858b55bc84f63e35.png)
(3)连接对象catenate
将n个对象连接起来(n>=0),中间以空格隔开:
![9f8de6383a67bcef906716c8916f4bdf.png](https://i-blog.csdnimg.cn/blog_migrate/555132777836a6a4caed413a485389c6.png)
运行结果如下:
![d5d00b1387f8d18007c3d3450004064c.png](https://i-blog.csdnimg.cn/blog_migrate/283e85abec4700708c0ec56160ffba26.png)
(4)定义列表create list
类似于Python中的列表:
![f0871fc3c26f02fa86926e9bbd26f10d.png](https://i-blog.csdnimg.cn/blog_migrate/b84e3d221d3fb53e0f0dc6faca39cbac.png)
运行结果如下:
![034a04897e86316292562d7ad5beb69f.png](https://i-blog.csdnimg.cn/blog_migrate/d52047dced2387bb3ab326a906166c5f.png)
运行结果中的u是为了统一编码,统一使用UTF-8
(5)时间操作get time/sleep
get time可以获取当前时间,sleep可以设置等待时间:
![791ea6ce3edea9cab0e9c4a4797b4554.png](https://i-blog.csdnimg.cn/blog_migrate/d8731385a62143ba19a670aec8843b42.png)
运行结果如下:
![7d66128962679fe7e2d0ef4831a50e78.png](https://i-blog.csdnimg.cn/blog_migrate/f2cb29ec84f3454af27481586611854a.png)
(6)if语句run keyword if
Run keyword if 类似于python中的if语句,其中,ELSE前的空格需要填写…,否则会报错:
![a707fe2c970bbf07ed423ae05020f8f2.png](https://i-blog.csdnimg.cn/blog_migrate/75bd8b63afe9afa37d20c0a2aa24b843.png)
运行结果如下:
![7d66128962679fe7e2d0ef4831a50e78.png](https://i-blog.csdnimg.cn/blog_migrate/f2cb29ec84f3454af27481586611854a.png)
(7)循环语句(:FOR in range)
循环语句:FOR,实现的功能类似python中的for循环:
![97046dafa42f09890329510e24b82896.png](https://i-blog.csdnimg.cn/blog_migrate/93df56e1e234f9641cd39c18593f9663.png)
可以使用Exit For Loop IF 来终止循环。
(8)Evaluate(通过这个关键字使用python中的方法)
在Evaluate之后添加相应表达式,即可进行相应计算:
![ea641bd26e83f03861434a78ef508eab.png](https://i-blog.csdnimg.cn/blog_migrate/78a8cde6195e5ded0b9cecd7b04d98b4.png)
运行结果如图:
![218b1bb1f9b391a17a4d33f7bb241d94.png](https://i-blog.csdnimg.cn/blog_migrate/746ae21083f06554c1087ed448e4a7e1.png)
(9)Import(Import Limbrary) 导入python脚本执行
新建一个.py文件,实现一个简单加法:
#coding=utf-8
def add(a,b):
return a+b
if __name__ == "__main__":
a = add(4,5)
print a
在RobotFrameWork中引用此python脚本:
![f5c7413fb47e3f0b1dfad3c53f4760aa.png](https://i-blog.csdnimg.cn/blog_migrate/9bd3c6760cf5cd698a87fa7e6d63414a.png)
运行结果如下:
![8a2cbe0a275c07efa2e0a523e9792e0f.png](https://i-blog.csdnimg.cn/blog_migrate/d03ab92f1815f9e435ff05a539404e5d.png)
备注:Python脚本所在的路径书写方式参照示例
如果没有对6和8进行int转换,如:
![63fef0ac65866d51fc9e72ef71db66bf.png](https://i-blog.csdnimg.cn/blog_migrate/f1d61ed59ae44622e8856ef55d812dfb.png)
则运行结果如图:
![2d91b11afc88dcb110205cabbd97e0b1.png](https://i-blog.csdnimg.cn/blog_migrate/b2b4d6d41e787e00a4e5251986d4e820.png)
(10)第三方库 Screenshot
可以在测试套中导入库:
![63fef0ac65866d51fc9e72ef71db66bf.png](https://i-blog.csdnimg.cn/blog_migrate/f1d61ed59ae44622e8856ef55d812dfb.png)
导入后在测试的脚本中添加Takeshot,就可以在测试后的log.html中看到相应截图:
![2d91b11afc88dcb110205cabbd97e0b1.png](https://i-blog.csdnimg.cn/blog_migrate/b2b4d6d41e787e00a4e5251986d4e820.png)
(11)Collections库
在测试套中导入Collections库:
![d4d36084def5d5144b8a930b17682449.png](https://i-blog.csdnimg.cn/blog_migrate/51ce9e6682abc56936241a6a925264aa.png)
Collections库所提供的关键字主要是字典,索引,列表的处理,我们可以创建字典,获取字典里的键值对,获取键,获取值,以及根据键去获取值:
![1d88799a309fa29b5c6484b06a2d416e.png](https://i-blog.csdnimg.cn/blog_migrate/da57deaaa7c02e889ace3ab21e0b2bb2.png)
运行结果如下:
![d78f03862be6c91b6e2f916ceb4ee300.png](https://i-blog.csdnimg.cn/blog_migrate/f6a64ace214a158693510501ecbe2855.png)
(12)Selenium2Library库
Selenium2Library库可以实现web自动化测试,模拟鼠标点击操作浏览器web界面元素,比如打开浏览器、关闭浏览器、搜索特定内容等。
*** Keywords ***
Checkbox应该不被选择
[Arguments] ${locator}
Checkbox Should Not Be Selected ${locator}
Frame应该包含文本
[Arguments] ${locator} ${text} ${loglevel}=INFO
Frame Should Contain ${locator} ${text} ${loglevel}
List应该无选项
[Arguments] ${locator}
List Should Have No Selections ${locator}
List选项应该是
[Arguments] ${locator} @{items}
List Selection Should Be ${locator} @{items}
Radio按钮不应该被选择
[Arguments] ${group_name}
Radio Button Should Not Be Selected ${group_name}
Radio按钮应该被设置成
[Arguments] ${group_name} ${value}
Radio Button Should Be Set To ${group_name} ${value}
Url应该包含
[Arguments] ${expected}
Location Should Contain ${expected}
Xpath应该匹配X次
[Arguments] ${xpath} ${expected_xpath_count} ${msg}= ${loglevel}=INFO
Xpath Should Match X Times ${xpath} ${expected_xpath_count} ${msg} ${loglevel}
checkbox应该被选择
[Arguments] ${locator}
checkbox should be selected ${locator}
url应该是
[Arguments] ${url}
Location Should Be ${url}
下次确认选择取消
Choose Cancel On Next Confirmation
下次确认选择确定
Choose Ok On Next Confirmation
从List中取消选项
[Arguments] ${locator} @{items}
Unselect From List ${locator} @{items}
从List选择选项
[Arguments] ${locator} @{items}
Select From List ${locator} @{items}
偏移拖拽
[Arguments] ${source} ${xoffset} ${yoffset}
Drag And Drop By Offset ${source} ${xoffset} ${yoffset}
元素应该不包含文本
[Arguments] ${locator} ${expected} ${msg}=
Element Should Not Contain ${locator} ${expected} ${msg}
元素应该不可用
[Arguments] ${locator}
Element Should Be Disabled ${locator}
元素应该不可见
[Arguments] ${locator} ${msg}=
Element Should Not Be Visible ${locator} ${msg}
元素应该包含文本
[Arguments] ${locator} ${text}
Element Should Contain ${locator} ${text}
元素应该匹配x次
[Arguments] ${locator} ${count} ${msg}= ${loglevel}=INFO
Locator Should Match X Times ${locator} ${count} ${msg} ${loglevel}
元素应该可用
[Arguments] ${locator}
Element Should Be Enabled ${locator}
元素应该可见
[Arguments] ${locator}
Element Should Be Visible ${locator}
元素文本应该是
[Arguments] ${locator} ${expected} ${msg}=
Element Text Should Be ${locator} ${expected} ${msg}
全选List元素
[Arguments] ${locator}
Select All From List ${locator}
关闭所有浏览器
Close All Browsers
关闭浏览器
Close Browser
关闭窗口
Close Window
切换浏览器
[Arguments] ${index}
Switch Browser ${index}
创建Webdriver
[Arguments] ${driver_name} ${alias}= ${kwargs}={}
Create Webdriver ${driver_name} ${alias} ${kwargs}
删除Cookie
[Arguments] ${name}
Delete Cookie ${name}
删除所有Cookie
Delete All Cookies
删除本地策略
[Arguments] ${strategy_name}
Remove Location Strategy ${strategy_name}
刷新页面
Reload Page
双击元素
[Arguments] ${locator}
Double Click Element ${locator}
取消选择Checkbox
[Arguments] ${locator}
Unselect Checkbox ${locator}
取消选择Frame
Unselect Frame
回退
Go Back
在图片上按下鼠标左键
[Arguments] ${locator}
Mouse Down On Image ${locator}
在连接上按下鼠标左键
[Arguments] ${locator}
Mouse Down On Link ${locator}
应该出现警告弹窗
[Arguments] ${text}=
Alert Should Be Present ${text}
当前Frame不应该包含
[Arguments] ${text} ${loglvl}=INFO
Current Frame Should Not Contain ${text} ${loglvl}
当前Frame包含
[Arguments] ${text} ${loglevel}=INFO
Current Frame Contains ${text} ${loglevel}
截屏
[Arguments] @{filename}
Capture Page Screenshot @{filename}
打开浏览器
[Arguments] ${url} ${browser}=chrome @{args}
Open Browser ${url} ${browser} @{args}
打开菜单
[Arguments] ${locator}
Open Context Menu ${locator}
执行js
[Arguments] ${code}
Execute Javascript ${code}
执行异步js
[Arguments] ${code}
Execute Async Javascript ${code}
拖拽元素
[Arguments] ${source} ${target}
Drag And Drop ${source} ${target}
按下鼠标左键
[Arguments] ${locator}
Mouse Down ${locator}
按键
[Arguments] ${locator} ${key}
[Documentation] 按键 text_field q
...
... 按键 login_button 13 #ASCII Code for enter Key
Press Key ${locator} ${key}
提交表单
[Arguments] ${locator}=
Submit Form ${locator}
文本区值应该是
[Arguments] ${locator} ${expected} ${msg}=
Textarea Value Should Be ${locator} ${expected} ${msg}
文本区应该包含
[Arguments] ${locator} ${expected} ${msg}=
Textarea Should Contain ${locator} ${expected} ${msg}
文本框值应该是
[Arguments] ${locator} ${expected} ${msg}=
Textfield Value Should Be ${locator} ${expected} ${msg}
文本框应该包含
[Arguments] ${locator} ${expected} ${msg}=
Textfield Should Contain ${locator} ${expected} ${msg}
最大化
Maximize Browser Window
标题应该是
[Arguments] ${title}
Title Should Be ${title}
根据Index从List中取消选项
[Arguments] ${locator} @{indexes}
Unselect From List By Index ${locator} @{indexes}
根据Index从List选择
[Arguments] ${locator} @{indexes}
Select From List By Index ${locator} @{indexes}
根据Label从List中取消选项
[Arguments] ${locator} @{Labels}
Unselect From List By Label ${locator} @{Labels}
根据Label从List选择
[Arguments] ${locator} @{labels}
Select From List By Label ${locator} @{labels}
根据Value从List中取消选项
[Arguments] ${locator} @{values}
Unselect From List By Value ${locator} @{values}
根据Value从List选择
[Arguments] ${locator} @{values}
Select From List By Value ${locator} @{values}
根据值选择列表元素
[Arguments] ${locator} ${value}
Select From List By Value ${locator} ${value}
模拟
[Arguments] ${locator} ${event}
Simulate ${locator} ${event}
注册失败后运行的关键字
[Arguments] ${kwd}
Register Keyword To Run On Failure ${kwd}
添加cookie
[Arguments] ${name} ${value} ${path}= ${domain}= ${secure}= ${expiry}=
添加本地策略
[Arguments] ${strategy_name} ${strategy_keyword} ${persist}=False
Add Location Strategy ${strategy_name} ${strategy_keyword} ${persist}
清除元素文本
[Arguments] ${locator}
Clear Element Text ${locator}
点击元素
[Arguments] ${path}
Click Element ${path}
点击元素坐标
[Arguments] ${locator} ${xoffset} ${yoffset}
Click Element At Coordinates ${locator} ${xoffset} ${yoffset}
点击图片
[Arguments] ${locator}
Click Image ${locator}
点击按钮
[Arguments] ${path}
Click Button ${path}
点击链接
[Arguments] ${locator}
Click Link ${locator}
睡眠
[Arguments] ${time}
Sleep ${time}
确认
Confirm Action
窗口列表
@{list} List Windows
[Return] @{list}
等待元素不包含文本
[Arguments] ${locator} ${text} ${timeout}= ${error}=
Wait Until Element Does Not Contain ${locator} ${text} ${timeout} ${error}
等待元素不可见
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Element Is Not Visible ${locator} ${timeout} ${error}
等待元素包含文本
[Arguments] ${locator} ${text} ${timeout}= ${error}=
Wait Until Element Contains ${locator} ${text} ${timeout} ${error}
等待元素可用
[Arguments] ${locator} ${timeout}= ${error}=
Wait Until Element Is Enabled ${locator} ${timeout} ${error}
获取所有连接
@{links} Get All Links
[Return] @{links}
获取文本
[Arguments] ${path}
${text} get text ${path}
[Return] ${text}
获取标题
${title} get title
[Return] ${title}
获取水平坐标
[Arguments] ${locator}
@{position} Get Horizontal Position ${locator}
[Return] @{position}
获取焦点
[Arguments] ${locator}
Focus ${locator}
获取窗口名字
@{names} Get Window Names
[Return] @{names}
获取窗口坐标
${x} ${y}= Get Window Position
[Return] ${x} ${y}
获取窗口大小
${width} ${higth}= GET Window Size
[Return] ${width} ${higth}
获取窗口标识
@{windows} Get Window Identifiers
[Return] @{windows}
获取窗口标题
@{titles} Get Window Titles
[Return] @{titles}
获取表格内容
[Arguments] ${table_locator} ${row} ${column} ${loglevel}=INFO
${cell} Get Table Cell ${table_locator} ${row} ${column} ${loglevel}
[Return] ${cell}
获取警告信息
${msg} Get Alert Message
[Return] ${msg}
获取资源
${source} Get Source
[Return] ${source}
获取页面元素
[Arguments] ${locator}
@{elements} Get Webelements ${locator}
[Return] @{elements}
获取页面标题
${title} Get Title
[Return] ${title}
获取页面资源
[Arguments] ${loglevel}=INFO
${source} Log Source ${loglevel}
[Return] ${source}
表头应该包含
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
table header should contain ${table_locator} ${expected} ${loglevel}
表头应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
Table Header Should Contain ${table_locator} ${expected} ${loglevel}
表应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}=INFO
Table Should Contain ${table_locator} ${expected} ${loglevel}
表格Footer应该包含文本
[Arguments] ${table_locator} ${expected} ${loglevel}
Table Footer Should Contain ${table_locator} ${expected} ${loglevel}
表格列应该包含文本
[Arguments] ${table_locator} ${col} ${expected} ${loglevel}=INFO
Table Column Should Contain ${table_locator} ${col} ${expected} ${loglevel}