Fix safely decorator and logout method.

This commit is contained in:
sfigato 2019-02-07 12:01:19 +01:00 committed by blallo
parent 862598f597
commit 8f032090ed
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -45,20 +45,25 @@ logger.debug("Init at debug")
def safely(retries: int = 0) -> T.Callable: def safely(retries: int = 0) -> T.Callable:
retr = retries
def _safely(f: T.Callable) -> T.Callable: def _safely(f: T.Callable) -> T.Callable:
ret = retr
def _protection(self, *args, **kwargs): def _protection(self, *args, **kwargs):
r = ret
done = False done = False
while done or retries >= 0: while r > 0:
try: try:
f(self, *args, **kwargs) f(self, *args, **kwargs)
done = True logger.debug("Success executing %s", f.__name__)
self.switch_to.default_content()
return
except WebDriverException as e: except WebDriverException as e:
self.logger.error( self.logger.error(
"Something went wrong: %s [tentative #%s]", e, retries "Something went wrong: %s [tentative #%s]", e, ret - r
) )
retries -= 1 r -= 1
finally:
self.switch_to.default_content()
return _protection return _protection
@ -144,6 +149,9 @@ class Operator(wd.Firefox):
self.logger.debug("Debug level") self.logger.debug("Debug level")
self._logged_in = False self._logged_in = False
self._checked_in = False self._checked_in = False
self.username = None
# Clean preceding session
self.delete_all_cookies()
@safely(RETRIES) @safely(RETRIES)
def login(self, user: str, password: str, force: bool = False) -> None: def login(self, user: str, password: str, force: bool = False) -> None:
@ -195,17 +203,18 @@ class Operator(wd.Firefox):
self.logger.info("Login success for user: %s", user) self.logger.info("Login success for user: %s", user)
else: else:
self.logger.error("Login failed: %s", user) self.logger.error("Login failed: %s", user)
self.username = user
@safely(RETRIES) @safely(RETRIES)
def logout(self, user: str, force: bool = False) -> None: def logout(self, force: bool = False) -> None:
""" """
Do the logout. Do the logout.
""" """
if not self._logged_in: if not self._logged_in:
self.logger.warning("Not yet logged in for user: %s", user) self.logger.warning("Not yet logged in")
if not force: if not force:
return return
self.logger.info("Forcing logout: %s", user) self.logger.info("Forcing logout: %s", self.username)
# Find the Profile menu and open it # Find the Profile menu and open it
profile_butt = self.find_element_by_xpath( profile_butt = self.find_element_by_xpath(
'//span[contains(@id, "imgNoPhotoLabel")]' '//span[contains(@id, "imgNoPhotoLabel")]'
@ -216,9 +225,10 @@ class Operator(wd.Firefox):
logout_butt = self.find_element_by_xpath('//input[@value="Logout user"]') logout_butt = self.find_element_by_xpath('//input[@value="Logout user"]')
logout_butt.click() logout_butt.click()
if "jsp/usut_wapplogout_portlet.jsp" in self.current_url: if "jsp/usut_wapplogout_portlet.jsp" in self.current_url:
self.logger.info("User successfully logged out: %s", user) self.logger.info("User successfully logged out: %s", self.username)
self.username = None
else: else:
self.logger.warning("Logout failed: %s", user) self.logger.warning("Logout failed: %s", self.username)
@property @property
def logged_in(self) -> bool: def logged_in(self) -> bool: