You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Robot Framework:首个关键字失败后仍执行后续关键字的问题

问题

我在测试用例中编写了以下两个关键字:

Wait Until Page Contains Element             'Error element'    timeout= 200
Wait Until Page Does Not Contain Element     'Error element'    timeout=100

我希望即使第一个关键字因错误元素未出现而失败,测试仍能继续执行。我尝试了以下两种方案但均无效:

方案1

Run Keyword And Ignore Error                 Wait Until Page Contains Element
...                                          'Error element'    timeout= 200       
Wait Until Page Does Not Contain Element     'Error element'    timeout=100

方案2

${Ignore_Error}     Wait Until Page Contains Element     'Error element'    timeout= 200
Run Keyword And Ignore Error                             ${Ignore_Error}
Wait Until Page Does Not Contain Element                 'Error element'   timeout=100

我还尝试了Run Keyword And Continue On Failure,同样无效。请问有什么解决建议吗?

解决建议

你的问题出在参数传递语法错误或者关键字调用方式不对,以下是正确的实现方式:

方法1:正确使用Run Keyword And Ignore Error

Robot Framework中,用Run Keyword And Ignore Error包裹关键字时,所有参数需和被调用关键字放在同一逻辑行(用...续行时要保证缩进正确),正确写法如下:

Run Keyword And Ignore Error    Wait Until Page Contains Element    'Error element'    timeout=200
Wait Until Page Does Not Contain Element    'Error element'    timeout=100

或者用续行符的规范写法(注意...后的参数要与前面关键字的参数对齐):

Run Keyword And Ignore Error    Wait Until Page Contains Element
...                            'Error element'
...                            timeout=200
Wait Until Page Does Not Contain Element    'Error element'    timeout=100

方法2:捕获关键字执行状态

如果需要判断第一个关键字的执行结果,可以接收Run Keyword And Ignore Error返回的状态和信息,后续逻辑不受影响:

${status}    ${output}    Run Keyword And Ignore Error    Wait Until Page Contains Element    'Error element'    timeout=200
# 可选:根据状态做额外处理
# IF    ${status} == FAIL
#     Log    错误元素未出现,继续执行后续步骤
# END
Wait Until Page Does Not Contain Element    'Error element'    timeout=100

原方案无效原因

  • 方案1:续行的参数缩进错误,Robot Framework无法识别为被包裹关键字的参数,导致Wait Until Page Contains Element未接收到必要参数,执行失败后中断了后续逻辑。
  • 方案2:你先执行了Wait Until Page Contains Element,失败时已经触发测试失败,再用Run Keyword And Ignore Error调用执行后的结果变量,根本起不到忽略错误的作用——忽略错误必须包裹关键字的执行过程,而非执行后的结果。

内容的提问来源于stack exchange,提问作者vinay rana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 12:21:12