代码之家  ›  专栏  ›  技术社区  ›  AutoTester213

机器人框架-如何使用不同关键字的全局变量

  •  -1
  • AutoTester213  · 技术社区  · 6 年前

    我有一个问题,我需要从不同的关键字访问一个变量

    我试过使用 set global variable

    ***Keywords***
    
    Random Name
        ${Name}=  Full Name
        set global variable  ${Name}
    
    Keyword Name
        Random Name
        Log  ${Name}
    
    Keyword Name2
        Random Name
        Log  ${Name}
    
    
    *** Test Cases ***
    Run Keywords
        Keyword Name
        Keyword Name2
    

    全名关键字

    Import names
    
    def Full_Name(self):
    
        return (names.get_first_name())
    

    输出

    enter image description here

    所以在关键字名称中,我得到的值是 John ,但在关键字Name2中,值为 Clair 约翰

    当我试图使用变量而不将关键字传递给关键字时,我无法访问所有变量。

    我也试着用 set suite variable 但结果是一样的。

    1 回复  |  直到 6 年前
        1
  •  0
  •   AutoTester213    6 年前

    我找到了解决办法。我在测试用例中没有调用Random Name关键字,而且我在variable部分创建了一个空变量

     *** Variables ***
    ${Name}
    
    ***Keywords***
    
    Random Name
        ${Name}=  Full Name
        set global variable  ${Name}
    
    Keyword Name
        Log  ${Name}
    
    Keyword Name2
        Log  ${Name}
    
    
    *** Test Cases ***
    Run Keywords
        Random Name
        Keyword Name
        Keyword Name2
    

        2
  •  0
  •   Riku Vartiainen    4 年前

    我知道你在两年前就已经开始工作了,但万一有人在这里绊倒了,我想提出一个替代方案:

    *** Keywords ***
    
    Setup: Run Keywords
        Set Test Variable    ${Name}    Full Name
    
    Keyword Name
        Log    ${Name}
    
    Keyword Name2
        Log    ${Name}
    
    
    *** Test Cases ***
    
    Run Keywords
        [Setup]    Setup: Run Keywords
        Keyword Name
        Keyword Name2