Fix safely decorator and logout method.
This commit is contained in:
parent
862598f597
commit
8f032090ed
|
@ -45,20 +45,25 @@ logger.debug("Init at debug")
|
|||
|
||||
|
||||
def safely(retries: int = 0) -> T.Callable:
|
||||
retr = retries
|
||||
|
||||
def _safely(f: T.Callable) -> T.Callable:
|
||||
ret = retr
|
||||
|
||||
def _protection(self, *args, **kwargs):
|
||||
r = ret
|
||||
done = False
|
||||
while done or retries >= 0:
|
||||
while r > 0:
|
||||
try:
|
||||
f(self, *args, **kwargs)
|
||||
done = True
|
||||
logger.debug("Success executing %s", f.__name__)
|
||||
self.switch_to.default_content()
|
||||
return
|
||||
except WebDriverException as e:
|
||||
self.logger.error(
|
||||
"Something went wrong: %s [tentative #%s]", e, retries
|
||||
"Something went wrong: %s [tentative #%s]", e, ret - r
|
||||
)
|
||||
retries -= 1
|
||||
finally:
|
||||
self.switch_to.default_content()
|
||||
r -= 1
|
||||
|
||||
return _protection
|
||||
|
||||
|
@ -144,6 +149,9 @@ class Operator(wd.Firefox):
|
|||
self.logger.debug("Debug level")
|
||||
self._logged_in = False
|
||||
self._checked_in = False
|
||||
self.username = None
|
||||
# Clean preceding session
|
||||
self.delete_all_cookies()
|
||||
|
||||
@safely(RETRIES)
|
||||
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)
|
||||
else:
|
||||
self.logger.error("Login failed: %s", user)
|
||||
self.username = user
|
||||
|
||||
@safely(RETRIES)
|
||||
def logout(self, user: str, force: bool = False) -> None:
|
||||
def logout(self, force: bool = False) -> None:
|
||||
"""
|
||||
Do the logout.
|
||||
"""
|
||||
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:
|
||||
return
|
||||
self.logger.info("Forcing logout: %s", user)
|
||||
self.logger.info("Forcing logout: %s", self.username)
|
||||
# Find the Profile menu and open it
|
||||
profile_butt = self.find_element_by_xpath(
|
||||
'//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.click()
|
||||
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:
|
||||
self.logger.warning("Logout failed: %s", user)
|
||||
self.logger.warning("Logout failed: %s", self.username)
|
||||
|
||||
@property
|
||||
def logged_in(self) -> bool:
|
||||
|
|
Loading…
Reference in New Issue
Block a user