Set Variable If
关键字普通使用方法:
The basic usage is giving a condition and two values. The given condition is first evaluated the same way as with the `Should Be True` keyword. If the condition is true, then the first value is returned, and otherwise the second value is returned. The second value can also be omitted, in which case it has a default value None. This usage is illustrated in the examples below, where ${rc} is assumed to be zero.
EX-1:
${butn} Set Variable 0
${StoreroomStatus} Set Variable If '${butn}'=='0' 0
log ${StoreroomStatus}
(变量${butn}=0,条件 '${butn}'=='0' 成立。所以${StoreroomStatus} 的值为0)
运行结果:

EX-2:
${butn} Set Variable 提交
${StoreroomStatus} Set Variable If '${butn}'=='0' 0 2
log ${StoreroomStatus}

(变量${butn}=提交,不等于0,条件 '${butn}'=='0' 不成立,所以变量${StoreroomStatus}的值为2)
运行结果:
Starting test: Web.产品管理.TT
20181210 09:52:51.841 : INFO : ${butn} = 提交
20181210 09:52:51.845 : INFO : ${StoreroomStatus} = 2
20181210 09:52:51.848 : INFO : 2
Ending test: Web.产品管理.TT

EX-3:
${butn} Set Variable 0
${StoreroomStatus} Set Variable If '${butn}'=='0' 0
${StoreroomStatus} Set Variable If '${butn}'=='1' 1
log ${StoreroomStatus}
(变量${butn}=0,条件1:${butn}==0成立,${StoreroomStatus}=0,条件${butn}=1不成立,${StoreroomStatus}=None)
运行结果:
Starting test: Web.产品管理.TT
20181210 10:06:25.904 : INFO : ${butn} = 0
20181210 10:06:25.908 : INFO : ${StoreroomStatus} = 0
20181210 10:06:25.910 : INFO : ${StoreroomStatus} = None
20181210 10:06:25.912 : INFO : None
Ending test: Web.产品管理.TT
升级用法:
It is also possible to have 'else if' support by replacing the second value with another condition, and having two new values after it. If the first condition is not true, the second is evaluated and one of the values after it is returned based on its truth value. This can be continued by adding more conditions without a limit.
EX-4:
${butn} Set Variable 0
${StoreroomStatus} Set Variable If '${butn}'=='0' 0
... '${butn}'=='1' 1
log ${StoreroomStatus}
(${butn}=0,条件${butn=0}成立,${StoreroomStatus}=0,退出循环,最终${StoreroomStatus}=0)
运行结果:
Starting test: Web.产品管理.TT
20181210 10:13:45.065 : INFO : ${butn} = 0
20181210 10:13:45.068 : INFO : ${StoreroomStatus} = 0
20181210 10:13:45.070 : INFO : 0
Ending test: Web.产品管理.TT
EX-5:
${butn} Set Variable 2
${StoreroomStatus} Set Variable If '${butn}'=='0' 0
... '${butn}'=='1' 1 2
log ${StoreroomStatus}
(${butn}=2,条件${butn}=0与条件${butn}=1均不成立,${StoreroomStatus}=2)
Starting test: Web.产品管理.TT
20181210 10:17:11.382 : INFO : ${butn} = 2
20181210 10:17:11.389 : INFO : ${StoreroomStatus} = 2
20181210 10:17:11.391 : INFO : 2
Ending test: Web.产品管理.TT

本文深入解析了SetVariableIf关键字的使用方法,包括基本用法和高级应用,通过多个实例展示了如何根据条件设置变量值,适用于自动化测试场景。
1501

被折叠的 条评论
为什么被折叠?



