Skip to content

Commit 52991a3

Browse files
committed
Make the standalone driver manager more flexible
1 parent eb9832f commit 52991a3

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

seleniumbase/fixtures/page_actions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,3 +1289,44 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
12891289
plural,
12901290
)
12911291
timeout_exception(Exception, message)
1292+
1293+
1294+
############
1295+
1296+
# Duplicates for easier use without BaseCase
1297+
1298+
def wait_for_element(
1299+
driver,
1300+
selector,
1301+
by="css selector",
1302+
timeout=settings.LARGE_TIMEOUT,
1303+
):
1304+
return wait_for_element_visible(
1305+
driver=driver,
1306+
selector=selector,
1307+
by=by,
1308+
timeout=timeout,
1309+
)
1310+
1311+
1312+
def wait_for_text(
1313+
driver,
1314+
text,
1315+
selector,
1316+
by="css selector",
1317+
timeout=settings.LARGE_TIMEOUT,
1318+
):
1319+
browser = None # Only used for covering a Safari edge case
1320+
try:
1321+
if "safari:platformVersion" in driver.capabilities:
1322+
browser = "safari"
1323+
except Exception:
1324+
pass
1325+
return wait_for_text_visible(
1326+
driver=driver,
1327+
text=text,
1328+
selector=selector,
1329+
by=by,
1330+
timeout=timeout,
1331+
browser=browser,
1332+
)

seleniumbase/plugins/driver_manager.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
from contextlib import contextmanager
1+
"""
2+
The SeleniumBase Driver as a Python Context Manager or a returnable object.
3+
###########################################################################
4+
5+
The SeleniumBase Driver as a context manager:
6+
Usage --> ``with Driver() as driver:``
7+
Usage example -->
8+
from seleniumbase import Driver
9+
with Driver() as driver:
10+
driver.get("https://google.com/ncr")
11+
# The browser exits automatically after the "with" block ends.
12+
13+
###########################################################################
14+
# Above: The driver as a context manager. (Used with a "with" statement.) #
15+
# ----------------------------------------------------------------------- #
16+
# Below: The driver as a returnable object. (Used with "return" command.) #
17+
###########################################################################
18+
19+
The SeleniumBase Driver as a returnable object:
20+
Usage --> ``driver = Driver()``
21+
Usage example -->
22+
from seleniumbase import Driver
23+
driver = Driver()
24+
driver.get("https://google.com/ncr")
25+
26+
###########################################################################
27+
"""
228

329

4-
@contextmanager # Usage: -> ``with Driver() as driver:``
530
def Driver(
631
browser=None, # Choose from "chrome", "edge", "firefox", or "safari".
732
headless=None, # The original headless mode for Chromium and Firefox.
@@ -51,13 +76,6 @@ def Driver(
5176
undetected=None, # Duplicate of "undetectable" to avoid confusion.
5277
uc_sub=None, # Duplicate of "uc_subprocess" to avoid confusion.
5378
):
54-
""" Context Manager for the SeleniumBase Driver Manager.
55-
Usage example:
56-
from seleniumbase import Driver
57-
with Driver() as driver:
58-
driver.get("https://google.com/ncr")
59-
# The browser exits automatically after the "with" block ends.
60-
"""
6179
import sys
6280
from seleniumbase.fixtures import constants
6381

@@ -357,10 +375,4 @@ def Driver(
357375
device_pixel_ratio=d_p_r,
358376
browser=browser_name,
359377
)
360-
try:
361-
yield driver
362-
finally:
363-
try:
364-
driver.quit()
365-
except Exception:
366-
pass
378+
return driver

0 commit comments

Comments
 (0)