【问题标题】:Nested loop in RobotFrameworkRobotFramework 中的嵌套循环
【发布时间】:2017-09-18 13:12:03
【问题描述】:

我需要在 Robot 框架中创建一个嵌套循环。 你能帮我做吗?

${contents}=    Get File    ${file path}
 @{lines}=    Split to lines    ${contents}
 ${matched elements}=    Get Webelements    ${LABEL PORTAIL XPATH }
 : FOR    ${element}    IN    @{matched elements}
 \    ${text}=    Get Text    ${element}
 \    : FOR    ${line}    IN    @{lines}
 \    Run Keyword If    '${text}' == '${line}'    Log    '${text} matched'

我需要一个嵌套循环,将文件中的所有${text} 与所有@{lines} 进行比较。

提前致谢

【问题讨论】:

    标签: java selenium for-loop nested robotframework


    【解决方案1】:

    RF 中没有嵌套循环;这只能通过在外部循环中调用带有内部循环的关键字来完成。

    但在您的特定情况下,您可以不使用它 - 因为您想匹配整行,这可以通过应该包含:

    ${contents}=    Get File    ${file path}
    @{lines}=    Split to lines    ${contents}
    ${matched elements}=    Get Webelements    ${LABEL PORTAIL XPATH }
    : FOR    ${element}    IN    @{matched elements}
    \  ${text}=     Get Text    ${element}
    \  ${present}=  Run Keyword And Return Status    Should Contain    ${lines} 
    ${text}
    \    Run Keyword If  ${present}    Log    '${text} matched'
    

    如果您要进行部分匹配 - 即 ${text} 成为 ${lines} 成员的一部分,那么这是不可能的。

    【讨论】:

      【解决方案2】:

      没有包含内部循环的自定义关键字是不可能的。 见文档:http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#nested-for-loops

      我想说这样的逻辑应该总是使用一些更强大的语言(python、java...)来编写,然后从 RF 中调用。

      【讨论】:

        【解决方案3】:

        从 4.0 版本开始,Robot Framework(终于 :) 支持嵌套 for 循环。
        所以问题中的代码,使用新的FOR 语法将是:

        ${contents}=  Get File    ${file path}
        @{lines}=     Split to lines    ${contents}
        ${matched elements}=    Get Webelements    ${LABEL PORTAIL XPATH }
        FOR    ${element}    IN    @{matched elements}
            ${text}=    Get Text    ${element}
            FOR    ${line}    IN    @{lines}
                Run Keyword If    '${text}' == '${line}'    Log    '${text} matched'
            END
        END
        

        4.0版本还带来了IF/ELSE流控,所以可以让内循环在第一次匹配就断掉:

            FOR    ${line}    IN    @{lines}
                IF    '${text}' == '${line}'
                    Log    '${text} matched'
                    Exit For Loop
                END
            END
        

        ,与使用Run Keyword If 和`运行关键字的“旧方式”相比:

            FOR    ${line}    IN    @{lines}
                Run Keyword If    '${text}' == '${line}'    Run Keywords    Log    '${text} matched'
                                                               ...    AND   Exit For Loop
            END
        

        【讨论】:

        • 我想知道 Tidy 是否会将 Run Keyword If 调用重新格式化为新的 IF 语法。会很好。
        【解决方案4】:

        嵌套 for 循环

        直接不支持嵌套 for 循环,但可以在 for 循环中使用用户关键字并在其中有另一个 for 循环。

        *** Keywords ***
        Handle Table
            [Arguments]    @{table}
            :FOR    ${row}    IN    @{table}
            \    Handle Row    @{row}
        
        Handle Row
            [Arguments]    @{row}
            :FOR    ${cell}    IN    @{row}
            \    Handle Cell    ${cell}
        

        引用自:http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#nested-for-loops

        【讨论】:

          【解决方案5】:

          *** 设置 ***

          文档学习机器人框架

          *** 变量 ***

          *** 关键字 ***

          带有for循环的关键字

          [Arguments]     ${x}
          
          FOR     ${y}    IN RANGE    1   5
          
              Log To Console      ${x} ${y}
          
          END
          

          *** 测试用例 ***

          嵌套for循环示例

          FOR   ${x}   IN RANGE   1   5
          
              Keyword with for loop       ${x}
          
          END
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-01-27
            • 2015-06-05
            • 2015-11-22
            • 2022-01-21
            • 2011-01-20
            • 2017-05-22
            • 2020-07-28
            • 1970-01-01
            相关资源
            最近更新 更多