Skip to content

Commit aad1129

Browse files
committed
Update Messenger methods
1 parent 6501c3f commit aad1129

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,22 +1213,78 @@ def activate_messenger(self):
12131213

12141214
def set_messenger_theme(self, theme="default", location="default",
12151215
max_messages="default"):
1216+
""" Sets a theme for posting messages.
1217+
Themes: ["flat", "future", "block", "air", "ice"]
1218+
Locations: ["top_left", "top_center", "top_right",
1219+
"bottom_left", "bottom_center", "bottom_right"]
1220+
max_messages is the limit of concurrent messages to display. """
1221+
if not theme:
1222+
theme = "default" # "future"
1223+
if not location:
1224+
location = "default" # "bottom_right"
1225+
if not max_messages:
1226+
max_messages = "default" # "8"
12161227
js_utils.set_messenger_theme(
12171228
self.driver, theme=theme,
12181229
location=location, max_messages=max_messages)
12191230

1220-
def post_message(self, message, style="info", duration=None):
1231+
def post_message(self, message, duration=None, pause=True, style="info"):
12211232
""" Post a message on the screen with Messenger.
12221233
Arguments:
12231234
message: The message to display.
1235+
duration: The time until the message vanishes. (Default: 2.55s)
1236+
pause: If True, the program waits until the message completes.
12241237
style: "info", "success", or "error".
1225-
duration: The time until the message vanishes.
12261238
12271239
You can also post messages by using =>
1228-
self.execute_script('Messenger().post("My Message")') """
1240+
self.execute_script('Messenger().post("My Message")')
1241+
"""
1242+
if not duration:
1243+
if not self.message_duration:
1244+
duration = settings.DEFAULT_MESSAGE_DURATION
1245+
else:
1246+
duration = self.message_duration
1247+
js_utils.post_message(
1248+
self.driver, message, duration, style=style)
1249+
if pause:
1250+
duration = float(duration) + 0.15
1251+
time.sleep(float(duration))
1252+
1253+
def post_success_message(self, message, duration=None, pause=True):
1254+
""" Post a success message on the screen with Messenger.
1255+
Arguments:
1256+
message: The success message to display.
1257+
duration: The time until the message vanishes. (Default: 2.55s)
1258+
pause: If True, the program waits until the message completes.
1259+
"""
1260+
if not duration:
1261+
if not self.message_duration:
1262+
duration = settings.DEFAULT_MESSAGE_DURATION
1263+
else:
1264+
duration = self.message_duration
12291265
js_utils.post_message(
1230-
self.driver, message, self.message_duration,
1231-
style=style, duration=duration)
1266+
self.driver, message, duration, style="success")
1267+
if pause:
1268+
duration = float(duration) + 0.15
1269+
time.sleep(float(duration))
1270+
1271+
def post_error_message(self, message, duration=None, pause=True):
1272+
""" Post an error message on the screen with Messenger.
1273+
Arguments:
1274+
message: The error message to display.
1275+
duration: The time until the message vanishes. (Default: 2.55s)
1276+
pause: If True, the program waits until the message completes.
1277+
"""
1278+
if not duration:
1279+
if not self.message_duration:
1280+
duration = settings.DEFAULT_MESSAGE_DURATION
1281+
else:
1282+
duration = self.message_duration
1283+
js_utils.post_message(
1284+
self.driver, message, duration, style="error")
1285+
if pause:
1286+
duration = float(duration) + 0.15
1287+
time.sleep(float(duration))
12321288

12331289
def get_property_value(self, selector, property, by=By.CSS_SELECTOR,
12341290
timeout=settings.SMALL_TIMEOUT):
@@ -2618,14 +2674,6 @@ def __scroll_to_element(self, element):
26182674
def __slow_scroll_to_element(self, element):
26192675
js_utils.slow_scroll_to_element(self.driver, element, self.browser)
26202676

2621-
def __post_messenger_success_message(self, message, duration=None):
2622-
js_utils.post_messenger_success_message(
2623-
self.driver, message, self.message_duration, duration=duration)
2624-
2625-
def __post_messenger_error_message(self, message, duration=None):
2626-
js_utils.post_messenger_error_message(
2627-
self.driver, message, self.message_duration, duration=duration)
2628-
26292677
def __highlight_with_assert_success(
26302678
self, message, selector, by=By.CSS_SELECTOR):
26312679
selector, by = self.__recalculate_selector(selector, by)

seleniumbase/fixtures/js_utils.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -470,19 +470,17 @@ def set_messenger_theme(driver, theme="default", location="default",
470470
time.sleep(0.1)
471471

472472

473-
def post_message(driver, message, msg_dur, style="info", duration=None):
473+
def post_message(driver, message, msg_dur, style="info"):
474474
""" A helper method to post a message on the screen with Messenger.
475475
(Should only be called from post_message() in base_case.py) """
476-
if not duration:
477-
if not msg_dur:
478-
duration = settings.DEFAULT_MESSAGE_DURATION
479-
else:
480-
duration = msg_dur
476+
if not msg_dur:
477+
msg_dur = settings.DEFAULT_MESSAGE_DURATION
478+
msg_dur = float(msg_dur)
481479
message = re.escape(message)
482480
message = escape_quotes_if_needed(message)
483481
messenger_script = ('''Messenger().post({message: "%s", type: "%s", '''
484482
'''hideAfter: %s, hideOnNavigate: true});'''
485-
% (message, style, duration))
483+
% (message, style, msg_dur))
486484
try:
487485
driver.execute_script(messenger_script)
488486
except Exception:
@@ -499,32 +497,28 @@ def post_message(driver, message, msg_dur, style="info", duration=None):
499497
driver.execute_script(messenger_script)
500498

501499

502-
def post_messenger_success_message(driver, message, msg_dur, duration=None):
503-
if not duration:
504-
if not msg_dur:
505-
duration = settings.DEFAULT_MESSAGE_DURATION
506-
else:
507-
duration = msg_dur
500+
def post_messenger_success_message(driver, message, msg_dur):
501+
if not msg_dur:
502+
msg_dur = settings.DEFAULT_MESSAGE_DURATION
503+
msg_dur = float(msg_dur)
508504
try:
509505
set_messenger_theme(driver, theme="future", location="bottom_right")
510506
post_message(
511-
driver, message, msg_dur, style="success", duration=duration)
512-
time.sleep(duration)
507+
driver, message, msg_dur, style="success")
508+
time.sleep(msg_dur + 0.07)
513509
except Exception:
514510
pass
515511

516512

517-
def post_messenger_error_message(driver, message, msg_dur, duration=None):
518-
if not duration:
519-
if not msg_dur:
520-
duration = settings.DEFAULT_MESSAGE_DURATION
521-
else:
522-
duration = msg_dur
513+
def post_messenger_error_message(driver, message, msg_dur):
514+
if not msg_dur:
515+
msg_dur = settings.DEFAULT_MESSAGE_DURATION
516+
msg_dur = float(msg_dur)
523517
try:
524518
set_messenger_theme(driver, theme="block", location="top_center")
525519
post_message(
526-
driver, message, msg_dur, style="error", duration=duration)
527-
time.sleep(duration)
520+
driver, message, msg_dur, style="error")
521+
time.sleep(msg_dur + 0.07)
528522
except Exception:
529523
pass
530524

0 commit comments

Comments
 (0)