Search code examples
variablesrobotframework

Robot Framework - How to use Global Variable for different keywords


I have this problem where I need to make a variable accessible from different keywords

I have Tried to use set global variable Keyword.

***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

Full Name Keyword

Import names

def Full_Name(self):

    return (names.get_first_name())

Output

enter image description here

So in Keyword Name The value I am getting is John, but in Keyword Name2 The value is Clair. I need to keep the value as John for all of the keywords.

When i try to use the variable without passing the Keyword into the keyword I am not able to access it all.

I also Tried to use set suite variable but that has the Same Result.

Note: Full Name keyword is a custom library to get random names. also this is only a example code


Solution

  • I have found a solution. I wasn't calling the Random Name Keyword in my Test case, and also i created a empty variable in the Variable section

     *** 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
    

    Now I am getting the Desired output