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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41464174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:00:43  来源:igfitidea点击:

IF ELSE in robot framework with variables assignment

pythonif-statementautomated-testsrobotframework

提问by neliCZka

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.But in case I run it it should not even go to ELSE branch because ${bool} is True... My custom function is filter openedjust checks whether filter is already opened - if so, returns True. My custom function uncheck all in filterjust returns XPATH of "uncheck all" button. My custom function find particular filterreturns XPATH of "filter dropdown" button. 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}

它失败了: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}

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!

非常感谢您的帮助!

采纳答案by Laurent Bristiel

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). So I would refactor your code this way:

在每个块中使用带有多个语句的 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}   

Hope this helps.

希望这可以帮助。

回答by Rakesh

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. If you want both the keywords to be executed based on the condition, then use "Run Keywords" keyword as mentioned in IF-ELSE syntax.

如果“bool=true”,它只会执行自定义关键字“uncheck all in filter”而不是“Click element”关键字。如果您希望根据条件执行这两个关键字,请使用 IF-ELSE 语法中提到的“运行关键字”关键字。

回答by neliCZka

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}

回答by MD5

${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

请注意缩进单个空格会弄乱您的执行流程