1. 程式人生 > >RobotFrameWork 關鍵字Set Variable If 使用方法記錄

RobotFrameWork 關鍵字Set Variable If 使用方法記錄

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