Improve login/logout logic.

This commit is contained in:
sfigato 2019-03-05 14:48:17 +01:00 committed by blallo
parent 11785bc1e2
commit 8a4632de75
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -55,15 +55,16 @@ def safely(retries: int = 0) -> T.Callable:
done = False done = False
while r > 0: while r > 0:
try: try:
f(self, *args, **kwargs) val = f(self, *args, **kwargs)
logger.debug("Success executing %s", f.__name__) logger.debug("Success executing %s", f.__name__)
self.switch_to.default_content() self.switch_to.default_content()
return return val
except WebDriverException as e: except WebDriverException as e:
self.logger.error( self.logger.error(
"Something went wrong: %s [tentative #%s]", e, ret - r "Something went wrong: %s [tentative #%s]", e, ret - r
) )
r -= 1 r -= 1
time.sleep(1)
return _protection return _protection
@ -158,14 +159,16 @@ class Operator(wd.Firefox):
""" """
Do the login and proceed. Do the login and proceed.
""" """
if self._logged_in: self.logger.info("Logging in - user: %s - session: %s", user, self.name)
if self.logged_in:
self.logger.warning("Already logged in: %s", user) self.logger.warning("Already logged in: %s", user)
if not force: if not force:
return return
self.logger.info("Forcing login: %s", user) self.logger.info("Forcing login: %s", user)
# Retrieve login page # Retrieve login page if not yet on it
if self.base_uri not in self.current_url:
self.get(self.base_uri) self.get(self.base_uri)
time.sleep(5) time.sleep(1)
_correct_url = "cpccchk" in self.current_url _correct_url = "cpccchk" in self.current_url
_now = datetime.now() _now = datetime.now()
_elapsed = timedelta(seconds=0) _elapsed = timedelta(seconds=0)
@ -183,6 +186,7 @@ class Operator(wd.Firefox):
return return
self.logger.debug("After login get: %s", self.current_url) self.logger.debug("After login get: %s", self.current_url)
time.sleep(1) time.sleep(1)
self.logger.info("Looking for fields in session %s", self.name)
# Username # Username
user_form = self.find_element_by_name("m_cUserName") user_form = self.find_element_by_name("m_cUserName")
# Password # Password
@ -192,21 +196,17 @@ class Operator(wd.Firefox):
# Compile and submit # Compile and submit
user_form.send_keys(user) user_form.send_keys(user)
pass_form.send_keys(password) pass_form.send_keys(password)
do_it = True
if self.debug and not force and not self.opts.headless:
_do_it = input("Really do the login? [y/n] ").lower()
do_it = True if _do_it == "y" else False
if do_it:
login_butt.submit() login_butt.submit()
time.sleep(5) time.sleep(5)
self.logger.debug("Login result: %s", self.title) self.logger.debug("Login result: %s", self.title)
if "Routine window" in self.title: while "Routine window" in self.title:
self.logger.debug("Reloading...") self.logger.debug("Reloading...")
self.refresh() self.refresh()
self.logger.info("Reloaded %s", self.name)
self.switch_to.alert.accept() self.switch_to.alert.accept()
if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout): if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout):
self._logged_in = True self._logged_in = True
self.logger.info("Login success for user: %s", user) self.logger.info("Login succeeded for user: %s", user)
else: else:
self.logger.error("Login failed: %s", user) self.logger.error("Login failed: %s", user)
self.username = user self.username = user
@ -230,12 +230,31 @@ class Operator(wd.Firefox):
# Find the logout button # Find the logout button
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 self._back_to_login():
self.logger.debug("Back on login page")
@safely(RETRIES)
def _back_to_login(self) -> bool:
_now = datetime.now()
_elapsed = timedelta(seconds=0)
_is_logout_page = "jsp/usut_wapplogout_portlet.jsp" in self.current_url
while not _is_logout_page or _elapsed > self.timeout:
time.sleep(1)
self.logger.debug("Waiting to land on logout page...")
_is_logout_page = "jsp/usut_wapplogout_portlet.jsp" in self.current_url
_elapsed = datetime.now() - _now
if _is_logout_page:
self.logger.info("User successfully logged out: %s", self.username) self.logger.info("User successfully logged out: %s", self.username)
self.username = None self.username = None
back_to_login_butt = self.find_element_by_xpath(
'//input[contains(@id, "_button15")]'
)
back_to_login_butt.click()
self.delete_all_cookies() self.delete_all_cookies()
return True
else: else:
self.logger.warning("Logout failed: %s", self.username) self.logger.warning("Logout failed: %s", self.username)
return False
@property @property
def logged_in(self) -> bool: def logged_in(self) -> bool: