问题
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?
回答1:
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
回答2:
You should add ROBOT_LIBRARY_SCOPE = 'GLOBAL'
to MyClass. If not Get Library Instance
will create second instance of the MyClass.
来源:https://stackoverflow.com/questions/25859499/accessing-static-class-variables-with-robotframework