简体   繁体   English

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

[英]IF ELSE in robot framework with variables assignment

I need to execute some keywords conditionally in robot framework, but I dont know how to do it, it does not work.我需要在机器人框架中有条件地执行一些关键字,但我不知道该怎么做,它不起作用。 I tried many options, but I guess I have the "IF-ELSE" statement completely wrong..我尝试了很多选择,但我想我的“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}

It fails with: Variable '${particular_filter}' not found.它失败了: Variable '${particular_filter}' not found. But in case I run it it should not even go to ELSE branch because ${bool} is True... My custom function is filter opened just checks whether filter is already opened - if so, returns True.但是,如果我运行它,它甚至不应该转到 ELSE 分支,因为 ${bool} 是 True...我的自定义函数is filter opened只是检查过滤器是否已经打开 - 如果是,则返回 True。 My custom function uncheck all in filter just returns XPATH of "uncheck all" button.我的自定义函数uncheck all in filter只返回“uncheck all”按钮的 XPATH。 My custom function find particular filter returns XPATH of "filter dropdown" button.我的自定义函数find particular filter返回“过滤器下拉”按钮的 XPATH。 In this whole keyword I need to check whether the filter dropdown is already opened - if so, then I have to click directly on ${uncheck_all_button} , else if the filter dropdown is not opened yet, I need to first click on the filter itself ${particular_filter} and after that I can click on ${uncheck_all_button}在整个关键字中,我需要检查过滤器下拉列表是否已经打开 - 如果是,那么我必须直接点击${uncheck_all_button} ,否则如果过滤器下拉列表尚未打开,我需要先点击过滤器本身${particular_filter}然后我可以点击${uncheck_all_button}

I also tried the "run keyword" line to have like this:我也试过“运行关键字”行是这样的:

${uncheck_all_button}=    run keyword  if    "${bool}" == "True"    Set Variable    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}

or this:或这个:

run keyword  if    "${bool}" == "True"   ${uncheck_all_button}=    uncheck all in filter    ${AVAILABLE FILTERS}    ${FILTER}

I also tried it with ${bool} == "True" and ${bool} == True我也试过${bool} == "True"${bool} == True

But nothing really works, still the same error :(但是没有任何效果,仍然是同样的错误:(

Thank you so much for any help!非常感谢您的帮助!

Having IF/THEN/ELSE with multiple statements in each block does not work in Robot (or you would have to use "Run Keywords" I suppose, but that would become unreadable).在每个块中使用带有多个语句的 IF/THEN/ELSE 在 Robot 中不起作用(或者我想你必须使用“运行关键字”,但这会变得不可读)。 So I would refactor your code this way:所以我会这样重构你的代码:

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}   

Hope this helps.希望这可以帮助。

Based on the below syntax, update your code n check it.根据以下语法,更新您的代码并检查它。

Syntax for IF-ELSE: IF-ELSE 的语法:

   Run Keyword If    '${Condition}'== 'True'    Run Keywords    <Keyword 1>     <Keyword 2>
   ...    ELSE    <Keyword 1>

Syntax for "Set Variable" based on condition:基于条件的“设置变量”的语法:

 ${Status}=    Run Keyword If    '${Condition}'== 'True'    Set Variable    <Yes>    <No>

As per your code in IF part,根据您在 IF 部分中的代码,

if "bool=true", it will execute only the custom keyword "uncheck all in filter" but not the "Click element" keyword.如果“bool=true”,它只会执行自定义关键字“uncheck all in filter”而不是“Click element”关键字。 If you want both the keywords to be executed based on the condition, then use "Run Keywords" keyword as mentioned in IF-ELSE syntax.如果您希望根据条件执行这两个关键字,请使用 IF-ELSE 语法中提到的“运行关键字”关键字。

Thank you so much, Laurent, your solution is right!非常感谢您,劳伦特,您的解决方案是正确的! I just had to do some small changes to make it working:我只需要做一些小的更改即可使其正常工作:

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 ${FILTER}
...                ELSE  actions_when_checked ${FILTER}

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

actions_when_checked ${FILTER}
${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}
${error_status} Set Variable    False       
log ${error_status}         
Run Keyword If  ${error_status} calltrue    ELSE    login_sucessful

calltrue and login_sucessful are keywords it can be any keyword of your choice. so in this case login_sucessful  keyword is executed. if error_status is set to true then calltrue function will get executed.

Please be careful about the indentation a single space will mess up your execution flow请注意缩进单个空格会弄乱您的执行流程

From the version above 4, robot framework supporting assignment inside IF-Else refer: https://robocorp.com/docs/languages-and-frameworks/robot-framework/conditional-execution从以上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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM