入门Robot Framework(3)——条件判断

字符串比较


Edit

Text Edit:

1
2
3
4
5
6
7
8
9
For001
RunKeywordIf001
@{LIST} Set Variable a b c d
: FOR ${n} IN @{LIST}
\ RUN Keyword If '${n}' == 'a' log 1
\ ... ELSE IF '${n}' == 'b' log 2
\ ... ELSE IF '${n}' == 'c' log 3
\ ... ELSE log 4
Log Outside Loop

log:

1
2
3
4
5
6
7
8
Starting test: TestProject.ProcessControl.RunKeywordIf001
20170712 14:48:46.615 : INFO : @{LIST} = [ a | b | c | d ]
20170712 14:48:46.622 : INFO : 1
20170712 14:48:46.628 : INFO : 2
20170712 14:48:46.636 : INFO : 3
20170712 14:48:46.643 : INFO : 4
20170712 14:48:46.646 : INFO : Outside Loop
Ending test: TestProject.ProcessControl.RunKeywordIf001

注意:
1.变量${n}值为字符时,在判断条件中其需要加单引号或双引号
2.在判断条件中,与变量${n}对比的字符常量也需要加单引号或双引号

数字比较


Edit

Text Edit:

1
2
3
4
5
6
7
8
RunKeywordIf002
@{LIST} Set Variable 1 2 3 4
: FOR ${n} IN @{LIST}
\ RUN Keyword If ${n} == 1 log a
\ ... ELSE IF 1 < ${n} < 3 log b
\ ... ELSE IF ${n} == 3 log c
\ ... ELSE log d
Log Outside Loop

log:

1
2
3
4
5
6
7
8
Starting test: TestProject.ProcessControl.RunKeywordIf002
20170712 15:00:39.068 : INFO : @{LIST} = [ 1 | 2 | 3 | 4 ]
20170712 15:00:39.074 : INFO : a
20170712 15:00:39.077 : INFO : b
20170712 15:00:39.083 : INFO : c
20170712 15:00:39.088 : INFO : d
20170712 15:00:39.091 : INFO : Outside Loop
Ending test: TestProject.ProcessControl.RunKeywordIf002

布尔类型比较


Edit

Text Edit:

1
2
3
4
5
6
7
RunKeywordIf003
@{LIST} Set Variable ${true} ${false}
: FOR ${n} IN @{LIST}
\ RUN Keyword If '${n}' == '${true}' log 1
\ ... ELSE IF '${n}' == '${false}' log 2
\ ... ELSE log 3
Log Outside Loop

log:

1
2
3
4
5
6
Starting test: TestProject.ProcessControl.RunKeywordIf003
20170712 15:02:55.359 : INFO : @{LIST} = [ True | False ]
20170712 15:02:55.362 : INFO : 1
20170712 15:02:55.365 : INFO : 2
20170712 15:02:55.368 : INFO : Outside Loop
Ending test: TestProject.ProcessControl.RunKeywordIf003

判断是否为列表中的元素


Edit

Text Edit:

1
2
3
4
5
6
7
RunKeywordIf004
@{LIST} Create List jjjds haha bigfire
${string} Set Variable bigfire
: FOR ${n} IN @{LIST}
\ RUN Keyword If '${n}' == '${string}' log 1
\ ... ELSE log 0
Log Outside Loop

log:

1
2
3
4
5
6
7
8
Starting test: TestProject.ProcessControl.RunKeywordIf004
20170712 15:04:47.901 : INFO : @{LIST} = [ jjjds | haha | bigfire ]
20170712 15:04:47.903 : INFO : ${string} = bigfire
20170712 15:04:47.906 : INFO : 0
20170712 15:04:47.908 : INFO : 0
20170712 15:04:47.911 : INFO : 1
20170712 15:04:47.914 : INFO : Outside Loop
Ending test: TestProject.ProcessControl.RunKeywordIf004

两个列表相比较


Edit

Text Edit:

1
2
3
4
5
RunKeywordIf005
@{LIST1} Create List jjjds haha bigfire
@{LIST2} Create List jjjds haha bigfire
Run Keyword If @{LIST1} == @{LIST2} log 1
... ELSE log 0

log:

1
2
3
4
5
Starting test: TestProject.ProcessControl.RunKeywordIf005
20170712 15:06:55.514 : INFO : @{LIST1} = [ jjjds | haha | bigfire ]
20170712 15:06:55.516 : INFO : @{LIST2} = [ jjjds | haha | bigfire ]
20170712 15:06:55.518 : INFO : 1
Ending test: TestProject.ProcessControl.RunKeywordIf005

两个列表中的元素相比较


Edit

Text Edit:

1
2
3
4
5
6
7
8
9
RunKeywordIf006
@{LIST1} Create List jjjds haha bigfire
@{LIST2} Create List jjjds haha bigfire222
Run Keyword If '@{LIST1}[0]' == '@{LIST2}[0]' log 1
... ELSE log 0
Run Keyword If '@{LIST1}[1]' == '@{LIST2}[1]' log 1
... ELSE log 0
Run Keyword If '@{LIST1}[2]' == '@{LIST2}[2]' log 1
... ELSE log 0

log:

1
2
3
4
5
6
7
Starting test: TestProject.ProcessControl.RunKeywordIf006
20170712 15:09:15.164 : INFO : @{LIST1} = [ jjjds | haha | bigfire ]
20170712 15:09:15.166 : INFO : @{LIST2} = [ jjjds | haha | bigfire222 ]
20170712 15:09:15.169 : INFO : 1
20170712 15:09:15.171 : INFO : 1
20170712 15:09:15.174 : INFO : 0
Ending test: TestProject.ProcessControl.RunKeywordIf006