consider adding entity.maybe(query) method
Some selenium webdriver commands:
- throws exception on failure
- others can return None
We already have a entity.get(query) method, that will try to perform the corresponding command, and correspondingly either fail or return None.
Let's consider adding entity.maybe(query) method to always return None.
Potential usage... Currently we don't have a method to check if browser is up and running... One would implement such helper and use it in such a way:
def is_browser_opened(browser):
try:
return browser.driver.title is not None
except Exception:
return None
# and then use it like:
if browser.get(is_browser_opened):
# do something safely here...
...
So... If we had entity.maybe(query) method, the code might be much simpler...
either
from selene import query
if browser.maybe(query.title):
# do something safely here...
...
or
from selene import query
is_browser_opened = query.title
if browser.maybe(query.title):
# do something safely here...
...
by the way... maybe we should consider to add something like is_browser_opened to selene/core/query.py...
@yashaka I think "maybe" is a questionable name. I ask myself "what if not maybe"? And it's hard to answer.
May be better to use browser.have() as boolean return?
Have does not work here, because this term already has meaning in Selene, and it's different.
Also it does not work from English perspective.
The idea of Maybe - comes from the "maybe monad", that is used two represent two states "the value exists and here it is" and the "the value is absent" - what is exactly what here we want to simulate with value | None in return. This is kind of - a classic term/classic name. Kind of exactly matching what we need, with also pretty good English understanding
- "maybe you will have a result but maybe not" - that's why you have to check for None before doing anything else.
The difference with classic Maybe monad though is in fact that real monad will enforce you to check, you will not be able to code the access to result value without check. In our case - we still can. If we would use in different place in Selene - actual Maybe Monad - we would have the conflict of meaning, but we never will use actual Monad in such a simple mass-market tool like Selene, this would be a crazy overengineering:-) But to use the classic idea and name - sounds for me - pretty reasonable.
Yet I am open to other "prefix" like "maybe" that can help to guess the correct sense of "maybe you have a result but maybe not".
For now I believe that "maybe" is the best (check other synonyms here: https://www.thesaurus.com/browse/maybe)
On Mon, Feb 21, 2022, 09:51 Aleksandr Kotlyar @.***> wrote:
@yashaka https://github.com/yashaka I think "maybe" is a questionable name. I ask myself "what if not maybe"? May be better to use browser.have() as boolean return?
— Reply to this email directly, view it on GitHub https://github.com/yashaka/selene/issues/402#issuecomment-1046566107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAO6ZHR6O6WTHNPNHKMKUKLU4HVH7ANCNFSM5O4OJPFQ . You are receiving this because you were mentioned.Message ID: @.***>