IF ELSE在具有变量赋值的机器人框架中

nel*_*Zka 14 python automated-tests if-statement robotframework

我需要在机器人框架中有条件地执行一些关键字,但我不知道该怎么做,它不起作用.我尝试了很多选项,但我想我的"IF-ELSE"声明完全错了..

Choose Particular Filter ${FILTER} And Uncheck All Values
    ${bool}=   is filter opened   ${AVAILABLE FILTERS}   ${FILTER}
    ${uncheck_all_button}=    run keyword  if    "${bool}" == "True"   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    ...                       click element   ${uncheck_all_button}
    ...                       ELSE
    ...                       Set variable    ${particular_filter}:    find particular filter   ${AVAILABLE FILTERS}  ${FILTER}
    ...                       click element   ${particular_filter}
    ...                       Set variable    ${uncheck_all_button}:   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    ...                       click element   ${uncheck_all_button}
Run Code Online (Sandbox Code Playgroud)

它失败了:Variable '${particular_filter}' not found. 但是如果我运行它,它甚至不应该转到ELSE分支,因为$ {bool}是True ...我的自定义函数is filter opened只检查过滤器是否已经打开 - 如果是,则返回True.我的自定义函数uncheck all in filter只返回"取消全部选中"按钮的XPATH.我的自定义函数find particular filter返回"过滤器下拉"按钮的XPATH.在这整个关键字中我需要检查过滤器下拉列表是否已经打开 - 如果是这样,那么我必须直接点击${uncheck_all_button},否则如果过滤器下拉列表尚未打开,我需要先点击过滤器本身${particular_filter}然后我可以点击${uncheck_all_button}

我也尝试过"run keyword"这一行:

${uncheck_all_button}=    run keyword  if    "${bool}" == "True"    Set Variable    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}
Run Code Online (Sandbox Code Playgroud)

或这个:

run keyword  if    "${bool}" == "True"   ${uncheck_all_button}=    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}
Run Code Online (Sandbox Code Playgroud)

我也尝试过${bool} == "True"${bool} == True

但没有什么真正有效,仍然是同样的错误:(

非常感谢你的帮助!

Lau*_*iel 10

在每个块中使用多个语句的IF/THEN/ELSE在Robot中不起作用(或者你必须使用"运行关键字",但这将变得不可读).所以我会这样重构你的代码:

Choose Particular Filter ${FILTER} And Uncheck All Values
    ${is_filter_opened}=   is filter opened   ${AVAILABLE FILTERS}   ${FILTER}
    run keyword  if    ${is_filter_opened}    actions_when_unchecked
    ...                ELSE  actions_when_checked

actions_when_unchecked
    uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    click element   ${uncheck_all_button}

actions_when_checked    
    ${particular_filter}:    find particular filter   ${AVAILABLE FILTERS}  ${FILTER}
    click element   ${particular_filter}
    ${uncheck_all_button}:   uncheck all in filter  ${AVAILABLE FILTERS}   ${FILTER}
    click element   ${uncheck_all_button}   
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Rak*_*esh 9

根据以下语法,更新您的代码并检查它.

IF-ELSE的语法:

   Run Keyword If    '${Condition}'== 'True'    Run Keywords    <Keyword 1>     <Keyword 2>
   ...    ELSE    <Keyword 1>
Run Code Online (Sandbox Code Playgroud)

基于条件的"设置变量"的语法:

 ${Status}=    Run Keyword If    '${Condition}'== 'True'    Set Variable    <Yes>    <No>
Run Code Online (Sandbox Code Playgroud)

根据你在IF部分的代码,

如果"bool = true",它将只执行自定义关键字"取消选中所有过滤器"而不执行"点击元素"关键字.如果您希望根据条件执行这两个关键字,请使用IF-ELSE语法中提到的"运行关键字"关键字.


小智 5

从以上版本4开始,机器人框架支持IF-Else内的赋值,请参考:https://robocorp.com/docs/languages-and-frameworks/robot-framework/conditional-execution

IF    ${random} == ${NUMBER_TO_PASS_ON}
    ${var}=    set variable    10
ELSE IF    ${random} > ${NUMBER_TO_PASS_ON}
    ${var}=    set variable  ${random}
ELSE
    ${var}=    set variable  ${NUMBER_TO_PASS_ON}
END
Run Code Online (Sandbox Code Playgroud)