Skip to content

Commit aeed133

Browse files
authored
Merge pull request #268 from seleniumbase/improve-test-messages
Improve Messenger methods and a few other things
2 parents 6501c3f + 373ccab commit aeed133

File tree

7 files changed

+147
-45
lines changed

7 files changed

+147
-45
lines changed

examples/raw_parameter_script.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
""" The main purpose of this file is to demonstrate running SeleniumBase
2+
scripts without the use of Pytest by calling the script directly
3+
with Python or from a Python interactive interpreter. Based on
4+
whether relative imports work or don't, the script can autodetect
5+
how this file was run. With pure Python, it will initialize
6+
all the variables that would've been automatically initialized
7+
by the Pytest plugin. The setUp() and tearDown() methods are also
8+
now called from the script itself.
9+
10+
One big advantage to running tests with Pytest is that most of this
11+
is done for you automatically, with the option to update any of the
12+
parameters through command line parsing. Pytest also provides you
13+
with other plugins, such as ones for generating test reports,
14+
handling multithreading, and parametrized tests. Depending on your
15+
specific needs, you may need to call SeleniumBase commands without
16+
using Pytest, and this example shows you how. """
17+
18+
try:
19+
# Running with Pytest / (Finds test methods to run using autodiscovery)
20+
# Example run command: "pytest raw_parameter_script.py"
21+
from .my_first_test import MyTestClass # (relative imports work: ".~")
22+
23+
except (ImportError, ValueError):
24+
# Running with pure Python OR from a Python interactive interpreter
25+
# Example run command: "python raw_parameter_script.py"
26+
from my_first_test import MyTestClass # (relative imports DON'T work)
27+
28+
b = MyTestClass("test_basic")
29+
b.browser = "chrome"
30+
b.headless = False
31+
b.servername = "localhost"
32+
b.port = 4444
33+
b.data = None
34+
b.environment = "test"
35+
b.database_env = "test"
36+
b.log_path = "latest_logs/"
37+
b.timeout_multiplier = None
38+
b.with_db_reporting = False
39+
b.with_s3_logging = False
40+
b.js_checking_on = False
41+
b.is_pytest = False
42+
b.demo_mode = False
43+
b.demo_sleep = 1
44+
b.message_duration = 2
45+
b.proxy_string = None
46+
b.ad_block_on = False
47+
b.highlights = None
48+
b.check_js = False
49+
b.cap_file = None
50+
51+
b.setUp()
52+
try:
53+
b.test_basic()
54+
finally:
55+
b.tearDown()
56+
del b

help_docs/method_summary.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ self.activate_jquery_confirm()
120120

121121
self.activate_messenger()
122122

123-
self.post_message(message, style="info", duration=None)
123+
self.post_message(message, duration=None, pause=True, style="info")
124+
125+
self.post_success_message(message, duration=None, pause=True)
126+
127+
self.post_error_message(message, duration=None, pause=True)
124128

125129
self.set_messenger_theme(theme="default", location="default",
126130
max_messages="default")

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ requests==2.21.0
1010
urllib3==1.24.1
1111
pytest>=4.1.1
1212
pytest-cov>=2.6.1
13-
pytest-html>=1.19.0
13+
pytest-html>=1.20.0
1414
pytest-rerunfailures>=6.0
1515
pytest-xdist>=1.26.0
1616
parameterized==0.6.1

seleniumbase/core/tour_helper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0):
239239
except Exception:
240240
js_utils.post_messenger_error_message(
241241
driver, "Tour Error: {'%s'} was not found!" % selector,
242-
msg_dur, duration=settings.SMALL_TIMEOUT)
242+
msg_dur)
243243
raise Exception(
244244
"Tour Error: {'%s'} was not found! "
245245
"Exiting due to failure on first tour step!"
@@ -308,7 +308,7 @@ def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0):
308308
driver.execute_script(remove_script)
309309
js_utils.post_messenger_error_message(
310310
driver, "Tour Error: {'%s'} was not found!" % selector,
311-
msg_dur, duration=settings.SMALL_TIMEOUT)
311+
msg_dur)
312312
time.sleep(0.1)
313313
driver.execute_script("Shepherd.activeTour.next()")
314314
if autoplay:
@@ -362,7 +362,7 @@ def play_bootstrap_tour(
362362
except Exception:
363363
js_utils.post_messenger_error_message(
364364
driver, "Tour Error: {'%s'} was not found!" % selector,
365-
msg_dur, duration=settings.SMALL_TIMEOUT)
365+
msg_dur)
366366
raise Exception(
367367
"Tour Error: {'%s'} was not found! "
368368
"Exiting due to failure on first tour step!"
@@ -442,7 +442,7 @@ def play_hopscotch_tour(
442442
except Exception:
443443
js_utils.post_messenger_error_message(
444444
driver, "Tour Error: {'%s'} was not found!" % selector,
445-
msg_dur, duration=settings.SMALL_TIMEOUT)
445+
msg_dur)
446446
raise Exception(
447447
"Tour Error: {'%s'} was not found! "
448448
"Exiting due to failure on first tour step!"
@@ -561,7 +561,7 @@ def play_introjs_tour(
561561
except Exception:
562562
js_utils.post_messenger_error_message(
563563
driver, "Tour Error: {'%s'} was not found!" % selector,
564-
msg_dur, duration=settings.SMALL_TIMEOUT)
564+
msg_dur)
565565
raise Exception(
566566
"Tour Error: {'%s'} was not found! "
567567
"Exiting due to failure on first tour step!"

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

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.17.22',
20+
version='1.18.0',
2121
description='Reliable Browser Automation & Testing Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',
@@ -63,7 +63,7 @@
6363
'urllib3==1.24.1', # Keep this lib in sync with "requests"
6464
'pytest>=4.1.1',
6565
'pytest-cov>=2.6.1',
66-
'pytest-html>=1.19.0',
66+
'pytest-html>=1.20.0',
6767
'pytest-rerunfailures>=6.0',
6868
'pytest-xdist>=1.26.0',
6969
'parameterized==0.6.1',

0 commit comments

Comments
 (0)