Hi,
I want to instantiate global variables dynamically in Robot Framework, but I’ve encountered challenges with making this simple and user friendly, and getting my IDE to recognize these dynamically created variables for auto-completion for users to use them. Despite trying various methods, none seem to fully work:
Why is this needed?
I am working on testing a web application where the XPaths of web elements vary depending on the version of the product. Since this is an old, legacy application and I cannot modify how it operates, I need to test multiple versions of the product—some older and some newer. I retrieve the version of the web app automatically, and based on this, I need Robot Framework to instantiate variables accordingly.
For example:
- If you define global variables in the
*** Variables ***
section, you cannot use conditional logic (e.g.,if
statements) to determine their values dynamically. - Setting variables within a Python library allows for conditional logic but does not always provide IDE support for auto-completion of these dynamically created variables in Robot Framework files.
- Using resource files or variable files with predefined values also doesn’t adapt to the dynamic needs based on conditions evaluated at runtime.
Questions:
- Is there a recommended approach or workaround to achieve dynamic global variable instantiation in a way that also supports IDE auto-completion?
- Can you suggest best practices or strategies for handling variable values that depend on runtime conditions or external inputs?
Thank you for any assistance or insights you can provide!
EXAMPLE CODE:
As you can see this is not user friendly, and the variables names wont be autocompleted in IDE’s, making the users more hard to use the tool if the whant to code and use those variables.:
from robot.libraries.BuiltIn import BuiltIn
class common:
def __init__(self, server):
# Example: Read conditions from environment variables or set defaults
if server.version == '2.1':
self.set_global_variable('variable1','value')
def set_global_variable(self, variable_name, value):
BuiltIn().set_global_variable(f"${{{variable_name}}}", value)