学习Python要从语法基础开始学起,Robot Framework(往后简称:RF)也不例外。
初级部分
-
输出:
log
log就相当于Python里面的
print
*** Test Cases *** test_case1 # log就是print log robot framework log python
-
定义变量:
Set Variable
*** Test Cases *** test_case2 ${a} set variable python log ${a}
-
连接多个对象:
Catenate
*** Test Cases *** test_case3 ${hi} catenate Hello world log ${hi}
-
加上
SEPARATOR=
对多个连接的对象进行分隔*** Test Cases *** test_case4 ${hi} catenate SEPARATOR=--- HELLO WORLD log ${hi}
-
通过
@{}
定义列表,并通过log many
关键字进行打印*** Test Cases *** test_case5 @{abc} Create List a b c log many @{abc}
-
时间操作:
get time
获取当前系统时间*** Test Cases *** test_case6 ${t} get time log ${t}
-
设置休眠时间:
Sleep
*** Test Cases *** test_case7 ${t} get time sleep 5 ${t} get time
高级部分
-
分支结构:熟悉Python的读者一定了解
if
语句,在RF中,通过run keyword if
关键字来编写分支语句。(Tips:一定要注意ELSE IF前面的三个点要和RUN对齐,ELSE IF大写)*** Test Cases *** test_case8 ${a} set variable 59 run keyword if ${a}>=90 log 优秀 ... ELSE IF ${a}>=70 log 良好 ... ELSE IF ${a}>=60 log 及格 ... ELSE log 不及格
-
循环结构:通过
:FOR
编写循环结构,IN RANGE
用于指定循环范围*** Test Cases *** test_case9 :FOR ${i} IN RANGE 10 \ log ${i}
-
通过
:FOR
关键字来遍历列表*** Test Cases *** test_case10 @{abc} Create List a b c :for ${i} IN @{abc} \ log ${i}
-
在Python中,使用
random.randint(a, b)
函数生成随机整数,在RF中,通过Evaluate
关键字可以调用Python中所提供的方法*** Test Cases *** test_case11 ${d} evaluate random.randint(1000, 9999) random log ${d}
-
调用自己写的Python程序:
新建一个
add.py
文件在RF脚本同级目录下,并在Settings中引用该文件。-
RF脚本
*** Settings *** Library add.py *** Test Cases *** test_case12 ${a} evaluate int(4) ${b} evaluate int(9) ${plus} add ${a} ${b} log ${plus}
-
add.py
def add(a, b): return a + b
-
-
注释:
Comment
关键字用于注释*** Test Cases *** test_case13 Comment 这是注释 # 这也是注释
总结
以上就是RF系列第三章的内容,如果有运行错误或者看不明白的地方,请在评论区留言,最近在更新,所以会及时回复?!下一章内容主要是Selenium2Library。