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
|
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
|
||||||
self.get(self.base_uri)
|
if self.base_uri not in self.current_url:
|
||||||
time.sleep(5)
|
self.get(self.base_uri)
|
||||||
|
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,23 +196,19 @@ 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
|
login_butt.submit()
|
||||||
if self.debug and not force and not self.opts.headless:
|
time.sleep(5)
|
||||||
_do_it = input("Really do the login? [y/n] ").lower()
|
self.logger.debug("Login result: %s", self.title)
|
||||||
do_it = True if _do_it == "y" else False
|
while "Routine window" in self.title:
|
||||||
if do_it:
|
self.logger.debug("Reloading...")
|
||||||
login_butt.submit()
|
self.refresh()
|
||||||
time.sleep(5)
|
self.logger.info("Reloaded %s", self.name)
|
||||||
self.logger.debug("Login result: %s", self.title)
|
self.switch_to.alert.accept()
|
||||||
if "Routine window" in self.title:
|
if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout):
|
||||||
self.logger.debug("Reloading...")
|
self._logged_in = True
|
||||||
self.refresh()
|
self.logger.info("Login succeeded for user: %s", user)
|
||||||
self.switch_to.alert.accept()
|
else:
|
||||||
if is_present(self, '//a[contains(@class, "imgMenu_ctrl")]', self.timeout):
|
self.logger.error("Login failed: %s", user)
|
||||||
self._logged_in = True
|
|
||||||
self.logger.info("Login success for user: %s", user)
|
|
||||||
else:
|
|
||||||
self.logger.error("Login failed: %s", user)
|
|
||||||
self.username = user
|
self.username = user
|
||||||
|
|
||||||
@safely(RETRIES)
|
@safely(RETRIES)
|
||||||
|
@ -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:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user