Add support for a SeleniumBase Context Manager

This commit is contained in:
Michael Mintz 2022-10-14 03:48:42 -04:00
parent 732667df4b
commit 893b129d29
9 changed files with 926 additions and 9 deletions

View File

@ -7,8 +7,11 @@ from seleniumbase.common import encryption # noqa
from seleniumbase.core.browser_launcher import get_driver # noqa
from seleniumbase.fixtures import js_utils # noqa
from seleniumbase.fixtures import page_actions # noqa
from seleniumbase.fixtures import page_utils # noqa
from seleniumbase.fixtures.base_case import BaseCase # noqa
from seleniumbase.masterqa.master_qa import MasterQA # noqa
from seleniumbase.plugins.sb_manager import SB # noqa
from seleniumbase.plugins.driver_manager import Driver # noqa
if sys.version_info[0] >= 3:
from seleniumbase import translate # noqa

View File

@ -180,6 +180,7 @@ def get_configured_sb(context):
sb.visual_baseline = False
sb.window_size = None
sb.maximize_option = False
sb.is_context_manager = False
sb.save_screenshot_after_test = False
sb.timeout_multiplier = None
sb.pytest_html_report = None
@ -215,6 +216,7 @@ def get_configured_sb(context):
sb.proxy_pac_url = None
sb.swiftshader = False
sb.ad_block_on = False
sb.is_nosetest = False
sb.highlights = None
sb.interval = None
sb.cap_file = None
@ -826,6 +828,10 @@ def get_configured_sb(context):
sb_config.headless = sb.headless
sb_config.headless_active = False
sb_config.headed = sb.headed
sb_config.is_behave = True
sb_config.is_pytest = False
sb_config.is_nosetest = False
sb_config.is_context_manager = False
sb_config.window_size = sb.window_size
sb_config.maximize_option = sb.maximize_option
sb_config.xvfb = sb.xvfb

View File

