Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove selenium-wire #332

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ def _override_pylenium_config_values(_load_pylenium_json: PyleniumConfig, reques
# with double quotes around each key. booleans are lowercase.
config.driver.capabilities = json.loads(cli_capabilities)

cli_wire_enabled = request.config.getoption("--wire_enabled")
if cli_wire_enabled:
# --wire_enabled is false unless they specify "true"
wire_enabled = cli_wire_enabled.lower() == "true"
config.driver.seleniumwire_enabled = wire_enabled

cli_wire_options = request.config.getoption("--wire_options")
if cli_wire_options:
# --wire_options must be in '{"name": "value", "boolean": true}' format
# with double quotes around each key. booleans are lowercase.
config.driver.seleniumwire_options = json.loads(cli_wire_options)

cli_page_wait_time = request.config.getoption("--page_load_wait_time")
if cli_page_wait_time and cli_page_wait_time.isdigit():
config.driver.page_load_wait_time = int(cli_page_wait_time)
Expand Down Expand Up @@ -347,15 +335,3 @@ def pytest_addoption(parser):
help="The amount of time to wait for a page load before raising an error. Default is 0.",
)
parser.addoption("--extensions", action="store", default="", help='Comma-separated list of extension paths. Ex. "*.crx, *.crx"')
parser.addoption(
"--wire_enabled",
action="store",
default=False,
help="Should the Wire Protocol be enabled? true | false",
)
parser.addoption(
"--wire_options",
action="store",
default="",
help='Dict of key-value pairs as a string. Ex. \'{"name": "value", "boolean": true}\'',
)
413 changes: 57 additions & 356 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions pylenium.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"experimental_options": null,
"extension_paths": [],
"webdriver_kwargs": {},
"seleniumwire_enabled": false,
"seleniumwire_options": {},
"local_path": ""
},
"logging": {
Expand Down
2 changes: 0 additions & 2 deletions pylenium/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class DriverConfig(BaseModel):
options: List[str] = []
capabilities: Dict = {}
experimental_options: Optional[List[Dict]] = None
seleniumwire_enabled: bool = False
seleniumwire_options: Dict = {}
extension_paths: Optional[List[str]] = None
webdriver_kwargs: Optional[Dict] = None
local_path: str = ""
Expand Down
24 changes: 0 additions & 24 deletions pylenium/scripts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ def _override_pylenium_config_values(_load_pylenium_json: PyleniumConfig, reques
# with double quotes around each key. booleans are lowercase.
config.driver.capabilities = json.loads(cli_capabilities)

cli_wire_enabled = request.config.getoption("--wire_enabled")
if cli_wire_enabled:
# --wire_enabled is false unless they specify "true"
wire_enabled = cli_wire_enabled.lower() == "true"
config.driver.seleniumwire_enabled = wire_enabled

cli_wire_options = request.config.getoption("--wire_options")
if cli_wire_options:
# --wire_options must be in '{"name": "value", "boolean": true}' format
# with double quotes around each key. booleans are lowercase.
config.driver.seleniumwire_options = json.loads(cli_wire_options)

cli_page_wait_time = request.config.getoption("--page_load_wait_time")
if cli_page_wait_time and cli_page_wait_time.isdigit():
config.driver.page_load_wait_time = int(cli_page_wait_time)
Expand Down Expand Up @@ -347,15 +335,3 @@ def pytest_addoption(parser):
help="The amount of time to wait for a page load before raising an error. Default is 0.",
)
parser.addoption("--extensions", action="store", default="", help='Comma-separated list of extension paths. Ex. "*.crx, *.crx"')
parser.addoption(
"--wire_enabled",
action="store",
default=False,
help="Should the Wire Protocol be enabled? true | false",
)
parser.addoption(
"--wire_options",
action="store",
default="",
help='Dict of key-value pairs as a string. Ex. \'{"name": "value", "boolean": true}\'',
)
2 changes: 0 additions & 2 deletions pylenium/scripts/pylenium.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"experimental_options": null,
"extension_paths": [],
"webdriver_kwargs": {},
"seleniumwire_enabled": false,
"seleniumwire_options": {},
"local_path": ""
},
"logging": {
Expand Down
64 changes: 14 additions & 50 deletions pylenium/webdriver_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
The Pylenium class asks for a PyleniumConfig object to build a WebDriver,
so the `build_from_config` method is the "main" method in this module.
"""

from typing import Dict, List, Optional

from selenium import webdriver
Expand All @@ -12,7 +13,6 @@
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.safari.service import Service as SafariService
from selenium.webdriver.remote.webdriver import WebDriver
from seleniumwire import webdriver as wire_driver

from pylenium.config import PyleniumConfig

Expand Down Expand Up @@ -123,7 +123,6 @@ def build_from_config(config: PyleniumConfig) -> WebDriver:
"""
browser = config.driver.browser.lower()
remote_url = config.driver.remote_url
seleniumwire_enabled = config.driver.seleniumwire_enabled
_config = {
"options": config.driver.options,
"capabilities": config.driver.capabilities,
Expand All @@ -139,23 +138,15 @@ def build_from_config(config: PyleniumConfig) -> WebDriver:
# Set fields for the rest of the non-remote drivers
_config["local_path"] = config.driver.local_path

# Build SeleniumWire driver if enabled
if seleniumwire_enabled:
if browser == Browser.CHROME:
return build_chrome(seleniumwire_options=config.driver.seleniumwire_options, **_config)
if browser == Browser.FIREFOX:
return build_firefox(seleniumwire_options=config.driver.seleniumwire_options, **_config)
raise ValueError(f"Only chrome and firefox are supported by SeleniumWire, not {config.driver.browser}")

# Otherwise, build the driver normally
# Build the WebDriver based on the browser
if browser == Browser.CHROME:
return build_chrome(seleniumwire_options=None, **_config)
return build_chrome(**_config)
if browser == Browser.EDGE:
return build_edge(**_config)
if browser == Browser.SAFARI:
return build_safari(**_config)
if browser == Browser.FIREFOX:
return build_firefox(seleniumwire_options=None, **_config)
return build_firefox(**_config)
if browser == Browser.IE:
return build_ie(**_config)
raise ValueError(f"{config.driver.browser} is not supported. Cannot build WebDriver from config.")
Expand All @@ -165,20 +156,16 @@ def build_chrome(
options: Optional[List[str]],
capabilities: Optional[Dict],
experimental_options: Optional[List[Dict]],
seleniumwire_options: Optional[Dict],
extension_paths: Optional[List[str]],
local_path: Optional[str],
webdriver_kwargs: Optional[Dict],
):
"""Build a Chrome WebDriver.

If seleniumwire_options is not None, a SeleniumWire Chrome WebDriver is built.

Args:
options: The list of options/arguments to include.
capabilities: The dict of capabilities to include.
experimental_options: The list of experimental options to include.
seleniumwire_options: The dict of seleniumwire options to include.
extension_paths: The list of extension filepaths to add to the browser.
local_path: The path to the driver binary (only to bypass WebDriverManager)
webdriver_kwargs: additional keyword arguments to pass.
Expand All @@ -191,21 +178,11 @@ def build_chrome(
for cap in caps:
browser_options.set_capability(cap, caps[cap])

if seleniumwire_options is None: # default to regular ChromeDriver
driver = webdriver.Chrome(
options=browser_options,
service=ChromeService(local_path or None),
**(webdriver_kwargs or {}),
)

else:
wire_options = seleniumwire_options or {}
driver = wire_driver.Chrome(
service=ChromeService(local_path or None),
options=browser_options,
seleniumwire_options=wire_options,
**(webdriver_kwargs or {}),
)
driver = webdriver.Chrome(
options=browser_options,
service=ChromeService(local_path or None),
**(webdriver_kwargs or {}),
)

# enable Performance Metrics from Chrome Dev Tools
driver.execute_cdp_cmd("Performance.enable", {})
Expand Down Expand Up @@ -282,20 +259,16 @@ def build_firefox(
options: Optional[List[str]],
capabilities: Optional[Dict],
experimental_options: Optional[List[Dict]],
seleniumwire_options: Optional[Dict],
extension_paths: Optional[List[str]],
local_path: Optional[str],
webdriver_kwargs: Optional[Dict],
):
"""Build a Firefox WebDriver.

If seleniumwire_options is not None, a SeleniumWire Firefox WebDriver is built.

Args:
options: The list of options/arguments to include.
capabilities: The dict of capabilities to include.
experimental_options: The list of experimental options to include.
seleniumwire_options: The dict of seleniumwire options to include.
extension_paths: The list of extensions to add to the browser.
local_path: The path to the driver binary.
webdriver_kwargs: additional keyword arguments to pass.
Expand All @@ -308,20 +281,11 @@ def build_firefox(
for cap in caps:
browser_options.set_capability(cap, caps[cap])

if seleniumwire_options is None: # default to regular FirefoxDriver
return webdriver.Firefox(
options=browser_options,
service=FirefoxService(local_path or None),
**(webdriver_kwargs or {}),
)
else:
wire_options = seleniumwire_options or {}
return wire_driver.Firefox(
options=browser_options,
service=FirefoxService(local_path or None),
seleniumwire_options=wire_options,
**(webdriver_kwargs or {}),
)
return webdriver.Firefox(
options=browser_options,
service=FirefoxService(local_path or None),
**(webdriver_kwargs or {}),
)


def build_ie(
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ python = ">=3.8.1"
requests = "^2.31.0"
pytest-xdist = "^3.5.0"
axe-selenium-python = "^2.1.6"
selenium-wire = "^5.1.0"
allure-pytest = "^2.13.2"
typer = { version = "^0.9.0", extras = ["all"] }
selenium = "^4.17.2"
Expand Down
21 changes: 14 additions & 7 deletions tests/ui/test_pydriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


THE_INTERNET = "https://the-internet.herokuapp.com"
TEST_PAGES = "https://testpages.eviltester.com"


def test_jit_webdriver(py: Pylenium):
Expand Down Expand Up @@ -82,13 +83,19 @@ def test_webdriver_wait_until(py: Pylenium):


def test_switch_to_frame_by_element_then_back(py: Pylenium):
py.visit(f"{THE_INTERNET}/iframe")
iframe = py.get("#mce_0_ifr")
py.switch_to.frame_by_element(iframe).get("#tinymce").clear().type("foo")
assert py.switch_to.default_content().contains("An iFrame").tag_name() == "h3"
py.switch_to.frame_by_element(iframe).get("#tinymce").type("bar")
assert "foobar" in py.get("#tinymce").text()
assert py.switch_to.parent_frame().contains("An iFrame").tag_name() == "h3"
py.visit(f"{TEST_PAGES}/styled/iframes-test.html")
iframe = py.get("iframe#theheaderhtml")
py.switch_to.frame_by_element(iframe)
assert py.get("h1").should().contain_text("Nested Page Example")

py.switch_to.default_content()
assert py.get("h1").should().contain_text("iFrames Example")

py.switch_to.frame_by_element(iframe)
assert py.get("h1").should().contain_text("Nested Page Example")

py.switch_to.parent_frame()
assert py.get("h2").should().contain_text("iFrame Example List")


def test_have_url(py: Pylenium):
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def test_py_config_defaults(py_config):
assert py_config.driver.capabilities == {}
assert py_config.driver.experimental_options is None
assert py_config.driver.webdriver_kwargs == {}
assert py_config.driver.seleniumwire_options == {}

# logging settings
assert py_config.logging.screenshots_on is True
Expand Down
Loading