如何从Robot Framework中的for循环退出

时间:2019-06-05 13:55:39

标签: robotframework

我从列表中选择一个随机值以对其执行一些操作,例如“运行关键字是否”,“退出循环”,“单击”元素,等到页面包含所有内容。现在,在满足条件后,我无法退出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.

1 个答案:

答案 0 :(得分:2)

解决方案1:

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

您可以尝试上述语法吗?这是最新的循环语法,对我有用,对你也应该有用。 使用最新版本的SeleniumLibrary。

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

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