【问题标题】:Is there a way to set variables inside an If condition in Robot Framework?有没有办法在 Robot Framework 的 If 条件中设置变量?
【发布时间】:2020-12-24 15:05:23
【问题描述】:

我试过这个来增加变量,但是没有用

Run Keyword If    '${state}'=='True'    Run Keywords    Click Element 
   xpath=//button[@overflow]  
...    AND Click Element    css=#stack  
...    ${i} Set Variable    1  
...    AND FOR ${item} IN ${items}  
            Log    ${item}
            Log    ${i}
            ${i}    Set Variable    ${i} + 1
           END

【问题讨论】:

  • 因为该代码块是关键字的一部分,而关键字调用另一个 no 会很奇怪?
  • 如果你想在 if 中运行一个 for 循环,你没有太多其他选择。我认为为子步骤设置更多关键字很好,特别是如果这使测试更具可读性。您可以在 Python 中将整个关键字实现为一个库,您可以在其中在单个函数(也称为关键字)中执行此类逻辑结构。

标签: testing automated-tests robotframework


【解决方案1】:

您应该删除Run Keywords 并将其替换为用户关键字,您可以在其中使用FOR 循环,并且可以增加变量,例如${i} Evaluate ${i}+1。 您也可以返回 ${i} 或其他任何内容,并在稍后的测试中使用它的值。

*** Test Cases ***
Test
    ${i}=    Run Keyword If    'True'=='True'    My logic
    Log    ${i}

*** Keyword ***
My Logic
    Log     xpath=//button[@overflow]  
    Log     css=#stack  
    ${i}    Set Variable    1
    FOR    ${item}    IN   item1    item2
        Log    ${item}
        Log    ${i}
        ${i}    Evaluate    ${i}+1
    END
    [Return]    ${i}

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2020-08-11
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多