@ -876,7 +876,7 @@ def validate_proxy_string(proxy_string):
def get_driver(
browser_name,
browser_name=None,
headless=False,
locale_code=None,
use_grid=False,
@ -921,7 +921,19 @@ def get_driver(
device_width=None,
device_height=None,
device_pixel_ratio=None,
browser=None, # A duplicate of browser_name to avoid confusion
):
if not browser_name:
if browser:
browser_name = browser
else:
browser_name = "chrome" # The default if not specified
browser_name = browser_name.lower()
if headless2 and browser_name == constants.Browser.FIREFOX:
headless2 = False # Only for Chromium
headless = True
if uc_subprocess and not undetectable:
undetectable = True
proxy_auth = False
proxy_user = None
proxy_pass = None

View File

@ -330,6 +330,24 @@ def get_test_id(test):
scenario_name = scenario_name.split(" -- @")[0]
test_id = "%s:%s => %s" % (file_name, line_num, scenario_name)
return test_id
elif hasattr(test, "is_context_manager") and test.is_context_manager:
filename = test.__class__.__module__.split(".")[-1] + ".py"
classname = test.__class__.__name__
methodname = test._testMethodName
context_id = None
if filename == "base_case.py" or methodname == "runTest":
import traceback
stack_base = traceback.format_stack()[0].split(", in ")[0]
test_base = stack_base.split(", in ")[0].split(os.sep)[-1]
if hasattr(test, "cm_filename") and test.cm_filename:
filename = test.cm_filename
else:
filename = test_base.split('"')[0]
classname = "SB"
methodname = ".py:" + test_base.split(", line ")[-1]
context_id = filename.split(".")[0] + methodname + ":" + classname
return context_id
test_id = None
try:
test_id = get_test_name(test)

View File

@ -3433,6 +3433,7 @@ class BaseCase(unittest.TestCase):
device_width=d_width,
device_height=d_height,
device_pixel_ratio=d_p_r,
browser=browser_name,
)
self._drivers_list.append(new_driver)
self._drivers_browser_map[new_driver] = browser_name
@ -4571,6 +4572,23 @@ class BaseCase(unittest.TestCase):
filename = self.__get_filename()
classname = self.__class__.__name__
methodname = self._testMethodName
context_filename = None
if (
hasattr(sb_config, "is_context_manager")
and sb_config.is_context_manager
and (filename == "base_case.py" or methodname == "runTest")
):
import traceback
stack_base = traceback.format_stack()[0].split(os.sep)[-1]
test_base = stack_base.split(", in ")[0]
if hasattr(self, "cm_filename") and self.cm_filename:
filename = self.cm_filename
else:
filename = test_base.split('"')[0]
classname = "SB"
methodname = "test_line_" + test_base.split(", line ")[-1]
context_filename = filename.split(".")[0] + "_rec.py"
if hasattr(self, "is_behave") and self.is_behave:
classname = sb_config.behave_feature.name
classname = classname.replace("/", " ").replace(" & ", " ")
@ -4627,6 +4645,8 @@ class BaseCase(unittest.TestCase):
file_name = sb_config.behave_scenario.filename.replace(".", "_")
file_name = file_name.split("/")[-1].split("\\")[-1]
file_name = file_name + "_rec.py"
elif context_filename:
file_name = context_filename
file_path = os.path.join(recordings_folder, file_name)
out_file = codecs.open(file_path, "w+", "utf-8")
out_file.writelines("\r\n".join(data))
@ -13245,6 +13265,11 @@ class BaseCase(unittest.TestCase):
has_exception = False
if hasattr(sys, "last_traceback") and sys.last_traceback is not None:
has_exception = True
elif hasattr(self, "is_context_manager") and self.is_context_manager:
if self.with_testing_base and self._has_failure:
return True
else:
return False
elif python3 and hasattr(self, "_outcome"):
if hasattr(self._outcome, "errors") and self._outcome.errors:
has_exception = True
@ -13277,6 +13302,22 @@ class BaseCase(unittest.TestCase):
scenario_name = scenario_name.replace(" ", "_")
test_id = "%s.%s" % (file_name, scenario_name)
return test_id
elif hasattr(self, "is_context_manager") and self.is_context_manager:
filename = self.__class__.__module__.split(".")[-1] + ".py"
methodname = self._testMethodName
context_id = None
if filename == "base_case.py" or methodname == "runTest":
import traceback
stack_base = traceback.format_stack()[0].split(", in ")[0]
test_base = stack_base.split(", in ")[0].split(os.sep)[-1]
if hasattr(self, "cm_filename") and self.cm_filename:
filename = self.cm_filename
else:
filename = test_base.split('"')[0]
methodname = ".line_" + test_base.split(", line ")[-1]
context_id = filename.split(".")[0] + methodname
return context_id
test_id = "%s.%s.%s" % (
self.__class__.__module__,
self.__class__.__name__,

View File

@ -7,6 +7,7 @@ import ast
import sys
import time
from nose.plugins import Plugin
from seleniumbase import config as sb_config
from seleniumbase.config import settings
from seleniumbase.core import download_helper
from seleniumbase.core import log_helper
@ -200,10 +201,12 @@ class Base(Plugin):
archive_logs = options.archive_logs
log_helper.log_folder_setup(log_path, archive_logs)
download_helper.reset_downloads_folder()
sb_config.is_nosetest = True
if self.report_on:
report_helper.clear_out_old_report_logs(archive_past_runs=False)
def beforeTest(self, test):
sb_config._context_of_runner = False # Context Manager Compatibility
variables = self.options.variables
if variables and type(variables) is str and len(variables) > 0:
bad_input = False

View File

@ -1285,6 +1285,9 @@ def pytest_configure(config):
sb_config.item_count_skipped = 0
sb_config.item_count_untested = 0
sb_config.is_pytest = True
sb_config.is_behave = False
sb_config.is_nosetest = False
sb_config.is_context_manager = False
sb_config.pytest_config = config
sb_config.browser = config.getoption("browser")
if sb_config._browser_shortcut:
@ -1302,7 +1305,10 @@ def pytest_configure(config):
sb_config.device_metrics = config.getoption("device_metrics")
sb_config.headless = config.getoption("headless")
sb_config.headless2 = config.getoption("headless2")
if sb_config.browser not in ["chrome", "edge"]:
if sb_config.headless2 and sb_config.browser == "firefox":
sb_config.headless2 = False # Only for Chromium browsers
sb_config.headless = True # Firefox has regular headless
elif sb_config.browser not in ["chrome", "edge"]:
sb_config.headless2 = False # Only for Chromium browsers
sb_config.headed = config.getoption("headed")
sb_config.xvfb = config.getoption("xvfb")
@ -1624,6 +1630,7 @@ def pytest_collection_finish(session):
"""This runs after item collection is finalized.
https://docs.pytest.org/en/stable/reference.html
"""
sb_config._context_of_runner = False # Context Manager Compatibility
if "--co" in sys_argv or "--collect-only" in sys_argv:
return
if len(session.items) > 0 and not sb_config._multithreaded:
@ -1713,14 +1720,20 @@ def pytest_runtest_teardown(item):
if hasattr(self, "xvfb") and self.xvfb:
if self.headless_active and "--pdb" not in sys_argv:
if hasattr(self, "display") and self.display:
self.headless_active = False
sb_config.headless_active = False
self.display.stop()
elif hasattr(self, "headless") and self.headless:
if self.headless_active and "--pdb" not in sys_argv:
if hasattr(self, "display") and self.display:
self.headless_active = False
sb_config.headless_active = False
self.display.stop()
elif hasattr(self, "headless2") and self.headless2:
if self.headless_active and "--pdb" not in sys_argv:
if hasattr(self, "display") and self.display:
self.headless_active = False
sb_config.headless_active = False
self.display.stop()
except Exception:
pass

View File

@ -0,0 +1,800 @@
from contextlib import contextmanager
@contextmanager # Usage: -> ``with SB() as sb:``
def SB(
test=None, # Test Mode: Output, Logging, Continue on failure unless "rtf".
raise_test_failure=None, # In "test" mode, raise Exception at 1st failure.
rtf=None, # Short form of "raise_test_failure". (Less typing, same thing!)
browser=None, # Choose from "chrome", "edge", "firefox", or "safari".
headless=None, # The original headless mode for Chromium and Firefox.
headless2=None, # Chromium's new headless mode. (Has more features)
locale_code=None, # Set the Language Locale Code for the web browser.
protocol=None, # The Selenium Grid protocol: "http" or "https".
servername=None, # The Selenium Grid server/IP used for tests.
port=None, # The Selenium Grid port used by the test server.
proxy=None, # Use proxy. Format: "SERVER:PORT" or "USER:PASS@SERVER:PORT".
proxy_bypass_list=None, # Skip proxy when using the listed domains.
proxy_pac_url=None, # Use PAC file. (Format: URL or USERNAME:PASSWORD@URL)
agent=None, # Modify the web browser's User-Agent string.
cap_file=None, # The desired capabilities to use with a Selenium Grid.
cap_string=None, # The desired capabilities to use with a Selenium Grid.
recorder_ext=None, # Enables the SeleniumBase Recorder Chromium extension.
disable_js=None, # Disable JavaScript on websites. Pages might break!
disable_csp=None, # Disable the Content Security Policy of websites.
enable_ws=None, # Enable Web Security on Chromium-based browsers.
enable_sync=None, # Enable "Chrome Sync" on websites.
use_auto_ext=None, # Use Chrome's automation extension.
undetectable=None, # Use undetected-chromedriver to evade bot-detection.
uc_subprocess=None, # Use undetected-chromedriver as a subprocess.
incognito=None, # Enable Chromium's Incognito mode.
guest_mode=None, # Enable Chromium's Guest mode.
devtools=None, # Open Chromium's DevTools when the browser opens.
remote_debug=None, # Enable Chrome's Debugger on "http://localhost:9222".
swiftshader=None, # Use Chrome's "--use-gl=swiftshader" feature.
ad_block_on=None, # Block some types of display ads from loading.
block_images=None, # Block images from loading during tests.
do_not_track=None, # Tell websites that you don't want to be tracked.
chromium_arg=None, # "ARG=N,ARG2" (Set Chromium args, ","-separated.)
firefox_arg=None, # "ARG=N,ARG2" (Set Firefox args, comma-separated.)
firefox_pref=None, # SET (Set Firefox PREFERENCE:VALUE set, ","-separated)
user_data_dir=None, # Set the Chrome user data directory to use.
extension_zip=None, # Load a Chrome Extension .zip|.crx, comma-separated.)
extension_dir=None, # Load a Chrome Extension directory, comma-separated.)
page_load_strategy=None, # Set Chrome PLS to "normal", "eager", or "none".
external_pdf=None, # Set Chrome "plugins.always_open_pdf_externally":True.
is_mobile=None, # Use the mobile device emulator while running tests.
device_metrics=None, # Set mobile metrics: "CSSWidth,CSSHeight,PixelRatio"
xvfb=None, # Run tests using the Xvfb virtual display server on Linux OS.
start_page=None, # The starting URL for the web browser when tests begin.
rec_print=None, # If Recorder is enabled, prints output after tests end.
rec_behave=None, # Like Recorder Mode, but also generates behave-gherkin.
record_sleep=None, # If Recorder enabled, also records self.sleep calls.
data=None, # Extra test data. Access with "self.data" in tests.
var1=None, # Extra test data. Access with "self.var1" in tests.
var2=None, # Extra test data. Access with "self.var2" in tests.
var3=None, # Extra test data. Access with "self.var3" in tests.
variables=None, # DICT (Extra test data. Access with "self.variables")
account=None, # Set account. Access with "self.account" in tests.
environment=None, # Set the test env. Access with "self.env" in tests.
headed=None, # Run tests in headed/GUI mode on Linux, where not default.
maximize=None, # Start tests with the browser window maximized.
disable_ws=None, # Reverse of "enable_ws". (None and False are different)
disable_beforeunload=None, # Disable the "beforeunload" event on Chromium.
settings_file=None, # A file for overriding default SeleniumBase settings.
uc=None, # Shortcut / Duplicate of "undetectable" to avoid confusion.
undetected=None, # Duplicate of "undetectable" to avoid confusion.
uc_sub=None, # Duplicate of "uc_subprocess" to avoid confusion.
save_screenshot=None, # Save a screenshot at the end of each test.
timeout_multiplier=None, # Multiplies the default timeout values.
js_checking_on=None, # Check for JavaScript errors after page loads.
slow=None, # Slow down the automation. Faster than using Demo Mode.
demo=None, # Slow down and visually see test actions as they occur.
demo_sleep=None, # SECONDS (Set wait time after Slow & Demo Mode actions.)
message_duration=None, # SECONDS (The time length for Messenger alerts.)
highlights=None, # Number of highlight animations for Demo Mode actions.
interval=None, # SECONDS (Autoplay interval for SB Slides & Tour steps.)
time_limit=None, # SECONDS (Safely fail tests that exceed the time limit.)
):
"""Context Manager for SeleniumBase.
Usage example ->
from seleniumbase import SB
with SB() as sb:
sb.open("https://google.com/ncr")
sb.type('[name="q"]', "SeleniumBase on GitHub\n")
sb.click('a[href*="github.com/seleniumbase"]')
sb.highlight("div.Layout-main")
sb.highlight("div.Layout-sidebar")
sb.sleep(0.5)
# The browser exits automatically after the "with" block ends.
"""
import sys
import time
import traceback
from seleniumbase import BaseCase
from seleniumbase import config as sb_config
from seleniumbase.config import settings
from seleniumbase.fixtures import constants
sb_config_backup = sb_config
sys_argv = sys.argv
archive_logs = False
existing_runner = False
do_log_folder_setup = False # The first "test=True" run does it
if (
(hasattr(sb_config, "is_behave") and sb_config.is_behave)
or (hasattr(sb_config, "is_pytest") and sb_config.is_pytest)
or (hasattr(sb_config, "is_nosetest") and sb_config.is_nosetest)
):
existing_runner = True
test = False # Already using a test runner. Skip extra test steps.
elif test is None and "--test" in sys_argv:
test = True
if (
existing_runner
and not hasattr(sb_config, "_context_of_runner")
):
sb_config._context_of_runner = True
if hasattr(sb_config, "is_pytest") and sb_config.is_pytest:
print(
"\n SB Manager script was triggered by pytest collection!"
'\n (Prevent that by using: `if __name__ == "__main__":`)'
)
elif hasattr(sb_config, "is_nosetest") and sb_config.is_nosetest:
print(
"\n SB Manager script was triggered by nosetest collection!"
'\n (Prevent that by using: ``if __name__ == "__main__":``)'
)
if (
not existing_runner
and not hasattr(sb_config, "_has_older_context")
and test
):
# This is the first "test" from context manager scripts run.
sb_config._has_older_context = True
do_log_folder_setup = True
else:
if test:
pass # Not the first "test" of context manager scripts.
else:
pass # Not in "test" mode. (No special output/logging.)
with_testing_base = False
if test:
with_testing_base = True
if (
raise_test_failure
or rtf
or "--raise-test-failure" in sys_argv
or "--rtf" in sys_argv
or "-x" in sys_argv # Carry-over from "pytest"
or "--exitfirst" in sys_argv # Carry-over from "pytest"
):
raise_test_failure = True # Exit on first error or failed test.
else:
raise_test_failure = False
browser_changes = 0
browser_set = None
browser_text = None
browser_list = []
# As a shortcut, you can use "--edge" instead of "--browser=edge", etc,
# but you can only specify one default browser for tests. (Default: chrome)
if "--browser=chrome" in sys_argv or "--browser chrome" in sys_argv:
browser_changes += 1
browser_set = "chrome"
browser_list.append("--browser=chrome")
if "--browser=edge" in sys_argv or "--browser edge" in sys_argv:
browser_changes += 1
browser_set = "edge"
browser_list.append("--browser=edge")
if "--browser=firefox" in sys_argv or "--browser firefox" in sys_argv:
browser_changes += 1
browser_set = "firefox"
browser_list.append("--browser=firefox")
if "--browser=opera" in sys_argv or "--browser opera" in sys_argv:
browser_changes += 1
browser_set = "opera"
browser_list.append("--browser=opera")
if "--browser=safari" in sys_argv or "--browser safari" in sys_argv:
browser_changes += 1
browser_set = "safari"
browser_list.append("--browser=safari")
if "--browser=ie" in sys_argv or "--browser ie" in sys_argv:
browser_changes += 1
browser_set = "ie"
browser_list.append("--browser=ie")
if "--browser=phantomjs" in sys_argv or "--browser phantomjs" in sys_argv:
browser_changes += 1
browser_set = "phantomjs"
browser_list.append("--browser=phantomjs")
if "--browser=remote" in sys_argv or "--browser remote" in sys_argv:
browser_changes += 1
browser_set = "remote"
browser_list.append("--browser=remote")
browser_text = browser_set
if "--chrome" in sys_argv and not browser_set == "chrome":
browser_changes += 1
browser_text = "chrome"
sb_config._browser_shortcut = "chrome"
browser_list.append("--chrome")
if "--edge" in sys_argv and not browser_set == "edge":
browser_changes += 1
browser_text = "edge"
sb_config._browser_shortcut = "edge"
browser_list.append("--edge")
if "--firefox" in sys_argv and not browser_set == "firefox":
browser_changes += 1
browser_text = "firefox"
sb_config._browser_shortcut = "firefox"
browser_list.append("--firefox")
if "--ie" in sys_argv and not browser_set == "ie":
browser_changes += 1
browser_text = "ie"
sb_config._browser_shortcut = "ie"
browser_list.append("--ie")
if "--opera" in sys_argv and not browser_set == "opera":
browser_changes += 1
browser_text = "opera"
sb_config._browser_shortcut = "opera"
browser_list.append("--opera")
if "--safari" in sys_argv and not browser_set == "safari":
browser_changes += 1
browser_text = "safari"
sb_config._browser_shortcut = "safari"
browser_list.append("--safari")
if browser_changes > 1:
message = "\n\n TOO MANY browser types were entered!"
message += "\n There were %s found:\n > %s" % (
browser_changes,
", ".join(browser_list),
)
message += "\n ONLY ONE default browser is allowed!"
message += "\n Select a single browser & try again!\n"
if not browser:
raise Exception(message)
if browser is None:
if browser_text:
browser = browser_text
else:
browser = "chrome"
else:
browser = browser.lower()
valid_browsers = constants.ValidBrowsers.valid_browsers
if browser not in valid_browsers:
raise Exception(
"Browser: {%s} is not a valid browser option. "
"Valid options = {%s}" % (browser, valid_browsers)
)
if headless is None:
if "--headless" in sys_argv:
headless = True
else:
headless = False
if headless2 is None:
if "--headless2" in sys_argv:
headless2 = True
else:
headless2 = False
if protocol is None:
protocol = "http" # For the Selenium Grid only!
if servername is None:
servername = "localhost" # For the Selenium Grid only!
if port is None:
port = "4444" # For the Selenium Grid only!
if not environment:
environment = "test"
if incognito is None:
if "--incognito" in sys_argv:
incognito = True
else:
incognito = False
if guest_mode is None:
if "--guest" in sys_argv:
guest_mode = True
else:
guest_mode = False
if devtools is None:
if "--devtools" in sys_argv:
devtools = True
else:
devtools = False
if is_mobile is None:
if "--mobile" in sys_argv:
is_mobile = True
else:
is_mobile = False
proxy_string = proxy
user_agent = agent
recorder_mode = False
if recorder_ext:
recorder_mode = True
if (
"--recorder" in sys_argv
or "--record" in sys_argv
or "--rec" in sys_argv
):
recorder_mode = True
recorder_ext = True
if rec_print is None:
if "--rec-print" in sys_argv:
rec_print = True
else:
rec_print = False
if rec_behave is None:
if "--rec-behave" in sys_argv:
rec_behave = True
else:
rec_behave = False
if record_sleep is None:
if "--rec-sleep" in sys_argv or "--record-sleep" in sys_argv:
record_sleep = True
else:
record_sleep = False
if "linux" not in sys.platform:
# The Xvfb virtual display server is for Linux OS Only!
xvfb = False
if (
"linux" in sys.platform
and not headed
and not headless
and not headless2
and not xvfb
):
headless = True
if headless2 and browser == "firefox":
headless2 = False # Only for Chromium browsers
headless = True # Firefox has regular headless
elif browser not in ["chrome", "edge"]:
headless2 = False # Only for Chromium browsers
if not headless and not headless2:
headed = True
if rec_print and not recorder_mode:
recorder_mode = True
recorder_ext = True
elif rec_behave and not recorder_mode:
recorder_mode = True
recorder_ext = True
elif record_sleep and not recorder_mode:
recorder_mode = True
recorder_ext = True
if recorder_mode and headless:
headless = False
headless2 = True
if variables and type(variables) is str and len(variables) > 0:
import ast
bad_input = False
if (
not variables.startswith("{")
or not variables.endswith("}")
):
bad_input = True
else:
try:
variables = ast.literal_eval(variables)
if not type(variables) is dict:
bad_input = True
except Exception:
bad_input = True
if bad_input:
raise Exception(
'\nExpecting a Python dictionary for "variables"!'
"\nEg. --variables=\"{'KEY1':'VALUE', 'KEY2':123}\""
)
else:
variables = {}
if disable_csp is None:
disable_csp = False
if enable_ws is None:
enable_ws = False
if enable_sync is None:
enable_sync = False
if (
(enable_ws is None and disable_ws is None)
or (disable_ws is not None and not disable_ws)
or (enable_ws is not None and enable_ws)
):
enable_ws = True
disable_ws = False
else:
enable_ws = False
disable_ws = True
if undetectable or undetected or uc or uc_subprocess or uc_sub:
undetectable = True
elif (
"--undetectable" in sys_argv
or "--undetected" in sys_argv
or "--uc" in sys_argv
or "--uc-subprocess" in sys_argv
or "--uc_subprocess" in sys_argv
or "--uc-sub" in sys_argv
):
undetectable = True
else:
undetectable = False
if uc_subprocess or uc_sub:
uc_subprocess = True
elif (
"--uc-subprocess" in sys_argv
or "--uc_subprocess" in sys_argv
or "--uc-sub" in sys_argv
):
uc_subprocess = True
else:
uc_subprocess = False
if use_auto_ext is None:
if "--use-auto-ext" in sys_argv:
use_auto_ext = True
else:
use_auto_ext = False
if disable_js is None:
if "--disable-js" in sys_argv:
disable_js = True
else:
disable_js = False
maximize_option = False
if maximize or "--maximize" in sys_argv:
maximize_option = True
_disable_beforeunload = False
if disable_beforeunload:
_disable_beforeunload = True
if page_load_strategy is not None:
if page_load_strategy.lower() not in ["normal", "eager", "none"]:
raise Exception(
'page_load_strategy must be "normal", "eager", or "none"!'
)
page_load_strategy = page_load_strategy.lower()
elif "--pls=normal" in sys_argv or '--pls="normal"' in sys_argv:
page_load_strategy = "normal"
elif "--pls=eager" in sys_argv or '--pls="eager"' in sys_argv:
page_load_strategy = "eager"
elif "--pls=none" in sys_argv or '--pls="none"' in sys_argv:
page_load_strategy = "none"
if (
"--sjw" in sys_argv
or "--skip_js_waits" in sys_argv
or "--skip-js-waits" in sys_argv
):
settings.SKIP_JS_WAITS = True
if save_screenshot is None:
if "--screenshot" in sys_argv or "--save-screenshot" in sys_argv:
save_screenshot = True
else:
save_screenshot = False
if js_checking_on is None:
if "--check-js" in sys_argv:
js_checking_on = True
else:
js_checking_on = False
slow_mode = False
if slow:
slow_mode = True
elif "--slow" in sys_argv:
slow_mode = True
demo_mode = False
if demo:
demo_mode = True
elif "--demo" in sys_argv:
demo_mode = True
if block_images is None:
if "--block-images" in sys_argv:
block_images = True
else:
block_images = False
if do_not_track is None:
if "--do-not-track" in sys_argv:
do_not_track = True
else:
do_not_track = False
if external_pdf is None:
if "--external-pdf" in sys_argv:
external_pdf = True
else:
external_pdf = False
if remote_debug is None:
if "--remote-debug" in sys_argv:
remote_debug = True
else:
remote_debug = False
if swiftshader is None:
if "--swiftshader" in sys_argv:
swiftshader = True
else:
swiftshader = False
if ad_block_on is None:
if "--ad-block" in sys_argv:
ad_block_on = True
else:
ad_block_on = False
if highlights is not None:
try:
highlights = int(highlights)
except Exception:
raise Exception('"highlights" must be an integer!')
if interval is not None:
try:
interval = float(interval)
except Exception:
raise Exception('"interval" must be numeric!')
if time_limit is not None:
try:
time_limit = float(time_limit)
except Exception:
raise Exception('"time_limit" must be numeric!')
sb_config.with_testing_base = with_testing_base
sb_config.browser = browser
if not hasattr(sb_config, "is_behave"):
sb_config.is_behave = False
if not hasattr(sb_config, "is_pytest"):
sb_config.is_pytest = False
if not hasattr(sb_config, "is_nosetest"):
sb_config.is_nosetest = False
sb_config.is_context_manager = True
sb_config.headless = headless
sb_config.headless2 = headless2
sb_config.headed = headed
sb_config.xvfb = xvfb
sb_config.start_page = start_page
sb_config.locale_code = locale_code
sb_config.protocol = protocol
sb_config.servername = servername
sb_config.port = port
sb_config.data = data
sb_config.var1 = var1
sb_config.var2 = var2
sb_config.var3 = var3
sb_config.variables = variables
sb_config.account = account
sb_config.environment = environment
sb_config.env = environment
sb_config.user_agent = user_agent
sb_config.incognito = incognito
sb_config.guest_mode = guest_mode
sb_config.devtools = devtools
sb_config.mobile_emulator = is_mobile
sb_config.device_metrics = device_metrics
sb_config.extension_zip = extension_zip
sb_config.extension_dir = extension_dir
sb_config.database_env = "test"
sb_config.log_path = "latest_logs"
sb_config.archive_logs = archive_logs
sb_config.disable_csp = disable_csp
sb_config.disable_ws = disable_ws
sb_config.enable_ws = enable_ws
sb_config.enable_sync = enable_sync
sb_config.use_auto_ext = use_auto_ext
sb_config.undetectable = undetectable
sb_config.uc_subprocess = uc_subprocess
sb_config.no_sandbox = None
sb_config.disable_gpu = None
sb_config.disable_js = disable_js
sb_config._multithreaded = False
sb_config.reuse_session = False
sb_config.crumbs = False
sb_config.final_debug = False
sb_config.visual_baseline = False
sb_config.window_size = None
sb_config.maximize_option = maximize_option
sb_config._disable_beforeunload = _disable_beforeunload
sb_config.save_screenshot = save_screenshot
sb_config.page_load_strategy = page_load_strategy
sb_config.timeout_multiplier = timeout_multiplier
sb_config.pytest_html_report = None
sb_config.with_db_reporting = False
sb_config.with_s3_logging = False
sb_config.js_checking_on = js_checking_on
sb_config.recorder_mode = recorder_mode
sb_config.recorder_ext = recorder_ext
sb_config.record_sleep = record_sleep
sb_config.rec_behave = rec_behave
sb_config.rec_print = rec_print
sb_config.report_on = False
sb_config.slow_mode = slow_mode
sb_config.demo_mode = demo_mode
sb_config._time_limit = time_limit
sb_config.demo_sleep = demo_sleep
sb_config.dashboard = False
sb_config._dashboard_initialized = False
sb_config.message_duration = message_duration
sb_config.block_images = block_images
sb_config.do_not_track = do_not_track
sb_config.external_pdf = external_pdf
sb_config.remote_debug = remote_debug
sb_config.settings_file = settings_file
sb_config.user_data_dir = user_data_dir
sb_config.chromium_arg = chromium_arg
sb_config.firefox_arg = firefox_arg
sb_config.firefox_pref = firefox_pref
sb_config.proxy_string = proxy_string
sb_config.proxy_bypass_list = proxy_bypass_list
sb_config.proxy_pac_url = proxy_pac_url
sb_config.swiftshader = swiftshader
sb_config.ad_block_on = ad_block_on
sb_config.highlights = highlights
sb_config.interval = interval
sb_config.cap_file = cap_file
sb_config.cap_string = cap_string
sb = BaseCase()
sb.with_testing_base = sb_config.with_testing_base
sb.browser = sb_config.browser
sb.is_behave = False
sb.is_pytest = False
sb.is_nosetest = False
sb.is_context_manager = sb_config.is_context_manager
sb.headless = sb_config.headless
sb.headless2 = sb_config.headless2
sb.headed = sb_config.headed
sb.xvfb = sb_config.xvfb
sb.start_page = sb_config.start_page
sb.locale_code = sb_config.locale_code
sb.protocol = sb_config.protocol
sb.servername = sb_config.servername
sb.port = sb_config.port
sb.data = sb_config.data
sb.var1 = sb_config.var1
sb.var2 = sb_config.var2
sb.var3 = sb_config.var3
sb.variables = sb_config.variables
sb.account = sb_config.account
sb.environment = sb_config.environment
sb.env = sb_config.env
sb.user_agent = sb_config.user_agent
sb.incognito = sb_config.incognito
sb.guest_mode = sb_config.guest_mode
sb.devtools = sb_config.devtools
sb.mobile_emulator = sb_config.mobile_emulator
sb.device_metrics = sb_config.device_metrics
sb.extension_zip = sb_config.extension_zip
sb.extension_dir = sb_config.extension_dir
sb.database_env = sb_config.database_env
sb.log_path = sb_config.log_path
sb.archive_logs = sb_config.archive_logs
sb.disable_csp = sb_config.disable_csp
sb.disable_ws = sb_config.disable_ws
sb.enable_ws = sb_config.enable_ws
sb.enable_sync = sb_config.enable_sync
sb.use_auto_ext = sb_config.use_auto_ext
sb.undetectable = sb_config.undetectable
sb.uc_subprocess = sb_config.uc_subprocess
sb.no_sandbox = sb_config.no_sandbox
sb.disable_gpu = sb_config.disable_gpu
sb.disable_js = sb_config.disable_js
sb._multithreaded = sb_config._multithreaded
sb._reuse_session = sb_config.reuse_session
sb._crumbs = sb_config.crumbs
sb._final_debug = sb_config.final_debug
sb.visual_baseline = sb_config.visual_baseline
sb.window_size = sb_config.window_size
sb.maximize_option = sb_config.maximize_option
sb._disable_beforeunload = sb_config._disable_beforeunload
sb.save_screenshot_after_test = sb_config.save_screenshot
sb.page_load_strategy = sb_config.page_load_strategy
sb.timeout_multiplier = sb_config.timeout_multiplier
sb.pytest_html_report = sb_config.pytest_html_report
sb.with_db_reporting = sb_config.with_db_reporting
sb.with_s3_logging = sb_config.with_s3_logging
sb.js_checking_on = sb_config.js_checking_on
sb.recorder_mode = sb_config.recorder_mode
sb.recorder_ext = sb_config.recorder_ext
sb.record_sleep = sb_config.record_sleep
sb.rec_behave = sb_config.rec_behave
sb.rec_print = sb_config.rec_print
sb.report_on = sb_config.report_on
sb.slow_mode = sb_config.slow_mode
sb.demo_mode = sb_config.demo_mode
sb.time_limit = sb_config._time_limit
sb.demo_sleep = sb_config.demo_sleep
sb.dashboard = sb_config.dashboard
sb._dash_initialized = sb_config._dashboard_initialized
sb.message_duration = sb_config.message_duration
sb.block_images = sb_config.block_images
sb.do_not_track = sb_config.do_not_track
sb.external_pdf = sb_config.external_pdf
sb.remote_debug = sb_config.remote_debug
sb.settings_file = sb_config.settings_file
sb.user_data_dir = sb_config.user_data_dir
sb.chromium_arg = sb_config.chromium_arg
sb.firefox_arg = sb_config.firefox_arg
sb.firefox_pref = sb_config.firefox_pref
sb.proxy_string = sb_config.proxy_string
sb.proxy_bypass_list = sb_config.proxy_bypass_list
sb.proxy_pac_url = sb_config.proxy_pac_url
sb.swiftshader = sb_config.swiftshader
sb.ad_block_on = sb_config.ad_block_on
sb.highlights = sb_config.highlights
sb.interval = sb_config.interval
sb.cap_file = sb_config.cap_file
sb.cap_string = sb_config.cap_string
sb._has_failure = False # This may change
if hasattr(sb_config, "headless_active"):
sb.headless_active = sb_config.headless_active
else:
sb.headless_active = False
test_name = None
if test:
import colorama
import os
colorama.init(autoreset=True)
c1 = colorama.Fore.GREEN
b1 = colorama.Style.BRIGHT
cr = colorama.Style.RESET_ALL
stack_base = traceback.format_stack()[0].split(os.sep)[-1]
test_name = stack_base.split(", in ")[0].replace('", line ', ":")
test_name += ":SB"
terminal_width = os.get_terminal_size()[0]
start_text = "=== {%s} starts ===" % test_name
remaining_spaces = terminal_width - len(start_text)
left_space = ""
right_space = ""
if remaining_spaces > 0:
left_spaces = int(remaining_spaces / 2)
left_space = left_spaces * "="
right_spaces = remaining_spaces - left_spaces
right_space = right_spaces * "="
if not test_name.startswith("runpy.py:"):
print("%s%s%s%s%s" % (b1, left_space, start_text, right_space, cr))
if do_log_folder_setup:
from seleniumbase.core import log_helper
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper
log_helper.log_folder_setup(sb_config.log_path)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()
start_time = time.time()
sb.setUp()
test_passed = True # This can change later
teardown_exception = None
try:
yield sb
except Exception as e:
sb._has_failure = True
exception = e
test_passed = False
if not test_name:
raise
else:
the_traceback = traceback.format_exc().strip()
try:
p2 = the_traceback.split(', in ')[1].split('", line ')[0]
filename = p2.split("/")[-1]
sb.cm_filename = filename
except Exception:
sb.cm_filename = None
# Tests will raise an exception later if "raise_test_failure"
finally:
try:
sb.tearDown()
except Exception as t_e:
teardown_exception = t_e
print(traceback.format_exc().strip())
if test and not test_passed:
print("********** ERROR: The test AND the tearDown() FAILED!")
end_time = time.time()
run_time = end_time - start_time
sb_config = sb_config_backup
if test:
sb_config._has_older_context = True
if existing_runner:
sb_config._context_of_runner = True
if test_name:
result = "passed"
if not test_passed:
result = "failed"
c1 = colorama.Fore.RED
terminal_width = os.get_terminal_size()[0]
end_text = (
"=== {%s} %s in %.2fs ==="
% (test_name, result, run_time)
)
remaining_spaces = terminal_width - len(end_text)
end_text = (
"=== %s%s{%s} %s%s%s in %.2fs ==="
% (b1, c1, test_name, result, cr, c1, run_time)
)
left_space = ""
right_space = ""
if remaining_spaces > 0:
left_spaces = int(remaining_spaces / 2)
left_space = left_spaces * "="
right_spaces = remaining_spaces - left_spaces
right_space = right_spaces * "="
if not test_passed:
print(the_traceback)
if not test_name.startswith("runpy.py:"):
print(
"%s%s%s%s%s"
% (c1, left_space, end_text, right_space, cr)
)
if test and test_name and not test_passed and raise_test_failure:
raise exception
elif (
teardown_exception
and (
not test
or (test_passed and raise_test_failure)
)
):
raise teardown_exception

View File

@ -801,9 +801,12 @@ class SeleniumBrowser(Plugin):
self.enabled = True # Used if test class inherits BaseCase
self.options = options
self.headless_active = False # Default setting
sb_config.headless_active = False
sb_config.is_nosetest = True
proxy_helper.remove_proxy_zip_if_present()
def beforeTest(self, test):
sb_config._context_of_runner = False # Context Manager Compatibility
browser = self.options.browser
if self.options.recorder_mode and browser not in ["chrome", "edge"]:
message = (
@ -836,12 +839,24 @@ class SeleniumBrowser(Plugin):
settings.HEADLESS_START_WIDTH = width
settings.HEADLESS_START_HEIGHT = height
test.test.is_nosetest = True
test.test.is_behave = False
test.test.is_pytest = False
test.test.is_context_manager = False
sb_config.is_nosetest = True
sb_config.is_behave = False
sb_config.is_pytest = False
sb_config.is_context_manager = False
test.test.browser = self.options.browser
test.test.cap_file = self.options.cap_file
test.test.cap_string = self.options.cap_string
test.test.headless = self.options.headless
test.test.headless2 = self.options.headless2
if test.test.browser not in ["chrome", "edge"]:
if test.test.headless2 and test.test.browser == "firefox":
test.test.headless2 = False # Only for Chromium browsers
test.test.headless = True # Firefox has regular headless
self.options.headless2 = False
self.options.headless = True
elif test.test.browser not in ["chrome", "edge"]:
test.test.headless2 = False # Only for Chromium browsers
self.options.headless2 = False
test.test.headed = self.options.headed
@ -901,6 +916,9 @@ class SeleniumBrowser(Plugin):
test.test.enable_sync = self.options.enable_sync
test.test.use_auto_ext = self.options.use_auto_ext
test.test.undetectable = self.options.undetectable
test.test.uc_subprocess = self.options.uc_subprocess
if test.test.uc_subprocess and not test.test.undetectable:
test.test.undetectable = True
test.test.no_sandbox = self.options.no_sandbox
test.test.disable_gpu = self.options.disable_gpu
test.test.remote_debug = self.options.remote_debug
@ -924,12 +942,6 @@ class SeleniumBrowser(Plugin):
# (Set --server="127.0.0.1" for localhost Grid)
if str(self.options.port) == "443":
test.test.protocol = "https"
# Recorder Mode can still optimize scripts in --headless2 mode.
if self.options.recorder_mode and self.options.headless:
self.options.headless = False
self.options.headless2 = True
test.test.headless = False
test.test.headless2 = True
if self.options.xvfb and "linux" not in sys.platform:
# The Xvfb virtual display server is for Linux OS Only!
self.options.xvfb = False
@ -949,6 +961,12 @@ class SeleniumBrowser(Plugin):
)
self.options.headless = True
test.test.headless = True
# Recorder Mode can still optimize scripts in --headless2 mode.
if self.options.recorder_mode and self.options.headless:
self.options.headless = False
self.options.headless2 = True
test.test.headless = False
test.test.headless2 = True
if not self.options.headless and not self.options.headless2:
self.options.headed = True
test.test.headed = True
@ -964,6 +982,7 @@ class SeleniumBrowser(Plugin):
self.display = Display(visible=0, size=(1440, 1880))
self.display.start()
self.headless_active = True
sb_config.headless_active = True
except Exception:
# pyvirtualdisplay might not be necessary anymore because
# Chrome and Firefox now have built-in headless displays
@ -995,6 +1014,8 @@ class SeleniumBrowser(Plugin):
if self.options.headless or self.options.xvfb:
if self.headless_active:
try:
self.headless_active = False
sb_config.headless_active = False
self.display.stop()
except AttributeError:
pass