-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve `GT.save()` usability * Isolate web driver preparation logic from `GT.save()` * Add `quit()` to `_NoOpDriverCtx` * Try to replace `_NoOpDriverCtx` with `no_op_callable()` * Add `from __future__ import annotations` to `_utils_selenium.py` * Introduce `cls_driver` and `cls_wd_options` to WebDriver * Fix wrong `cls_driver` for `_SafariWebDriver` * Fix the type hint for `debug_port` in `_BaseWebDriver` * Add `test__utils_selenium.py` * Remove `**params` from `GT.save()` * Restore the comment about using `PIL` for converting tables into different formats
- Loading branch information
Showing
3 changed files
with
163 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from __future__ import annotations | ||
|
||
from types import TracebackType | ||
from typing import Literal | ||
from typing_extensions import TypeAlias | ||
from selenium import webdriver | ||
|
||
# Create a list of all selenium webdrivers | ||
WebDrivers: TypeAlias = Literal[ | ||
"chrome", | ||
"firefox", | ||
"safari", | ||
"edge", | ||
] | ||
|
||
|
||
class _BaseWebDriver: | ||
|
||
def __init__(self, debug_port: int | None = None): | ||
self.debug_port = debug_port | ||
self.wd_options = self.cls_wd_options() | ||
self.add_arguments() | ||
self.driver = self.cls_driver(self.wd_options) | ||
|
||
def add_arguments(self): ... | ||
|
||
def __enter__(self) -> WebDrivers | webdriver.Remote: | ||
return self.driver | ||
|
||
def __exit__( | ||
self, | ||
exc_type: type[BaseException] | None, | ||
exc_value: BaseException | None, | ||
traceback: TracebackType | None, | ||
) -> bool | None: | ||
self.driver.quit() | ||
|
||
|
||
class _ChromeWebDriver(_BaseWebDriver): | ||
cls_driver = webdriver.Chrome | ||
cls_wd_options = webdriver.ChromeOptions | ||
|
||
def add_arguments(self): | ||
self.wd_options.add_argument("--headless=new") | ||
if self.debug_port is not None: | ||
self.wd_options.add_argument(f"--remote-debugging-port={self.debug_port}") | ||
|
||
|
||
class _SafariWebDriver(_BaseWebDriver): | ||
cls_driver = webdriver.Safari | ||
cls_wd_options = webdriver.SafariOptions | ||
|
||
|
||
class _FirefoxWebDriver(_BaseWebDriver): | ||
cls_driver = webdriver.Firefox | ||
cls_wd_options = webdriver.FirefoxOptions | ||
|
||
def add_arguments(self): | ||
self.wd_options.add_argument("--headless") | ||
if self.debug_port is not None: | ||
self.wd_options.add_argument(f"--start-debugger-server {self.debug_port}") | ||
|
||
|
||
class _EdgeWebDriver(_BaseWebDriver): | ||
cls_driver = webdriver.Edge | ||
cls_wd_options = webdriver.EdgeOptions | ||
|
||
def add_arguments(self): | ||
self.wd_options.add_argument("--headless") | ||
|
||
|
||
def no_op_callable(web_driver: webdriver.Remote): | ||
def wrapper(*args, **kwargs): | ||
return web_driver | ||
|
||
return wrapper | ||
|
||
|
||
def _get_web_driver(web_driver: WebDrivers | webdriver.Remote): | ||
if isinstance(web_driver, webdriver.Remote): | ||
return no_op_callable(web_driver) | ||
elif web_driver == "chrome": | ||
return _ChromeWebDriver | ||
elif web_driver == "safari": | ||
return _SafariWebDriver | ||
elif web_driver == "firefox": | ||
return _FirefoxWebDriver | ||
elif web_driver == "edge": | ||
return _EdgeWebDriver | ||
else: | ||
raise ValueError(f"Unsupported web driver: {web_driver}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
|
||
from great_tables._utils_selenium import ( | ||
_get_web_driver, | ||
no_op_callable, | ||
_ChromeWebDriver, | ||
_SafariWebDriver, | ||
_FirefoxWebDriver, | ||
_EdgeWebDriver, | ||
) | ||
|
||
|
||
def test_no_op_callable(): | ||
""" | ||
The test should cover the scenario of obtaining a remote driver in `_get_web_driver`. | ||
""" | ||
fake_input = object() | ||
f = no_op_callable(fake_input) | ||
assert f(1, x="x") is fake_input | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"web_driver,Driver", | ||
[ | ||
("chrome", _ChromeWebDriver), | ||
("safari", _SafariWebDriver), | ||
("firefox", _FirefoxWebDriver), | ||
("edge", _EdgeWebDriver), | ||
], | ||
) | ||
def test_get_web_driver(web_driver, Driver): | ||
assert _get_web_driver(web_driver) is Driver | ||
|
||
|
||
def test_get_web_driver_raise(): | ||
fake_web_driver = "fake_web_driver" | ||
with pytest.raises(ValueError) as exc_info: | ||
_get_web_driver(fake_web_driver) | ||
assert exc_info.value.args[0] == f"Unsupported web driver: {fake_web_driver}" |