Skip to content

Improve Messenger methods and a few other things #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions examples/raw_parameter_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
""" The main purpose of this file is to demonstrate running SeleniumBase
scripts without the use of Pytest by calling the script directly
with Python or from a Python interactive interpreter. Based on
whether relative imports work or don't, the script can autodetect
how this file was run. With pure Python, it will initialize
all the variables that would've been automatically initialized
by the Pytest plugin. The setUp() and tearDown() methods are also
now called from the script itself.

One big advantage to running tests with Pytest is that most of this
is done for you automatically, with the option to update any of the
parameters through command line parsing. Pytest also provides you
with other plugins, such as ones for generating test reports,
handling multithreading, and parametrized tests. Depending on your
specific needs, you may need to call SeleniumBase commands without
using Pytest, and this example shows you how. """

try:
# Running with Pytest / (Finds test methods to run using autodiscovery)
# Example run command: "pytest raw_parameter_script.py"
from .my_first_test import MyTestClass # (relative imports work: ".~")

except (ImportError, ValueError):
# Running with pure Python OR from a Python interactive interpreter
# Example run command: "python raw_parameter_script.py"
from my_first_test import MyTestClass # (relative imports DON'T work)

b = MyTestClass("test_basic")
b.browser = "chrome"
b.headless = False
b.servername = "localhost"
b.port = 4444
b.data = None
b.environment = "test"
b.database_env = "test"
b.log_path = "latest_logs/"
b.timeout_multiplier = None
b.with_db_reporting = False
b.with_s3_logging = False
b.js_checking_on = False
b.is_pytest = False
b.demo_mode = False
b.demo_sleep = 1
b.message_duration = 2
b.proxy_string = None
b.ad_block_on = False
b.highlights = None
b.check_js = False
b.cap_file = None

b.setUp()
try:
b.test_basic()
finally:
b.tearDown()
del b
6 changes: 5 additions & 1 deletion help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ self.activate_jquery_confirm()

self.activate_messenger()

self.post_message(message, style="info", duration=None)
self.post_message(message, duration=None, pause=True, style="info")

self.post_success_message(message, duration=None, pause=True)

self.post_error_message(message, duration=None, pause=True)

