RobotFramework if else

转载博客:xiongmengdebigcrocodile

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
posted @ 2022-01-08 09:32  没醉  阅读(80)  评论(0编辑  收藏  举报