Rf中循环用法-FOR Loops。
基本语法
RF在某个版本之后,FOR循环的语法是以FOR开头,END结尾。
IN后面的序列可以是两种形式。普通变量或变量组,list变量。
普通变量或是变量组的形式有:
- 变量。带{}的变量
- 字符串的值(不带{})或数字${4}或4。
- 字符串,变量或数字混合。
FOR ${animal} IN cat dog
Log ${animal}
Log 2nd keyword
END
FOR ${var} IN one two ${3} four ${five}
... kuusi 7 eight nine ${last}
Log ${var}
END
IN后面跟List变量的语法:
***Variables***
@{ELEMENTS} 1 2 3 4
***Test Cases***
List for loops
FOR ${element} IN @{ELEMENTS}
Log ${element}
END
FOR IN RANGE用法
IN RANGE有三种形式。
- 只指定end值
- 指定start和end值
- 指定start,end,步长值。步长值是正数,代表加法;负数代表减法。
FOR ${var} IN RANGE ${5}
Log ${var}
END
Only upper limit
[Documentation] Loops over values from 0 to 9
FOR ${index} IN RANGE 10
Log ${index}
END
Start and end
[Documentation] Loops over values from 1 to 10
FOR ${index} IN RANGE 1 11
Log ${index}
END
Also step given
[Documentation] Loops over values 5, 15, and 25
FOR ${index} IN RANGE 5 26 10
Log ${index}
END
Negative step
[Documentation] Loops over values 13, 3, and -7
FOR ${index} IN RANGE 13 -13 -10
Log ${index}
END
LOOP之break功能
循环过程中如果想要提前终止循环,类似Python中的break功能,可以用两种形式。而且退出语句是在Loop语句语法体内。
- Run KeyWord If … Exit For Loop
- Exit For Loop If …
Exit for loop1
FOR ${var} IN 1 2 3 4 5
Log To Console ${var}
Run Keyword If '${var}' == '4' Exit For Loop
END
Exit for loop2
FOR ${var} IN RANGE 5
Log To Console ${var}
Exit For Loop If '${var}' == '4'
END
结果为:
1
2
3
4
LOOP之continue功能
Run Keyword If … Continue For Loop和Continue For Loop If …用于终止本次循环接下来的语句,继续开始下次循环。
Continue for loop1
FOR ${var} IN 1 2 3 4 5
Log ${var}
Run Keyword If '${var}' == '3' Continue For Loop
Log Hello
END
Continue for loop2
FOR ${var} IN RANGE 5
Log ${var}
Continue For Loop If '${var}' == '3'
Log Hello
END
结果是:
1
hello
2
hello
3
4
hello
5
hello