0

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

11
  • This code won't run, it is full of errors. If it did run, it likely still wouldn't reproduce the problem since you call Random Name in every keyword so it stands to reason you would get a random name in every keyword (assuming Random Name actually sets ${name} to a random name). Commented Nov 8, 2018 at 11:54
  • This is a sample code (My code is too large to post it here), I am calling the Random Name keyword in every keyword because the global variable ${Name} is not found otherwise, thats my problem, i need to use the ${Name} without passing in the whole keyword. And Random Name does set ${Name} to a random name Commented Nov 8, 2018 at 12:02
  • @BryanOakley i have edited my answer Commented Nov 8, 2018 at 12:09
  • The code you posted still doesn't work. I get No keyword with name 'Full Name' found.. When I fix the obvious error, both of the other keywords log "Full Name" just as I would expect. Commented Nov 8, 2018 at 12:16
  • 1
    @WojtekT You should put into an answer and not as an edit to the question itself. :) Commented Nov 10, 2018 at 20:51

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

I know you already got it to work over two years ago but in case someone else stumbles here I'd like to propose an alternative:

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

Using setups allow you to use variables appropriate to their usage better. I pretty much never need to use Global Variables unless I have multiple test suites related to each other and they'd have such similar suite setups that I make an init.robot to set them all up using Global Variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.