Make the standalone driver manager more flexible

This commit is contained in:
Michael Mintz 2022-10-20 00:00:04 -04:00
parent eb9832fbdb
commit 52991a3786
2 changed files with 69 additions and 16 deletions

View File

@ -1289,3 +1289,44 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
plural,
)
timeout_exception(Exception, message)
############
# Duplicates for easier use without BaseCase
def wait_for_element(
driver,
selector,
by="css selector",
timeout=settings.LARGE_TIMEOUT,
):
return wait_for_element_visible(
driver=driver,
selector=selector,
by=by,
timeout=timeout,
)
def wait_for_text(
driver,
text,
selector,
by="css selector",
timeout=settings.LARGE_TIMEOUT,
):
browser = None # Only used for covering a Safari edge case
try:
if "safari:platformVersion" in driver.capabilities:
browser = "safari"
except Exception:
pass
return wait_for_text_visible(
driver=driver,
text=text,
selector=selector,
by=by,
timeout=timeout,
browser=browser,
)

View File

@ -1,7 +1,32 @@
from contextlib import contextmanager
"""
The SeleniumBase Driver as a Python Context Manager or a returnable object.
###########################################################################
The SeleniumBase Driver as a context manager:
Usage --> ``with Driver() as driver:``
Usage example -->
from seleniumbase import Driver
with Driver() as driver:
driver.get("https://google.com/ncr")
# The browser exits automatically after the "with" block ends.
###########################################################################
# Above: The driver as a context manager. (Used with a "with" statement.) #
# ----------------------------------------------------------------------- #
# Below: The driver as a returnable object. (Used with "return" command.) #
###########################################################################
The SeleniumBase Driver as a returnable object:
Usage --> ``driver = Driver()``
Usage example -->
from seleniumbase import Driver
driver = Driver()
driver.get("https://google.com/ncr")
###########################################################################
"""
@contextmanager # Usage: -> ``with Driver() as driver:``
def Driver(
browser=None, # Choose from "chrome", "edge", "firefox", or "safari".
headless=None, # The original headless mode for Chromium and Firefox.
@ -51,13 +76,6 @@ def Driver(
undetected=None, # Duplicate of "undetectable" to avoid confusion.
uc_sub=None, # Duplicate of "uc_subprocess" to avoid confusion.
):
""" Context Manager for the SeleniumBase Driver Manager.
Usage example:
from seleniumbase import Driver
with Driver() as driver:
driver.get("https://google.com/ncr")
# The browser exits automatically after the "with" block ends.
"""
import sys
from seleniumbase.fixtures import constants
@ -357,10 +375,4 @@ def Driver(
device_pixel_ratio=d_p_r,
browser=browser_name,
)
try:
yield driver
finally:
try:
driver.quit()
except Exception:
pass
return driver