Safely with retries.
This commit is contained in:
parent
589d95b5ac
commit
ed1e267b41
|
@ -32,6 +32,9 @@ from selenium.common.exceptions import (
|
|||
) # noqa
|
||||
|
||||
|
||||
# TODO: read it from configuration.
|
||||
RETRIES = 3
|
||||
|
||||
logging.basicConfig(
|
||||
level=os.environ.get("BOTZ_LOGLEVEL", logging.INFO),
|
||||
format="%(levelname)s: [%(name)s] -> %(message)s",
|
||||
|
@ -40,18 +43,26 @@ logging.basicConfig(
|
|||
m_logger = logging.getLogger(__name__)
|
||||
m_logger.debug("Init at debug")
|
||||
|
||||
|
||||
def safely(f: T.Callable) -> T.Callable:
|
||||
def safely(retries: int = 0) -> T.Callable:
|
||||
def _safely(f: T.Callable) -> T.Callable:
|
||||
def _protection(self, *args, **kwargs):
|
||||
done = False
|
||||
while done or retries >= 0:
|
||||
try:
|
||||
f(self, *args, **kwargs)
|
||||
done = True
|
||||
except WebDriverException as e:
|
||||
self.logger.error("Something went wrong: %s", e)
|
||||
self.logger.error(
|
||||
"Something went wrong: %s [tentative #%s]", e, retries
|
||||
)
|
||||
retries -= 1
|
||||
finally:
|
||||
self.switch_to.default_content()
|
||||
|
||||
return _protection
|
||||
|
||||
return _safely
|
||||
|
||||
|
||||
def _is_present(driver: wd.Firefox, xpath: str) -> bool:
|
||||
try:
|
||||
|
@ -98,6 +109,7 @@ class Operator(wd.Firefox):
|
|||
"""
|
||||
Adds some configuration to Firefox.
|
||||
"""
|
||||
self.retries = RETRIES
|
||||
self.profile = wd.FirefoxProfile()
|
||||
# Do not send telemetry
|
||||
self.profile.set_preference("datareporting.policy.dataSubmissionEnabled", False)
|
||||
|
@ -132,7 +144,7 @@ class Operator(wd.Firefox):
|
|||
self._logged_in = False
|
||||
self._checked_in = False
|
||||
|
||||
@safely
|
||||
@safely(RETRIES)
|
||||
def login(self, user: str, password: str, force: bool = False) -> None:
|
||||
"""
|
||||
Do the login and proceed.
|
||||
|
@ -183,7 +195,7 @@ class Operator(wd.Firefox):
|
|||
else:
|
||||
self.logger.error("Login failed: %s", user)
|
||||
|
||||
@safely
|
||||
@safely(RETRIES)
|
||||
def logout(self, user: str, force: bool = False) -> None:
|
||||
"""
|
||||
Do the logout.
|
||||
|
@ -219,7 +231,14 @@ class Operator(wd.Firefox):
|
|||
_cookies = "dtLatC" in cookies
|
||||
return _right_url and _cookies
|
||||
|
||||
@safely
|
||||
@property
|
||||
def checked_in(self) -> bool:
|
||||
"""
|
||||
Check if the user is checked in already.
|
||||
"""
|
||||
return self._checked_in
|
||||
|
||||
@safely(RETRIES)
|
||||
def check_in(self, force: bool = False) -> None:
|
||||
"""
|
||||
Click the check in button.
|
||||
|
@ -239,9 +258,9 @@ class Operator(wd.Firefox):
|
|||
enter_butt.click()
|
||||
# Click the check in button and change
|
||||
# self._checked_in state in case of success
|
||||
pass
|
||||
self._checked_in = True
|
||||
|
||||
@safely
|
||||
@safely(RETRIES)
|
||||
def check_out(self, force: bool = False) -> None:
|
||||
"""
|
||||
Click the check out button.
|
||||
|
@ -261,7 +280,7 @@ class Operator(wd.Firefox):
|
|||
exit_butt.click()
|
||||
# Click the check in button and change
|
||||
# self._checked_in state in case of success
|
||||
pass
|
||||
self._checked_in = False
|
||||
|
||||
def __del__(self) -> None:
|
||||
self.quit()
|
||||
|
|
Loading…
Reference in New Issue
Block a user