Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

robotframework - Robot Framework: access Robot's global variables from Python library code?

I have some settings-type global vars that I'd like to be able to access from Python code. For example:

pybot --variable RESULTS_PATH:/wherever/this/points test.txt

Now, my module logger.py file needs to know the results_path to set up properly.

I know that I can initialize the logger with a variable, like

***Settings***
Library  logger  ${RESULTS_PATH}

And then in logger I'll be passed results_path:

 def __init__(self, results_path):
   # Whatever

However the problem with doing it this way for me is that I want to access and use the logger from both Python code and within test cases. So if I set it up this way, if I want to use the logger from Python code I run into the same problem of needing results_path to initialize the logger properly.

Are there any functions in the robot framework library that would allow me to grab the value of ${RESULTS_PATH} from Python code? What is the proper way to do something like this?

Right now, my workaround for the issue is to set RESULTS_PATH as an environment variable. So I have something like:

Run like:

RESULTS_PATH=/wherever/this/points pybot test.txt

File test.txt:

***Settings***
Library  logger
...

File logger.py:

results_path = os.environ["RESULTS_PATH"]
# Other set up stuff
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will want to use rf's BuiltIn library, for reference read the documentation as found here. This provides the keywords that are built into Robot Framework and so should reliably stay usable:

from robot.libraries.BuiltIn import BuiltIn
results_path = BuiltIn().get_variable_value("${RESULTS_PATH}")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...