条件判断:
RF中中Run Keyword If关键字做条件判断
条件表达式参数给python的eval函数:
run keyword if '2020' in $html log to console 内容
#python条件表达式如果要引用RF变量,直接在变量名前面加$符号即可,不用加{}
参数在下一行的语法:
...加两个以上空格
run keyword if '2020' in $html and 'UTC' in $html
... Log to console \n2020年的,UTC时间
ELSE分支
ELSE:
*** Settings ***
Library mylib4
*** Test Cases ***
判断-if-else
${resp} getwebinfo #getwebinfo是自定义的一个关键字
#ELSE注意要大写
run keyword if '2020' in $resp log to console 今年是2020
... ELSE log to console 不是2020
判断-esle-if
${resp} getwebinfo
#ELSE-IF注意要大写
run keyword if '2020' in $resp log to console 今年是2020
... ELSE IF 'utc' in $resp log to console 当前是UTC时间
... ELSE IF 'Sep' in $resp log to console 现在是9月份
... ELSE log to console 不知道什么时间
#RF里面的判断,每个条件之后只能跟一个关键字
判断注意点
run keyword if 1==1
... log to console 语句1
mylib4.py
import requests
from robot.api.logger import console
def getWebInfo():
response = requests.get("http://mirrors.163.com/centos/timestamp.txt")
return response
def check_score(score):
if int(score)>=60:
print("恭喜你及格了")
console("恭喜你及格了")
else:
print("再回去复习吧")
console("再回去复习吧")
def get_webdriver():
from selenium import webdriver
driver = webdriver.Chrome()
return driver
循环里的判断:
*** Settings ***
Library mylib4
Library Dialogs #RF标准库,用于用户交互
*** Test Cases ***
循环终止1-break
#用for循环来批量获取--如果不知道循环多少次?while
#用FOR循环模拟while循环,只要模拟出足够次数即可
FOR ${i} IN RANGE 9999
#获取用户输入,类似python的input
${score} Get Value From User 请输入你的分数
#判断用户输入如果是exit就退出
run keyword if 'exit'==$score exit for loop
check score ${score}
END
循环终止2-continue
#continue--结束本次循环,进入下一轮循环
FOR ${i} IN RANGE 9999
#获取用户输入,类似python的input
${score} Get Value From User 请输入你的分数
#判断用户输入如果是continue就中止本次循环
#run keyword if 'continue'==$score continue for loop
continue for loop if continue'==$score
#run keyword if 'exit'==$score exit for loop
exit for loop if 'exit'==$score
check score ${score}
END
run keyword if 'continue'==$score continue for loop等价于 continue for loop if continue'==$score
run keyword if 'exit'==$score exit for loop等价于 exit for loop if 'exit'==$score
RF里面循环判断的语法相对于其他编程语言比骄傲弱
RF的for循环不能嵌套
所以如果以后要实现一些复杂逻辑,用python实现,然后RF调用
Evaluate:
直接用python代码表达式来生成一个结果
${var} set variable ${890}
完全可以写成
${var} evaluate 890
复杂的表达式:
${var} evaluate ['hello']*10
*** Settings ***
Library mylib4
*** Test Cases ***
case1
${res} evaluate [i for i in range(100)]
log to console ${res}
${driver} get webdriver
evaluate $driver.get("http://baidu.com")
不建议用例里面全部都是evaluate