1

I'm editing a variable in a custom keyword that I run conditionally when a condition is met. At first I thought the arguments were not being returned so I created a global keyword and made modifications inside this part of the code, and when returning to the main and logging that variable, it still has the value I declared for that global variable. Is there a bug that prevents modifying variables when running Run Keyword If?

Log

....
FOR  ${i}  IN RANGE  -1  -4  -1
     ${temp_names_cell} =  Get table cell  ${table_locator}  ${i}  1
     Loop columns  ${i}  ${temp_values_cell}  ${column_count}  ${table_locator}
     log  ${WORKAROUND}
     Set to dictionary  ${test}  ${temp_names_cell}  ${WORKAROUND}  #${temp_values_cell}
log dictionary  ${test}
END

Loop columns
    [Arguments]  ${i}  ${temp_values_cell}  ${column_count}  ${table_locator}
    ${column_name} =  Set variable
    FOR  ${j}  IN RANGE  1  ${column_count}+1  1
        ${column_name} =  get table cell  ${table_locator}  1  ${j}
        ${column_name} =  Replace string  ${column_name}  \n  ${SPACE}
        Run keyword if  "${column_name}" == "Short Term Backlog"   Get cell value  ${temp_values_cell}  ${table_locator}  ${i}  ${j}
        log  ${temp_values_cell}
    END
    [Return]  ${i}  ${temp_values_cell}  ${column_count}  ${table_locator}

Get cell value
    [Arguments]  ${temp_values_cell}  ${table_locator}  ${i}  ${j}
    ${temp_values_cell} =  Get table cell  ${table_locator}  ${i}  ${j}
    ${WORKAROUND} =  Get table cell  ${table_locator}  ${i}  ${j}
    [Return]  ${temp_values_cell}  ${table_locator}  ${i}  ${j}

1 Answer 1

1

You are describing two separate issues - they are not 2 questions, just two approaches for sharing a state you hit issues with.

Let me tackle the usage of a "global" variable - ${WORKAROUND}, to pass information from within a keyword (cause it's easier ;).

The behavior you see is because of variables scope. Consider this pseudo-code:

a = 5
print(a)  # 5

def keyword():
    a = 10
    print(a)

keyword()  # 10
print(a)   # 5?

So why the last print() outputs 5?
Because in the scope it's ran this is the value of the variable. What happens in keyword() is isolated (by default) to it; you create a variable that coincidently is also called "a", and assign a value to it. In this moment you're shadowing the variable "a" from the outer scope - it continues to exist for the program, but is no longer reachable from within keyword(). Once keyword() finishes, its "a" disappears, while the global one is unchanged.
This listing is python by the way, the language Robot Framework is based on, and the principles it more or less follows.

So pretty much the same happens in your code - the variable ${WORKAROUND} is defined in a case, or keyword, and you define a new one with the same name inside Get cell value. Thus, whatever you do to it in the keyword is not reflected to the original one in the outer scope.

Thankfully, there is an easy workaround (in Robot Framework) - use Set Test Variable inside the keyword, to overwrite the variable from the outer scope. All follow-up usages of ${WORKAROUND} in the case will have the changed value. There are also the similar keywords Set Suite Variable and Set Global Variable, which put the variable in a higher (and highest) scope.


Now the second case - passing a variable as an argument, and changing it inside a ~method~ keyword, in the hope it'll be changed in the calling scope.

I was going to write something long and as boring as the first part for passing by value vs passing by reference, and name -> objects bindings, but - no, I will
a) leave this here - https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ , which explains that plus scopes better than how I would have, and
b) get straight to the point - the issue here is the same as with the local variable's scope.

In this code:

Get cell value
    [Arguments]  ${temp_values_cell}  ${table_locator}  ${i}  ${j}
    ${temp_values_cell} =  Get table cell  ${table_locator}  ${i}  ${j}

, you declare that your keyword will take an argument, which you will reference with the name "temp_values_cell".
But on the very next line, you rebind that name to a new value - a new location in the memory; for your keyword, there is now only the variable "temp_values_cell" which holds the result of Get table cell. And it now does not have anything in common with the original variable that was used in the keyword call.
For the outside world - nothing has changed; the original variable you have passed to keyword is not affected, and will not be changed.

And - Set Test Variable will not help here (why - exercise for later :).

The orthodox way is to return a value from the keyword, and use it. Which you do in your sample code - return values, but don't assign.

5
  • Thanks for your answer. I will have the Set test variable keyword for future use.The problem is then, why it is not returned when I use the same name as an argument and a return argument. The other variables (i, j) seem to be passed without issues
    – squall2002
    Mar 6, 2019 at 20:10
  • I realized I was using the [Return] functionality wrong
    – squall2002
    Mar 6, 2019 at 22:43
  • I just realized I was treating the arguments as being passed by reference, when in fact they are passed as copy. Any way to pass by reference using robot framework?
    – squall2002
    Mar 7, 2019 at 3:53
  • Go thorough the article I've looked in my answer - there is no such thing as passing by value or reference in Robot Framework (or Python, or Java or Javascript) - what is passed is a name identifying a memory area. If you reassign that name inside the keyword/method - you are not influencing the original memory, you are working on a new one. This is what happens in your question. If on the other hand you mutate the original one, that mutation will stick - will be effective after the keyword/method ends. Mar 7, 2019 at 5:43
  • So if your parameter is a list, or dictionary or similar object - a mutable variable, and you pass such var as argument, and you change it inside the keyword/method - not reassign, but change its members attributes - mutate it, that will change the passed argument itself. For instance, if you call Append To List ${my arg} abc, and you have passed a list, after the keyword completion that list variable will have an extra member - "abc". Mar 7, 2019 at 5:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.