Improve login/logout logic.
This commit is contained in:
parent
11785bc1e2
commit
8a4632de75
|
@ -55,15 +55,16 @@ def safely(retries: int = 0) -> T.Callable:
|
|||
done = False
|
||||
while r > 0:
|
||||
try:
|
||||
f(self, *args, **kwargs)
|
||||
val = f(self, *args, **kwargs)
|
||||
logger.debug("Success executing %s", f.__name__)
|
||||
self.switch_to.default_content()
|
||||
return
|
||||
return val
|
||||
except WebDriverException as e:
|
||||
self.logger.error(
|
||||
"Something went wrong: %s [tentative #%s]", e, ret - r
|
||||
)
|
||||
r -= 1
|
||||
time.sleep(1)
|
||||
|
||||
return _protection
|
||||
|
||||
|
@ -158,14 +159,16 @@ class Operator(wd.Firefox):
|
|||
"""
|
||||
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)
|
||||
if not force:
|
||||
return
|
||||
self.logger.info("Forcing login: %s", user)
|
||||
# Retrieve login page
|
||||
self.get(self.base_uri)
|
||||
time.sleep(5)
|
||||
# Retrieve login page if not yet on it
|
||||
if self.base_uri not in self.current_url:
|
||||
self.get(self.base_uri)
|
||||
time.sleep(1)
|
||||
_correct_url = "cpccchk" in self.current_url
|
||||
_now = datetime.now()
|
||||
_elapsed = timedelta(seconds=0)
|
||||
|
@ -183,6 +186,7 @@ class Operator(wd.Firefox):
|
|||
return
|
||||
self.logger.debug("After login get: %s", self.current_url)
|
||||
time.sleep(1)
|
||||
self.logger.info("Looking for fields in session %s", self.name)
|
||||
# Username
|
||||
user_form = self.find_element_by_name("m_cUserName")
|
||||
# Password
|
||||
|
@ -192,23 +196,19 @@ class Operator(wd.Firefox):
|
|||
# Compile and submit
|
||||
user_form.send_keys(user)
|
||||
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()
|
||||
time.sleep(5)
|
||||
self.logger.debug("Login result: %s", self.title)
|
||||
if "Routine window" in self.title:
|
||||
self.logger.debug("Reloading...")
|
||||
self.refresh()
|
||||
self.switch_to.alert.accept()
|
||||
if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout):
|
||||
self._logged_in = True
|
||||
self.logger.info("Login success for user: %s", user)
|
||||
else:
|
||||
self.logger.error("Login failed: %s", user)
|
||||
login_butt.submit()
|
||||
time.sleep(5)
|
||||
self.logger.debug("Login result: %s", self.title)
|
||||
while "Routine window" in self.title:
|
||||
self.logger.debug("Reloading...")
|
||||
self.refresh()
|
||||
self.logger.info("Reloaded %s", self.name)
|
||||
self.switch_to.alert.accept()
|
||||
if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout):
|
||||
self._logged_in = True
|
||||
self.logger.info("Login succeeded for user: %s", user)
|
||||
else:
|
||||
self.logger.error("Login failed: %s", user)
|
||||
self.username = user
|
||||
|
||||
@safely(RETRIES)
|
||||
|
@ -230,12 +230,31 @@ class Operator(wd.Firefox):
|
|||
# Find the logout button
|
||||
logout_butt = self.find_element_by_xpath('//input[@value="Logout user"]')
|
||||
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.username = None
|
||||
back_to_login_butt = self.find_element_by_xpath(
|
||||
'//input[contains(@id, "_button15")]'
|
||||
)
|
||||
back_to_login_butt.click()
|
||||
self.delete_all_cookies()
|
||||
return True
|
||||
else:
|
||||
self.logger.warning("Logout failed: %s", self.username)
|
||||
return False
|
||||
|
||||
@property
|
||||
def logged_in(self) -> bool:
|
||||
|
|
Loading…
Reference in New Issue
Block a user