Skip to content

Commit

Permalink
Add "DriverContext" as a Python Context Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmintz committed Nov 5, 2022
1 parent f7e7668 commit f781162
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
15 changes: 7 additions & 8 deletions examples/raw_driver.py → examples/raw_driver_context.py
@@ -1,23 +1,22 @@
"""This script can be run with pure "python". (pytest not needed)."""
from seleniumbase import js_utils
from seleniumbase import page_actions
from seleniumbase import Driver
from seleniumbase import DriverContext

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

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

# Python Context Manager (with options given)
with Driver(browser="chrome", incognito=True) as driver:
with DriverContext(browser="chrome", incognito=True) as driver:
driver.get("https://seleniumbase.io/apps/calculator")
page_actions.wait_for_element(driver, "4", "id").click()
page_actions.wait_for_element(driver, "2", "id").click()
Expand Down
17 changes: 8 additions & 9 deletions help_docs/syntax_formats.md
Expand Up @@ -821,31 +821,30 @@ This pure Python format gives you a raw <code>webdriver</code> instance in a <co
"""This script can be run with pure "python". (pytest not needed)."""
from seleniumbase import js_utils
from seleniumbase import page_actions
from seleniumbase import Driver
from seleniumbase import DriverContext

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

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

# Python Context Manager (with options given)
with Driver(browser="chrome", incognito=True) as driver:
with DriverContext(browser="chrome", incognito=True) as driver:
driver.get("https://seleniumbase.io/apps/calculator")
page_actions.wait_for_element(driver, "4", "id").click()
page_actions.wait_for_element(driver, "2", "id").click()
page_actions.wait_for_text(driver, "42", "output", "id")
js_utils.highlight_with_js(driver, "#output", loops=6)
```

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

<a id="sb_sf_23"></a>
<h3><img src="https://seleniumbase.github.io/img/logo3b.png" title="SeleniumBase" width="32" /> 23. The driver manager (via direct import)</h3>
Expand Down
1 change: 1 addition & 0 deletions seleniumbase/__init__.py
Expand Up @@ -12,6 +12,7 @@
from seleniumbase.masterqa.master_qa import MasterQA # noqa
from seleniumbase.plugins.sb_manager import SB # noqa
from seleniumbase.plugins.driver_manager import Driver # noqa
from seleniumbase.plugins.driver_manager import DriverContext # noqa

if sys.version_info[0] >= 3:
from seleniumbase import translate # noqa
Expand Down
29 changes: 26 additions & 3 deletions seleniumbase/plugins/driver_manager.py
Expand Up @@ -3,10 +3,10 @@
###########################################################################
The SeleniumBase Driver as a context manager:
Usage --> ``with Driver() as driver:``
Usage --> ``with DriverContext() as driver:``
Usage example -->
from seleniumbase import Driver
with Driver() as driver:
with DriverContext() as driver:
driver.get("https://google.com/ncr")
# The browser exits automatically after the "with" block ends.
Expand All @@ -25,6 +25,30 @@
###########################################################################
"""
import sys


class DriverContext():
def __init__(self, *args, **kwargs):
self.driver = Driver(*args, **kwargs)

def __enter__(self):
return self.driver

def __exit__(self, exc_type, exc_val, exc_tb):
try:
if (
hasattr(self, "driver")
and hasattr(self.driver, "quit")
and (
sys.platform not in ["win32", "win64", "x64"]
or self.driver.service.process
)
):
self.driver.quit()
except Exception:
pass
return False


def Driver(
Expand Down Expand Up @@ -82,7 +106,6 @@ def Driver(
wire=None, # Shortcut / Duplicate of "use_wire".
pls=None, # Shortcut / Duplicate of "page_load_strategy".
):
import sys
from seleniumbase.fixtures import constants

sys_argv = sys.argv
Expand Down

0 comments on commit f781162

Please sign in to comment.