self.set_messenger_theme(theme="default", location="default",
max_messages="default")
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ requests==2.21.0
urllib3==1.24.1
pytest>=4.1.1
pytest-cov>=2.6.1
pytest-html>=1.19.0
pytest-html>=1.20.0
pytest-rerunfailures>=6.0
pytest-xdist>=1.26.0
parameterized==0.6.1
Expand Down
10 changes: 5 additions & 5 deletions seleniumbase/core/tour_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0):
except Exception:
js_utils.post_messenger_error_message(
driver, "Tour Error: {'%s'} was not found!" % selector,
msg_dur, duration=settings.SMALL_TIMEOUT)
msg_dur)
raise Exception(
"Tour Error: {'%s'} was not found! "
"Exiting due to failure on first tour step!"
Expand Down Expand Up @@ -308,7 +308,7 @@ def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0):
driver.execute_script(remove_script)
js_utils.post_messenger_error_message(
driver, "Tour Error: {'%s'} was not found!" % selector,
msg_dur, duration=settings.SMALL_TIMEOUT)
msg_dur)
time.sleep(0.1)
driver.execute_script("Shepherd.activeTour.next()")
if autoplay:
Expand Down Expand Up @@ -362,7 +362,7 @@ def play_bootstrap_tour(
except Exception:
js_utils.post_messenger_error_message(
driver, "Tour Error: {'%s'} was not found!" % selector,
msg_dur, duration=settings.SMALL_TIMEOUT)
msg_dur)
raise Exception(
"Tour Error: {'%s'} was not found! "
"Exiting due to failure on first tour step!"
Expand Down Expand Up @@ -442,7 +442,7 @@ def play_hopscotch_tour(
except Exception:
js_utils.post_messenger_error_message(
driver, "Tour Error: {'%s'} was not found!" % selector,
msg_dur, duration=settings.SMALL_TIMEOUT)
msg_dur)
raise Exception(
"Tour Error: {'%s'} was not found! "
"Exiting due to failure on first tour step!"
Expand Down Expand Up @@ -561,7 +561,7 @@ def play_introjs_tour(
except Exception:
js_utils.post_messenger_error_message(
driver, "Tour Error: {'%s'} was not found!" % selector,
msg_dur, duration=settings.SMALL_TIMEOUT)
msg_dur)
raise Exception(
"Tour Error: {'%s'} was not found! "
"Exiting due to failure on first tour step!"
Expand Down
74 changes: 61 additions & 13 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,22 +1213,78 @@ def activate_messenger(self):

def set_messenger_theme(self, theme="default", location="default",
max_messages="default"):
""" Sets a theme for posting messages.
Themes: ["flat", "future", "block", "air", "ice"]
Locations: ["top_left", "top_center", "top_right",
"bottom_left", "bottom_center", "bottom_right"]
max_messages is the limit of concurrent messages to display. """
if not theme:
theme = "default" # "future"
if not location:
location = "default" # "bottom_right"
if not max_messages:
max_messages = "default" # "8"
js_utils.set_messenger_theme(
self.driver, theme=theme,
location=location, max_messages=max_messages)

def post_message(self, message, style="info", duration=None):
def post_message(self, message, duration=None, pause=True, style="info"):
""" Post a message on the screen with Messenger.
Arguments:
message: The message to display.
duration: The time until the message vanishes. (Default: 2.55s)
pause: If True, the program waits until the message completes.
style: "info", "success", or "error".
duration: The time until the message vanishes.

You can also post messages by using =>
self.execute_script('Messenger().post("My Message")') """
self.execute_script('Messenger().post("My Message")')
"""
if not duration:
if not self.message_duration:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = self.message_duration
js_utils.post_message(
self.driver, message, duration, style=style)
if pause:
duration = float(duration) + 0.15
time.sleep(float(duration))

def post_success_message(self, message, duration=None, pause=True):
""" Post a success message on the screen with Messenger.
Arguments:
message: The success message to display.
duration: The time until the message vanishes. (Default: 2.55s)
pause: If True, the program waits until the message completes.
"""
if not duration:
if not self.message_duration:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = self.message_duration
js_utils.post_message(
self.driver, message, self.message_duration,
style=style, duration=duration)
self.driver, message, duration, style="success")
if pause:
duration = float(duration) + 0.15
time.sleep(float(duration))

def post_error_message(self, message, duration=None, pause=True):
""" Post an error message on the screen with Messenger.
Arguments:
message: The error message to display.
duration: The time until the message vanishes. (Default: 2.55s)
pause: If True, the program waits until the message completes.
"""
if not duration:
if not self.message_duration:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = self.message_duration
js_utils.post_message(
self.driver, message, duration, style="error")
if pause:
duration = float(duration) + 0.15
time.sleep(float(duration))

def get_property_value(self, selector, property, by=By.CSS_SELECTOR,
timeout=settings.SMALL_TIMEOUT):
Expand Down Expand Up @@ -2618,14 +2674,6 @@ def __scroll_to_element(self, element):
def __slow_scroll_to_element(self, element):
js_utils.slow_scroll_to_element(self.driver, element, self.browser)

def __post_messenger_success_message(self, message, duration=None):
js_utils.post_messenger_success_message(
self.driver, message, self.message_duration, duration=duration)

def __post_messenger_error_message(self, message, duration=None):
js_utils.post_messenger_error_message(
self.driver, message, self.message_duration, duration=duration)

def __highlight_with_assert_success(
self, message, selector, by=By.CSS_SELECTOR):
selector, by = self.__recalculate_selector(selector, by)
Expand Down
40 changes: 17 additions & 23 deletions seleniumbase/fixtures/js_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,19 +470,17 @@ def set_messenger_theme(driver, theme="default", location="default",
time.sleep(0.1)


def post_message(driver, message, msg_dur, style="info", duration=None):
def post_message(driver, message, msg_dur, style="info"):
""" A helper method to post a message on the screen with Messenger.
(Should only be called from post_message() in base_case.py) """
if not duration:
if not msg_dur:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = msg_dur
if not msg_dur:
msg_dur = settings.DEFAULT_MESSAGE_DURATION
msg_dur = float(msg_dur)
message = re.escape(message)
message = escape_quotes_if_needed(message)
messenger_script = ('''Messenger().post({message: "%s", type: "%s", '''
'''hideAfter: %s, hideOnNavigate: true});'''
% (message, style, duration))
% (message, style, msg_dur))
try:
driver.execute_script(messenger_script)
except Exception:
Expand All @@ -499,32 +497,28 @@ def post_message(driver, message, msg_dur, style="info", duration=None):
driver.execute_script(messenger_script)


def post_messenger_success_message(driver, message, msg_dur, duration=None):
if not duration:
if not msg_dur:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = msg_dur
def post_messenger_success_message(driver, message, msg_dur):
if not msg_dur:
msg_dur = settings.DEFAULT_MESSAGE_DURATION
msg_dur = float(msg_dur)
try:
set_messenger_theme(driver, theme="future", location="bottom_right")
post_message(
driver, message, msg_dur, style="success", duration=duration)
time.sleep(duration)
driver, message, msg_dur, style="success")
time.sleep(msg_dur + 0.07)
except Exception:
pass


def post_messenger_error_message(driver, message, msg_dur, duration=None):
if not duration:
if not msg_dur:
duration = settings.DEFAULT_MESSAGE_DURATION
else:
duration = msg_dur
def post_messenger_error_message(driver, message, msg_dur):
if not msg_dur:
msg_dur = settings.DEFAULT_MESSAGE_DURATION
msg_dur = float(msg_dur)
try:
set_messenger_theme(driver, theme="block", location="top_center")
post_message(
driver, message, msg_dur, style="error", duration=duration)
time.sleep(duration)
driver, message, msg_dur, style="error")
time.sleep(msg_dur + 0.07)
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setup(
name='seleniumbase',
version='1.17.22',
version='1.18.0',
description='Reliable Browser Automation & Testing Framework',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down Expand Up @@ -63,7 +63,7 @@
'urllib3==1.24.1', # Keep this lib in sync with "requests"
'pytest>=4.1.1',
'pytest-cov>=2.6.1',
'pytest-html>=1.19.0',
'pytest-html>=1.20.0',
'pytest-rerunfailures>=6.0',
'pytest-xdist>=1.26.0',
'parameterized==0.6.1',
Expand Down