Robot Framework IF语句详解,解决IF Condition使用python语句的问题 (Run Keyword IF & Run Keyword and Return Status)


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