You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatically import modules that are used with Evaluate, Run Keyword If, and others #3349
Currently BuiltIn keywords like Run Keyword If and Should Be True that evaluate Python expressions automatically add sys and os modules to the evaluation namespace (#1449) and Evaluate has a separate modules argument to specify which modules to add to the namespace (#240). We are now adding inline Python evaluation support (#3179) and need to decide how to handle modules with it. It would benefit from more modules than sys and os, but deciding what modules to add automatically isn't easy and it's not easy to come up with a good syntax to explicitly add modules either. The best idea discussed there seems to be automatically importing modules that are used.
Automatically importing modules basically means that if an expression like foo.bar is evaluated, and foo isn't found from the evaluation namespace otherwise, we'd automatically try to import module foo. Without this functionality foo.bar would resort to an error like NameError: name 'foo' is not defined, so the change ought to be fully backwards compatible.
One thing to think about is how to handle failed module imports. If we just let the original exception from Python to go through, the error will be something like ImportError: No module named foo. This is OK if there was an idea to import a module, but if there's a typo like lem(...) (not len(...)) then it can be pretty strange. Perhaps we should provide a custom message telling something like 'lem' was not found nor importable as a module.
If this functionality is implemented, the current modules argument with Evaluate won't have any effect anymore. We can deprecate it but because there's no hurry removing, we shouldn't do it "loudly" in the beginning. Instead we can document it's not anymore needed with RF 3.2 and ignore it. Deprecating it so that there's a warning can wait until RF 3.3 or later.
instead (and in the future omit modules altogether), but it turned this usage fails with AttributeError: module 'selenium' has no attribute 'webdriver'. This is due to how the selenium module has been implemented. You can try also on the interpreter that only importing selenium doesn't make selenium.webdriver submodule available, you need to explicitly import it:
would work, but unfortunately that fails with NameError: name 'selenium' is not defined. Apparently now only the selenium.webdriver submodule is in the evaluation namespace and selenium alone isn't found. This is pretty strange, even the interpreter works differently, but we cannot really fight against Python's import/evaluation machinery. The original example avoids this problem by telling Evaluate to import selenium.webdriver and then finding it from sys.modules by name. That's clever but in my opinion this solution (that also works) is a bit cleaner:
It's a bit annoying (and definitely not obvious) that we need to tell Evaluate to import both selenium and selenium.webdriver modules. Still better than using sys.modules in my opinion.
What does all that mean? Well, many things:
The approach to automatically import module foo when used like foo.bar.zap doesn't necessarily work if bar is a submodule. This depends on the module implementation. (If we really wanted, we could solve this by returning a proxy object that wraps foo and automatically imports submodules when accessed like foo.bar, but I seriously doubt that's worth the effort.)
The above means modules with Evaluate are still needed. They just aren't needed as often as they used to be.
We could consider handling modules=root.submodule a bit better to avoid the need to use modules=root, root.submodule. We could simply import both root and root.submodule in the former case as well. That would make the example above with modules=selenium.webdriver work. This enhancement requires a separate issue.
Finally, Open Browser in SeleniumLibrary 4.0 and newer has better support for custom browser configuration making the need to construct ChromeOptions like this pretty rare. It may still be needed sometimes and similar needs may arise with other libraries, so the underlying problem with Evaluate is still relevant.
Submitted #3355 about automatically importing parent modules when using modules=rootmod.submod.
Implementation of this issue as well as highly related #3179 and #3355 is ready and I'll commit changes shortly. Documentation still missing but I hope to get that done soon as well.
Activity
pekkaklarck commentedon Oct 21, 2019
In comments of #3179 @stdedos asked about this usage that has been commonly used with SeleniumLibrary to open browser with custom configuration:
My initial reaction was to use
instead (and in the future omit
modulesaltogether), but it turned this usage fails withAttributeError: module 'selenium' has no attribute 'webdriver'. This is due to how theseleniummodule has been implemented. You can try also on the interpreter that only importingseleniumdoesn't makeselenium.webdriversubmodule available, you need to explicitly import it:Then I thought that at least
would work, but unfortunately that fails with
NameError: name 'selenium' is not defined. Apparently now only theselenium.webdriversubmodule is in the evaluation namespace andseleniumalone isn't found. This is pretty strange, even the interpreter works differently, but we cannot really fight against Python's import/evaluation machinery. The original example avoids this problem by tellingEvaluateto importselenium.webdriverand then finding it fromsys.modulesby name. That's clever but in my opinion this solution (that also works) is a bit cleaner:It's a bit annoying (and definitely not obvious) that we need to tell
Evaluateto import bothseleniumandselenium.webdrivermodules. Still better than usingsys.modulesin my opinion.What does all that mean? Well, many things:
The approach to automatically import module
foowhen used likefoo.bar.zapdoesn't necessarily work ifbaris a submodule. This depends on the module implementation. (If we really wanted, we could solve this by returning a proxy object that wrapsfooand automatically imports submodules when accessed likefoo.bar, but I seriously doubt that's worth the effort.)The above means
moduleswithEvaluateare still needed. They just aren't needed as often as they used to be.We could consider handling
modules=root.submodulea bit better to avoid the need to usemodules=root, root.submodule. We could simply import bothrootandroot.submodulein the former case as well. That would make the example above withmodules=selenium.webdriverwork. This enhancement requires a separate issue.Finally,
Open Browserin SeleniumLibrary 4.0 and newer has better support for custom browser configuration making the need to construct ChromeOptions like this pretty rare. It may still be needed sometimes and similar needs may arise with other libraries, so the underlying problem withEvaluateis still relevant.stdedos commentedon Oct 21, 2019
Note that ^^ this is coming from e.g.
https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Create%20Webdriver
I will not attempt to fix it, as I cannot clain to fully understand Selenium
Maybe this vv should be added in its place (if also applicable)
pekkaklarck commentedon Oct 21, 2019
That's a strange example in SL docs! Ping @aaltat.
Anyway, it would still continue to work
aaltat commentedon Oct 21, 2019
Jep that should be changed. Thx
Also acceptance test should be changed https://github.com/robotframework/SeleniumLibrary/blob/master/atest/acceptance/create_webdriver.robot#L49
pekkaklarck commentedon Oct 29, 2019
Submitted #3355 about automatically importing parent modules when using
modules=rootmod.submod.Implementation of this issue as well as highly related #3179 and #3355 is ready and I'll commit changes shortly. Documentation still missing but I hope to get that done soon as well.
Automatic module imports with Evaluate and friends
Fix Evaluate with explicit modules used in lambda.
Fix Evaluate with explicit modules used in lambda.