如何在 Robot Framework 中退出 for 循环

Posted

技术标签:

【中文标题】如何在 Robot Framework 中退出 for 循环【英文标题】:How to exit from for loop in Robot Framework 【发布时间】:2019-10-21 01:15:21 【问题描述】:

我从列表中选择一个随机值来对其执行一些操作,例如 Run Keyword If、Exit For Loop If、单击元素、等到页面包含所有内容。现在,在满足条件后我无法退出 for 循环,但发生的情况是从为值 Batman 设置的开始条件开始。

已经尝试过 :break & Exit For Loop 关键字,但对我没有用

@Hero  Batman  Superman  Ironman

View All Details of Superhero
 $value =  Evaluate  random.choice($Hero)  random
    input text  $SEARCH_BAR  $value
    log to console  \nvalue: $value
    click element  $SEARCH_BUTTON

    Run Keyword If  '$value' == 'Batman'  click element  $P1
    ...  ELSE IF  '$value' == 'Superman'  click element  $P2
    ...  ELSE IF  '$value' == 'Ironman'  click element  $P3
    ...  ELSE  log to console  condition didn't met

    :For  $value  IN  @Hero
        \  Log    $value
        \  Run Keyword If  '$value' == 'Batman'  sleep  5s
        \  EXIT FOR LOOP IF  '$value' == 'Batman'
        \  click element  //*[@href='#external']
        \  click element  $BASIC_INFO
        \  wait until page contains  Summary
        \  click element  $RELATIONSHIP
        @expected_relationship_result  create list  Catwoman  Robin
        list should contain value  @expected_relationship_result
        \  Log  condition didn't met

        \  Run Keyword If  '$value' == 'Superman'  click element
        \  click element  //*[@href='#external']
        \  click element  $BASIC_INFO
        \  wait until page contains  Summary
        \  click element  $RELATIONSHIP
        @expected_relationship_result  create list  Wonderwomen  Lois Lane
        list should contain value  @expected_relationship_result  
        \  Log  condition didn't met

        \  Run Keyword If  '$value' == 'Ironman'  sleep  5s  
        \  EXIT FOR LOOP IF  '$value' == 'Ironman'
        \  click element  //*[@href='#external']  
        \  click element  $BASIC_INFO
        \  wait until page contains  Summary
        \  click element  $RELATIONSHIP
        @expected_relationship_result  create list  Wonderwomen  Lois Lane
        list should contain value  @expected_relationship_result
        \  Log  condition didn't met


I'm not able to figure out when the condition is already been met then why Exit For Loop If is not working.

【问题讨论】:

'\' 从机器人 3.2 开始折旧 你能添加控制台输出正在发生的事情吗? 【参考方案1】:

解决方案 1:

FOR    $value    IN    @Hero
    Do your stuff
    Exit For Loop IF    "$value" == "$Batman"
    Do your stuff
END

你能试试上面的语法吗?这是最新的 for 循环语法。它为我工作,也应该为你工作。 使用 SeleniumLibrary 最新版本。

解决方案 2:旧语法(这也可以)

: FOR    $value    IN    @Hero
\    Do your stuff
\    Exit For Loop IF    "$value" == "$Batman"
\    Do your stuff

【讨论】:

以上是关于如何在 Robot Framework 中退出 for 循环的主要内容,如果未能解决你的问题,请参考以下文章

Robot Framework 如何获取文本项

如何在 Robot Framework 中运行无头测试

如何在 Robot Framework 中比较两个图像

如何在 Robot Framework 中编写 if 语句的多个条件

Robot Framework:如何动态构建拆解

robot framework ride 在添加Resource时报错,应如何解决