Skip to content

Commit f781162

Browse files
committed
Add "DriverContext" as a Python Context Manager
1 parent f7e7668 commit f781162

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

examples/raw_driver.py renamed to examples/raw_driver_context.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
"""This script can be run with pure "python". (pytest not needed)."""
22
from seleniumbase import js_utils
33
from seleniumbase import page_actions
4-
from seleniumbase import Driver
4+
from seleniumbase import DriverContext
55

6-
# Python Context Manager
7-
with Driver() as driver: # By default, browser="chrome"
8-
driver.get("https://google.com/ncr")
9-
js_utils.highlight_with_js(driver, 'img[alt="Google"]', loops=6)
6+
# Driver Context Manager - (By default, browser="chrome". Lots of options)
7+
with DriverContext() as driver:
8+
driver.get("https://seleniumbase.github.io/")
9+
js_utils.highlight_with_js(driver, 'img[alt="SeleniumBase"]', loops=6)
1010

11-
with Driver() as driver: # Also accepts command-line options
11+
with DriverContext() as driver:
1212
driver.get("https://seleniumbase.github.io/demo_page")
1313
js_utils.highlight_with_js(driver, "h2", loops=5)
1414
by_css = "css selector"
1515
driver.find_element(by_css, "#myTextInput").send_keys("Automation")
1616
driver.find_element(by_css, "#checkBox1").click()
1717
js_utils.highlight_with_js(driver, "img", loops=5)
1818

19-
# Python Context Manager (with options given)
20-
with Driver(browser="chrome", incognito=True) as driver:
19+
with DriverContext(browser="chrome", incognito=True) as driver:
2120
driver.get("https://seleniumbase.io/apps/calculator")
2221
page_actions.wait_for_element(driver, "4", "id").click()
2322
page_actions.wait_for_element(driver, "2", "id").click()

help_docs/syntax_formats.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -821,31 +821,30 @@ This pure Python format gives you a raw <code>webdriver</code> instance in a <co
821821
"""This script can be run with pure "python". (pytest not needed)."""
822822
from seleniumbase import js_utils
823823
from seleniumbase import page_actions
824-
from seleniumbase import Driver
824+
from seleniumbase import DriverContext
825825

826-
# Python Context Manager
827-
with Driver() as driver: # By default, browser="chrome"
828-
driver.get("https://google.com/ncr")
829-
js_utils.highlight_with_js(driver, 'img[alt="Google"]', loops=6)
826+
# Driver Context Manager - (By default, browser="chrome". Lots of options)
827+
with DriverContext() as driver:
828+
driver.get("https://seleniumbase.github.io/")
829+
js_utils.highlight_with_js(driver, 'img[alt="SeleniumBase"]', loops=6)
830830

831-
with Driver() as driver: # Also accepts command-line options
831+
with DriverContext() as driver:
832832
driver.get("https://seleniumbase.github.io/demo_page")
833833
js_utils.highlight_with_js(driver, "h2", loops=5)
834834
by_css = "css selector"
835835
driver.find_element(by_css, "#myTextInput").send_keys("Automation")
836836
driver.find_element(by_css, "#checkBox1").click()
837837
js_utils.highlight_with_js(driver, "img", loops=5)
838838

839-
# Python Context Manager (with options given)
840-
with Driver(browser="chrome", incognito=True) as driver:
839+
with DriverContext(browser="chrome", incognito=True) as driver:
841840
driver.get("https://seleniumbase.io/apps/calculator")
842841
page_actions.wait_for_element(driver, "4", "id").click()
843842
page_actions.wait_for_element(driver, "2", "id").click()
844843
page_actions.wait_for_text(driver, "42", "output", "id")
845844
js_utils.highlight_with_js(driver, "#output", loops=6)
846845
```
847846

848-
(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_driver.py">examples/raw_driver.py</a> for an example.)
847+
(See <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_driver_context.py">examples/raw_driver_context.py</a> for an example.)
849848

850849
<a id="sb_sf_23"></a>
851850
<h3><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 23. The driver manager (via direct import)</h3>

seleniumbase/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from seleniumbase.masterqa.master_qa import MasterQA # noqa
1313
from seleniumbase.plugins.sb_manager import SB # noqa
1414
from seleniumbase.plugins.driver_manager import Driver # noqa
15+
from seleniumbase.plugins.driver_manager import DriverContext # noqa
1516

1617
if sys.version_info[0] >= 3:
1718
from seleniumbase import translate # noqa

seleniumbase/plugins/driver_manager.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
###########################################################################
44
55
The SeleniumBase Driver as a context manager:
6-
Usage --> ``with Driver() as driver:``
6+
Usage --> ``with DriverContext() as driver:``
77
Usage example -->
88
from seleniumbase import Driver
9-
with Driver() as driver:
9+
with DriverContext() as driver:
1010
driver.get("https://google.com/ncr")
1111
# The browser exits automatically after the "with" block ends.
1212
@@ -25,6 +25,30 @@
2525
2626
###########################################################################
2727
"""
28+
import sys
29+
30+
31+
class DriverContext():
32+
def __init__(self, *args, **kwargs):
33+
self.driver = Driver(*args, **kwargs)
34+
35+
def __enter__(self):
36+
return self.driver
37+
38+
def __exit__(self, exc_type, exc_val, exc_tb):
39+
try:
40+
if (
41+
hasattr(self, "driver")
42+
and hasattr(self.driver, "quit")
43+
and (
44+
sys.platform not in ["win32", "win64", "x64"]
45+
or self.driver.service.process
46+
)
47+
):
48+
self.driver.quit()
49+
except Exception:
50+
pass
51+
return False
2852

2953

3054
def Driver(
@@ -82,7 +106,6 @@ def Driver(
82106
wire=None, # Shortcut / Duplicate of "use_wire".
83107
pls=None, # Shortcut / Duplicate of "page_load_strategy".
84108
):
85-
import sys
86109
from seleniumbase.fixtures import constants
87110

88111
sys_argv = sys.argv

0 commit comments

Comments
 (0)