Skip to content

Commit 58f7940

Browse files
committed
Make updates to Multi-Factor Authentication methods
1 parent fa0091e commit 58f7940

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5278,9 +5278,10 @@ def is_chromedriver_too_old(self):
52785278
return True # chromedriver is too old! Please upgrade!
52795279
return False
52805280

5281-
def get_totp_code(self, totp_key=None):
5282-
"""Returns a time-based one-time password based on the
5283-
Google Authenticator algorithm. Works with Authy and Okta.
5281+
def get_mfa_code(self, totp_key=None):
5282+
"""Same as get_totp_code() and get_google_auth_password().
5283+
Returns a time-based one-time password based on the
5284+
Google Authenticator algorithm for multi-factor authentication.
52845285
If the "totp_key" is not specified, this method defaults
52855286
to using the one provided in [seleniumbase/config/settings.py].
52865287
Google Authenticator codes expire & change at 30-sec intervals.
@@ -5307,6 +5308,36 @@ def get_totp_code(self, totp_key=None):
53075308
totp = pyotp.TOTP(totp_key)
53085309
return str(totp.now())
53095310

5311+
def enter_mfa_code(
5312+
self, selector, totp_key=None, by=By.CSS_SELECTOR, timeout=None
5313+
):
5314+
"""Enters into the field a Multi-Factor Authentication TOTP Code.
5315+
If the "totp_key" is not specified, this method defaults
5316+
to using the one provided in [seleniumbase/config/settings.py].
5317+
The TOTP code is generated by the Google Authenticator Algorithm.
5318+
This method will automatically press ENTER after typing the code."""
5319+
self.__check_scope()
5320+
if not timeout:
5321+
timeout = settings.SMALL_TIMEOUT
5322+
self.wait_for_element_visible(selector, by=by, timeout=timeout)
5323+
if self.recorder_mode:
5324+
css_selector = self.convert_to_css_selector(selector, by=by)
5325+
url = self.get_current_url()
5326+
if url and len(url) > 0:
5327+
if ("http:") in url or ("https:") in url or ("file:") in url:
5328+
origin = self.get_origin()
5329+
if self.get_session_storage_item("pause_recorder") == "no":
5330+
time_stamp = self.execute_script("return Date.now();")
5331+
sel_key = [css_selector, totp_key]
5332+
action = ["e_mfa", sel_key, origin, time_stamp]
5333+
self.__extra_actions.append(action)
5334+
# Sometimes Sign-In leaves the origin... Save work first.
5335+
self.__origins_to_save.append(origin)
5336+
tab_actions = self.__get_recorded_actions_on_active_tab()
5337+
self.__actions_to_save.append(tab_actions)
5338+
mfa_code = self.get_mfa_code(totp_key)
5339+
self.update_text(selector, mfa_code + "\n", by=by, timeout=timeout)
5340+
53105341
def convert_css_to_xpath(self, css):
53115342
return css_to_xpath.convert_css_to_xpath(css)
53125343

@@ -6247,12 +6278,24 @@ def assert_element_not_present(
62476278
return True
62486279

62496280
def get_google_auth_password(self, totp_key=None):
6250-
""" Same as self.get_totp_code() """
6251-
return self.get_totp_code(totp_key=totp_key)
6281+
""" Same as self.get_mfa_code() """
6282+
return self.get_mfa_code(totp_key=totp_key)
62526283

62536284
def get_google_auth_code(self, totp_key=None):
6254-
""" Same as self.get_totp_code() """
6255-
return self.get_totp_code(totp_key=totp_key)
6285+
""" Same as self.get_mfa_code() """
6286+
return self.get_mfa_code(totp_key=totp_key)
6287+
6288+
def get_totp_code(self, totp_key=None):
6289+
""" Same as self.get_mfa_code() """
6290+
return self.get_mfa_code(totp_key=totp_key)
6291+
6292+
def enter_totp_code(
6293+
self, selector, totp_key=None, by=By.CSS_SELECTOR, timeout=None
6294+
):
6295+
""" Same as self.enter_mfa_code() """
6296+
return self.enter_mfa_code(
6297+
selector=selector, totp_key=totp_key, by=by, timeout=timeout
6298+
)
62566299

62576300
def assert_no_broken_links(self, multithreaded=True):
62586301
""" Same as self.assert_no_404_errors() """

0 commit comments

Comments
 (0)