我需要在机器人框架中有条件地执行一些关键字,但我不知道该怎么做,它不起作用。我尝试了很多选项,但我想我有#34; 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}
失败了:Variable '${particular_filter}' not found.
但是如果我运行它,它甚至不应该去ELSE分支,因为$ {bool}是真的...
我的自定义函数is filter opened
只检查过滤器是否已经打开 - 如果是,则返回True。
我的自定义函数uncheck all in filter
只返回&#34的XPATH;取消选中所有"按钮。
我的自定义函数find particular filter
返回XPATH"过滤器下拉列表"按钮。
在整个关键字中,我需要检查过滤器下拉列表是否已经打开 - 如果是,那么我必须直接点击${uncheck_all_button}
,否则如果过滤器下拉列表尚未打开,我需要先点击过滤器本身${particular_filter}
然后我可以点击${uncheck_all_button}
我还尝试了"运行关键字"行有这样的:
${uncheck_all_button}= run keyword if "${bool}" == "True" Set Variable uncheck all in filter ${AVAILABLE FILTERS} ${FILTER}
或者这个:
run keyword if "${bool}" == "True" ${uncheck_all_button}= uncheck all in filter ${AVAILABLE FILTERS} ${FILTER}
我还尝试使用${bool} == "True"
和${bool} == True
但是没有什么真的有用,仍然是同样的错误:(
非常感谢您的帮助!
答案 0 :(得分: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}
希望这有帮助。
答案 1 :(得分:9)
根据以下语法,更新您的代码并检查它。
IF-ELSE的语法:
Run Keyword If '${Condition}'== 'True' Run Keywords <Keyword 1> <Keyword 2>
... ELSE <Keyword 1>
&#34;语法设置变量&#34;根据条件:
${Status}= Run Keyword If '${Condition}'== 'True' Set Variable <Yes> <No>
根据您在IF部分的代码,
如果&#34; bool = true&#34;,它将只执行自定义关键字&#34;取消选中过滤器&#34;但不是&#34; Click元素&#34;关键词。如果您希望根据条件执行这两个关键字,请使用&#34;运行关键字&#34; IF-ELSE语法中提到的关键字。
答案 2 :(得分:1)
非常感谢Laurent,你的解决方案是对的! 我只需做一些小改动就可以了:
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}
答案 3 :(得分:0)
${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.
请注意一个空格会弄乱执行流程的缩进
答案 4 :(得分:0)
从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