Robotframework

Rf中循环用法-FOR Loops。

基本语法

RF在某个版本之后,FOR循环的语法是以FOR开头,END结尾。
IN后面的序列可以是两种形式。普通变量或变量组,list变量。
普通变量或是变量组的形式有:

  1. 变量。带{}的变量
  2. 字符串的值(不带{})或数字${4}或4。
  3. 字符串,变量或数字混合。
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有三种形式。

  1. 只指定end值
  2. 指定start和end值
  3. 指定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语句语法体内。

  1. Run KeyWord If … Exit For Loop
  2. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值