编辑推荐: |
本文来自于testerhome.com,主要介绍了F5按键功能,常用关键字,执行本地程序,导入库,Screenshot
库,Collections 库等。 |
|
F5按键功能

search term: 用于搜索关键字。
source:用于选择相关库,默认在所有库下搜索关键字。
创建分上下两部分,上部分显关键字列表,下半部分显示某一关键字的详细说明。
常用关键字
log
关键字就是编程语言里的“print”一样,可以打印任何你想打印的内容


通过运行结果中看到,“ INFO:”的“hello world” 就是通过 log 关键字打印的信息。
Set variable
在 robot Framework 中通过“Set variable”关键字来定义变量


Catenate
Catenate连接多个信息


SEPARATOR
“SEPARATOR=”可以对多个连接的信息进行分割。


Create List
通过“Create List”关键字可以定义列表。


每个字符串前面加 u,是为了统一编码问题,将字符串转为 Unicode 编码。


如果通过“@{}”去定义列表的话,可以通过“log many”关键字进行打印
get time
在 Robot Framework 中也提供操作时间的关键字。


Robot Framework 中提供了“get time”关键字用来获取当前时间。


“sleep”关键字用来设置休眠一定时间。 sleep 关键字默认以“秒”为单位。
if 语句
函数结构范例:


首先定义两个变量, a为 59 。
If 判断 a 大于等于 90 ,满足条件 log 输出 “优秀 ”;
不满足上面的条件,接着 else if 判断 a 大于等于 70 ,满足条件 log 输出 “良好”;
不满足上面的条件,接着 else if 判断 a 大于等于 60 ,满足条件 log 输出 “及格”;
上面的条件都不满足,else log 输出“不及格”。

for 循环
在 Robot Framework 中编写循环通过“:for”


通过“:for”定义 for 循环;in range 用于指定循环的范围。
注意,in range 定义为 10,它的范围是 0~9。
遍历列表
“create list” 关键字用来定义列表(a,b,c),“@{}”用来存放列表。


通过过“:for”循环来来遍历@{abc}列表中的字符。
Evaluate
Python:

random 模块的 randint()方法用于获取当前时间
在 Robot Framework 中使用“Evauate”也可以调用 Python 所提供的 random
模块下的 randint()方法。


执行本地程序
通过调用Python所提供的 os 模块中的 system()方法可以执行本地 Python 文件。
至于在.py文件中Python 可以做任何想做的事。

对于 system()方法来说,它也不单单可执行 Python 文件,任何在 cmd 命令提示符下可运行文件和命令,它都可以执行。
不过,一般情况下不建议通过 system()方法去执行外部程序。这样做其实就脱离了 Robot Framework,也就是说和Robot
Framework没啥关系了。因此,我们尽量把要做的事情通过 Python 封装成关键字给RobotFramework
使用。
导入库
在 Python 语言中可以使用 import 导入标准和第三方模块或框架。那么在 Robot Framework
中也提供了“Import Library”关键字来完成这个事情。

我们导入了 Python 标准的单元测试框架unittest。当然,在 Robot Framework
中并不能使用 unittest单元测试框架。这里只演示“Import Library”等同于 Python
语言中的 import。

注释
“Comment”关键字用于设置脚本中的注释。
除此之外,你也可以像 Python一样使用“#”号进行注释。

Screenshot 库
Scrennshot 同样为 Robot Framework 标准类库,我们只将它提供的其它中一个关键字“Take
Screenshot”,它用于截取到当前窗口。
虽然 Scrennshot 也为 Robot Framework 标准类库,但它默认不会加载,需要手动加载这个库。


“Screenshot”库有3个关键字:Set Screenshot Directory、Take
Screenshot、Take Screenshot Without Embedding。
第一个关键字Set Screenshot Directory是设置截屏图片保存的路径,后两个关键字Take
Screenshot、Take Screenshot Without Embedding是用来截屏并保存的。
两者区别是:
Take Screenshot:截屏后,图片将显示在报告中
Take Screenshot Without Embedding:截屏后,报告中只有图片的链接
使用“Take Screenshot”关键字实现截取当前屏幕:



Collections 库
Collections 库同样为 Robot Framework 标准类库,它所提供的关键字主要用于列表、索引、字典的
处理。
在使用之前需要在测试套件(项目)中添加:


创建字典
字典也是一种常见的存放数据的形式,Create Dictionary 关键字用于创建关键字。

字典的存放方式是 key:value 形式存放的
操作字典
Get Dictionary Items 关键字用于读取字典的 key 和 value


Get Dictionary Items:获取字典中的 key 和 value
Get Dictionary Keys:获取字典中的 key
Get Dictionary Values:获取字典中的 value
Get From Dictionary:获取字典中的 key 对应的 value

|