RobotFramework学习02-基础关键字
2018-09-14 本文已影响129人
残阳夕露

本文包含内容:
- 基础关键字
- 基础关键字(高级用法)
- 内置关键字查询
关键字
关键字类似于python中的函数
- Go To
访问某个网址(前提是已经打开过浏览器)(可在初始化中打开浏览器)
Go To https://www.baidu.com
- Should Be Equal
- Should Be ...
- Should Be True
如果是字符串,需要加引号;数字不需要
# 示例
${str1}= set variable hello
Should Be True $str1 == 'hello'
Should Be True '${str1}' == 'hello'
${str1}= convert to interger 20
Should Be True ${str1} == 19
Should Be True $str1 == 19
-
convert to interger 转换为整数
-
convert to Number 转换为浮点数
-
log to console 直接打印(类似python中的print)
-
${33} 直接传数字
-
可以自己写一个xxx.py,自定义函数,然后在robot文件中在Settings中引用后
Library xxx.py
,此时可直接调用该函数(此时函数名就是关键字)
Library xxx.py 寻找路径同pyhon中选择模块的方法(path)
文档中import部分为导入文档时可使用的参数
# 例如导入SeleniumLibrary
Library SeleniumLibrary implicit_wait=10 # 修改等待时间
基础关键字
log
就是python中的"print"
-
log
关键字就是编程语言里的的print,可以打印任何想打印的内容
*** Test Cases ***
test case1
log robot framework
log python

log to console
将信息打印到控制台(不会存储到log文件中)
test case1
log to console robot framework
log to console python

定义变量
- 在Robot Framework中通过
setvariable
关键字定义变量
*** Test Cases ***
test case2
${var} set variable test python
log ${var}

定义及展示列表
- 关键字
Create List
可以用来定义列表 - 关键字
log many
打印列表元素
test case4
${hi} Create List hello world
log ${hi}
log many @{hi}

连接对象
-
Catenate
关键字可以链接多个对象
*** Test Cases ***
test case3
${hi} Catenate hello world
log ${hi}

-
Catenate
关键字加上SEPARATOR=
可以对多个连接的信息进行分割。
test case4
${hi} Catenate SEPARATOR=--- hello world
log ${hi}

时间操作
-
get time
关键字用来获取当前时间
test case4
${hi} get time
log to console ${hi}

-
sleep
关键字用来设置休眠一定时间
test case4
${t1} get time
sleep 5
${t2} get time

基础关键字(高级用法)
if语句
- 通过
run keyword if
关键字可以编写if分支语句
注1:
ELSE IF
和ELSE
前面的三个点(…)
注2:ELSE IF
和ELSE
必须大写
test case4
${hi} set variable 90
run keyword if ${hi}>=90 log to console 优秀
... ELSE IF ${hi}>=80 log to console 良好
... ELSE IF ${hi}>=60 log to console 及格
... ELSE log to console 不及格

for循环
在Robot Framework中编写循环通过
:FOR
。
循环内的语句必须以\
开头
- 循环0-9
test case4
:for ${i} in range 10
\ log to console ${i}

- 遍历列表
test case4
${abc} create list 1 2 3 a
:for ${i} in @{abc}
\ log to console ${i}

强大的Evaluate
通过
Evaluate
可以使用 Python 语言中所提供的方法。
- 生成随机数
test case4
${d} evaluate random.randint(999,9999) random
log to console ${d}

- 转化类型
test case4
${d} evaluate int(4)