Run KeyWord If
使用關鍵字Run Keyword if 務必不要忘記在ELSE前單元格使用”…”
Runs the given keyword with the given arguments, if `condition` is true. The given `condition` is evaluated similarly as with `Should Be True` keyword, and `name` and `*args` have same semantics as with `Run Keyword`.
${status} | ${value}= | Run Keyword And Ignore Error | My Keyword |
Run Keyword If | ‘${status}’==’PASS’ | Some Action | arg |
Run Keyword Unless | ‘${status}’==’PASS’ | Another Action | My Keyword |
If `condition` is a string (e.g. ‘${rc} < 10’), it is evaluated as a Python expression using the built-in ‘eval’ function and the keyword status is decided based on the result. If a non-string item is given, the status is got directly from its truth value.
由上文可知condition會進行evaluated,類似於Should Be True這個關鍵字,其實本質上是調用python直接執行condition(注意這一點,后面我們在寫condition條件時可以靈活的使用python的一些條件表達式寫法):
1.單重條件判斷
a. 單一條件
${status} | Set Variable | 1 | |
Run Keyword If | ${status} <= 3 | log | “right” |
… | ELSE | log | “error” |

b. 多個條件表達式
Run Keyword If | ‘${color}’ == ‘Red’ or ‘${size}’ == ‘Small’ or ‘Design’ == ‘Simple’ | log | “right” |
2.多重條件判斷( …ELSE IF … ELSE)
${status} | Set Variable | 1 | ||
Run Keyword If | ${status} < = 3 | log | “right” | |
… | Else If | ${status} > 4 | log | “error” |
… | Else | log | “end” |
3.利用IF關鍵字給變量賦值
即在Run Keyword If關鍵字左側留參數接收返回值
${status} | Set Variable | 1 | ||
${result} | Run Keyword If | ${status} <= 3 | Set Variable | “right” |
… | Else | Set Variable | “error” |
另外也可以使用set variable if關鍵字為變量賦值
${status} | Set Variable | 1 | |
${result} | Set Variable If | ${status} <= 3 | “right” |
4.unless關鍵字
if關鍵字,當判斷條件為“true”時,后面的語句才會執行。而unless關鍵字與if相反,只有當判斷條件為“false”時,后面的語句才會執行
Run Keyword Unless | ‘${color}’ == ‘Red’ or ‘${size}’ == ‘Small’ or ‘Design’ == ‘Simple’ | log | “right” |
5. 結合Python語句使用
(由於上文提到python實質上直接執行condition中表達式,因此可以直接在condition中書寫python風格條件表達式)
${data} | Create Dictionary | 1 | use |
${roleId} | Set Variable | 1 | |
Run Keyword If | “${roleId}” in “${data}” | log | “right” |
… | ELSE | log | “error” |
Run KeyWord and Return Status
It runs the given keyword with given arguments and returns the status as a Boolean value. This keyword returns True if the keyword that is executed succeeds and False if it fails. This is useful, for example, in combination with Run Keyword If. If you are interested in the error message or return value, use Run Keyword And Ignore Error instead.
可以用於與Run Keyword If結合使用以獲取checkbox的值:

${status} | Run Keyword And Return Status | checkbox should be selected | id=checked_box | |
${pageCheckedBox} | Run Keyword If | ‘${status}’ == ‘True’ | Set Variable | True |
… | Else | Set Variable | False |