2

Is it possible to access static class variables from a library loaded into robot framework?

For example, say I include the following python library in my .robot file:

foo = 'value'

class MyClass(self):
    bar = 'value'

Is there a way in the .robot file that included it to refer to foo or bar?

2 Answers 2

4

You can get at these by getting a reference to the raw python module with Get Library Instance, and then use extended variable syntax to get the values.

For example, consider a library named MyLibrary.py:

# MyLibrary.py
foo = "this is foo"

class MyClass(object):
    bar = "this is bar"

You can access foo and bar by using Get Library Instance to get a handle to the library:

*** Settings ***
| Library | MyLibrary.py

*** Test Cases ***
| Example of accessing variables in a library
| | ${lib}= | Get Library Instance | MyLibrary
| | 
| | Should be equal as strings | ${lib.foo} | this is foo
| | Should be equal as strings | ${lib.MyClass.bar} | this is bar
Sign up to request clarification or add additional context in comments.

2 Comments

So even with the ${object.variable} access capability in robotframework, it does not apply to "libraries" only objects?
@kimon: after thinking a bit more about this problem I came up with a solution. I've edited my answer.
1

You should add ROBOT_LIBRARY_SCOPE = 'GLOBAL' to MyClass. If not Get Library Instance will create second instance of the MyClass.

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.