Skip to content

Commit fa0091e

Browse files
committed
Make updates to Recorder Mode
1 parent 8cb5b51 commit fa0091e

File tree

1 file changed

+89
-6
lines changed

1 file changed

+89
-6
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def __init__(self, *args, **kwargs):
9595
self.__last_page_url = None
9696
self.__last_page_source = None
9797
self.__skip_reason = None
98+
self.__origins_to_save = []
99+
self.__actions_to_save = []
98100
self.__dont_record_open = False
99101
self.__dont_record_js_click = False
100102
self.__new_window_on_rec_open = True
@@ -2987,6 +2989,15 @@ def save_screenshot_to_logs(
29872989
return page_actions.save_screenshot(
29882990
self.driver, name, test_logpath, selector, by
29892991
)
2992+
if self.recorder_mode:
2993+
url = self.get_current_url()
2994+
if url and len(url) > 0:
2995+
if ("http:") in url or ("https:") in url or ("file:") in url:
2996+
if self.get_session_storage_item("pause_recorder") == "no":
2997+
time_stamp = self.execute_script("return Date.now();")
2998+
origin = self.get_origin()
2999+
action = ["ss_tl", "", origin, time_stamp]
3000+
self.__extra_actions.append(action)
29903001
return page_actions.save_screenshot(self.driver, name, test_logpath)
29913002

29923003
def save_page_source(self, name, folder=None):
@@ -3183,6 +3194,22 @@ def activate_recorder(self):
31833194
except Exception:
31843195
pass
31853196

3197+
def save_recorded_actions(self):
3198+
"""(When using Recorder Mode, use this method if you plan on
3199+
navigating to a different domain/origin in the same tab.)
3200+
This method saves recorded actions from the active tab so that
3201+
a complete recording can be exported as a SeleniumBase file at the
3202+
end of the test. This is only needed in special cases because most
3203+
actions that result in a new origin, (such as clicking on a link),
3204+
should automatically open a new tab while Recorder Mode is enabled."""
3205+
url = self.get_current_url()
3206+
if url and len(url) > 0:
3207+
if ("http:") in url or ("https:") in url or ("file:") in url:
3208+
origin = self.get_origin()
3209+
self.__origins_to_save.append(origin)
3210+
tab_actions = self.__get_recorded_actions_on_active_tab()
3211+
self.__actions_to_save.append(tab_actions)
3212+
31863213
def __get_recorded_actions_on_active_tab(self):
31873214
url = self.driver.current_url
31883215
if (
@@ -3213,6 +3240,11 @@ def __process_recorded_actions(self):
32133240
if action not in used_actions:
32143241
used_actions.append(action)
32153242
raw_actions.append(action)
3243+
for tab_actions in self.__actions_to_save:
3244+
for action in tab_actions:
3245+
if action not in used_actions:
3246+
used_actions.append(action)
3247+
raw_actions.append(action)
32163248
for action in self.__extra_actions:
32173249
if action not in used_actions:
32183250
used_actions.append(action)
@@ -3234,6 +3266,15 @@ def __process_recorded_actions(self):
32343266
and srt_actions[n-1][0] == "sk_op"
32353267
):
32363268
srt_actions[n][0] = "_skip"
3269+
for n in range(len(srt_actions)):
3270+
if (
3271+
(srt_actions[n][0] == "begin" or srt_actions[n][0] == "_url_")
3272+
and n > 1
3273+
and srt_actions[n-1][0] == "_skip"
3274+
and srt_actions[n-2][0] == "sk_op"
3275+
and srt_actions[n][2] == srt_actions[n-1][2]
3276+
):
3277+
srt_actions[n][0] = "_skip"
32373278
for n in range(len(srt_actions)):
32383279
if (
32393280
(srt_actions[n][0] == "begin" or srt_actions[n][0] == "_url_")
@@ -3325,6 +3366,23 @@ def __process_recorded_actions(self):
33253366
):
33263367
srt_actions[n-1][0] = "_skip"
33273368
srt_actions[n][2] = srt_actions[n-1][1][1]
3369+
for n in range(len(srt_actions)):
3370+
if (
3371+
srt_actions[n][0] == "input"
3372+
and n > 0
3373+
and srt_actions[n-1][0] == "e_mfa"
3374+
):
3375+
srt_actions[n][0] = "_skip"
3376+
for n in range(len(srt_actions)):
3377+
if (
3378+
(srt_actions[n][0] == "begin" or srt_actions[n][0] == "_url_")
3379+
and n > 0
3380+
and (
3381+
srt_actions[n-1][0] == "submi"
3382+
or srt_actions[n-1][0] == "e_mfa"
3383+
)
3384+
):
3385+
srt_actions[n][0] = "f_url"
33283386
origins = []
33293387
for n in range(len(srt_actions)):
33303388
if (
@@ -3337,6 +3395,8 @@ def __process_recorded_actions(self):
33373395
origin = origin[0:-1]
33383396
if origin not in origins:
33393397
origins.append(origin)
3398+
for origin in self.__origins_to_save:
3399+
origins.append(origin)
33403400
for n in range(len(srt_actions)):
33413401
if (
33423402
srt_actions[n][0] == "click"
@@ -3382,6 +3442,8 @@ def __process_recorded_actions(self):
33823442
ext_actions.append("s_c_d")
33833443
ext_actions.append("sh_fc")
33843444
ext_actions.append("c_l_s")
3445+
ext_actions.append("e_mfa")
3446+
ext_actions.append("ss_tl")
33853447
for n in range(len(srt_actions)):
33863448
if srt_actions[n][0] in ext_actions:
33873449
origin = srt_actions[n][2]
@@ -3395,6 +3457,9 @@ def __process_recorded_actions(self):
33953457
if srt_actions[n][0] == "js_ty":
33963458
srt_actions[n][2] = srt_actions[n][1][1]
33973459
srt_actions[n][1] = srt_actions[n][1][0]
3460+
if srt_actions[n][0] == "e_mfa":
3461+
srt_actions[n][2] = srt_actions[n][1][1]
3462+
srt_actions[n][1] = srt_actions[n][1][0]
33983463
if srt_actions[n][0] == "_url_" and origin not in origins:
33993464
origins.append(origin)
34003465
if origin not in origins:
@@ -3449,6 +3514,21 @@ def __process_recorded_actions(self):
34493514
elif '"' in action[1] and '"' in text:
34503515
sb_actions.append("self.%s('%s', '%s')" % (
34513516
method, action[1], text))
3517+
elif action[0] == "e_mfa":
3518+
method = "enter_mfa_code"
3519+
text = action[2].replace("\n", "\\n")
3520+
if '"' not in action[1] and '"' not in text:
3521+
sb_actions.append('self.%s("%s", "%s")' % (
3522+
method, action[1], text))
3523+
elif '"' not in action[1] and '"' in text:
3524+
sb_actions.append('self.%s("%s", \'%s\')' % (
3525+
method, action[1], text))
3526+
elif '"' in action[1] and '"' not in text:
3527+
sb_actions.append('self.%s(\'%s\', "%s")' % (
3528+
method, action[1], text))
3529+
elif '"' in action[1] and '"' in text:
3530+
sb_actions.append("self.%s('%s', '%s')" % (
3531+
method, action[1], text))
34523532
elif action[0] == "h_clk":
34533533
method = "hover_and_click"
34543534
if '"' not in action[1] and '"' not in action[2]:
@@ -3631,19 +3711,22 @@ def __process_recorded_actions(self):
36313711
else:
36323712
sb_actions.append("self.%s('%s')" % (
36333713
method, action[1][0]))
3714+
elif action[0] == "ss_tl":
3715+
method = "save_screenshot_to_logs"
3716+
sb_actions.append('self.%s()' % method)
36343717
elif action[0] == "sh_fc":
3635-
cb_method = "show_file_choosers"
3636-
sb_actions.append('self.%s()' % cb_method)
3718+
method = "show_file_choosers"
3719+
sb_actions.append('self.%s()' % method)
36373720
elif action[0] == "c_l_s":
36383721
sb_actions.append("self.clear_local_storage()")
36393722
elif action[0] == "c_box":
3640-
cb_method = "check_if_unchecked"
3723+
method = "check_if_unchecked"
36413724
if action[2] == "no":
3642-
cb_method = "uncheck_if_checked"
3725+
method = "uncheck_if_checked"
36433726
if '"' not in action[1]:
3644-
sb_actions.append('self.%s("%s")' % (cb_method, action[1]))
3727+
sb_actions.append('self.%s("%s")' % (method, action[1]))
36453728
else:
3646-
sb_actions.append("self.%s('%s')" % (cb_method, action[1]))
3729+
sb_actions.append("self.%s('%s')" % (method, action[1]))
36473730

36483731
filename = self.__get_filename()
36493732
new_file = False

0 commit comments

Comments
 (0)