RobotFramework if else


1.在Run Keyword If語句中

如果有多個判斷語句,可以用小寫 and 或者是 or 連接,具體用 and 還是 or 根據自己程序的情況而定

如果判斷后要執行多個語句

則需要使用大寫 AND 配合 Run Keywords 使用

  2、不久又遇到一個問題,我if 條件后面需要接多個執行語句,還記得當時自己猜測亂寫:run keyword if +條件 log 1 log 2(結果肯定報錯啦),后面沒辦法想出了一個解決辦法是,把多個執行語句封裝成一個關鍵字:run keyword if +條件 +封裝的關鍵字。總感覺肯定還有其他解決辦法,就是查不出來,也沒問到。終於在一個群里問到解決方法:Run Keyword If    1==1    Run Keywords    log    1    AND    log    2  。可以看到用到了關鍵字Run KeywordsAND來處理。  

 3. https://www.cnblogs.com/labixinxinyexiangyouxiaobai/articles/12760440.html

 4.https://www.cnblogs.com/testlife007/p/5029101.html 

 5.   eg

    Run keyword if    ${2} == ${2}
    ...    Run Keywords    Log to console     hello world
    ...    AND    Keyword1     Pramas1    Params2
    ...    AND    Keyword2     Pramas1    Params2
    ...    AND    Keyword3     Pramas1    Params2
    ...    AND    Keyword3     Pramas1    Params2  

 

 6.

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
 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM