-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
[py][bidi]: add bidi webExtension module #15749
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
20b9d93
add bidi webextension module
navin772 b23c653
add tests
navin772 1c3f881
Merge branch 'trunk' into py-bidi-webextension
navin772 c3edcb6
run formatter
navin772 5d33a00
load page and check extension install
navin772 f6a3d90
Merge branch 'trunk' into py-bidi-webextension
navin772 be3befd
Merge branch 'trunk' into py-bidi-webextension
navin772 f6403ed
Merge branch 'trunk' into py-bidi-webextension
cgoldberg 04f83ab
modify bazel
navin772 2b988dd
copy required directories
navin772 999df93
Refactor tests to remove code duplication
navin772 a05f5ff
use bazel runfiles
navin772 d9f7934
Merge branch 'trunk' into py-bidi-webextension
navin772 b3b5938
add webextension in api docs
navin772 5e45570
Merge branch 'trunk' into py-bidi-webextension
navin772 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from typing import Dict, Union | ||
|
||
from selenium.webdriver.common.bidi.common import command_builder | ||
|
||
|
||
class WebExtension: | ||
""" | ||
BiDi implementation of the webExtension module. | ||
""" | ||
|
||
def __init__(self, conn): | ||
self.conn = conn | ||
|
||
def install(self, path=None, archive_path=None, base64_value=None) -> Dict: | ||
"""Installs a web extension in the remote end. | ||
You must provide exactly one of the parameters. | ||
Parameters: | ||
----------- | ||
path: Path to an extension directory | ||
archive_path: Path to an extension archive file | ||
base64_value: Base64 encoded string of the extension archive | ||
Returns: | ||
------- | ||
Dict: A dictionary containing the extension ID. | ||
""" | ||
if sum(x is not None for x in (path, archive_path, base64_value)) != 1: | ||
raise ValueError("Exactly one of path, archive_path, or base64_value must be provided") | ||
|
||
if path is not None: | ||
extension_data = {"type": "path", "path": path} | ||
elif archive_path is not None: | ||
extension_data = {"type": "archivePath", "path": archive_path} | ||
elif base64_value is not None: | ||
extension_data = {"type": "base64", "value": base64_value} | ||
|
||
params = {"extensionData": extension_data} | ||
result = self.conn.execute(command_builder("webExtension.install", params)) | ||
return result | ||
|
||
def uninstall(self, extension_id_or_result: Union[str, Dict]) -> None: | ||
"""Uninstalls a web extension from the remote end. | ||
Parameters: | ||
----------- | ||
extension_id_or_result: Either the extension ID as a string or the result dictionary | ||
from a previous install() call containing the extension ID. | ||
""" | ||
if isinstance(extension_id_or_result, dict): | ||
extension_id = extension_id_or_result.get("extension") | ||
else: | ||
extension_id = extension_id_or_result | ||
|
||
params = {"extension": extension_id} | ||
self.conn.execute(command_builder("webExtension.uninstall", params)) |
This file contains hidden or 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
123 changes: 123 additions & 0 deletions
123
py/test/selenium/webdriver/common/bidi_webextension_tests.py
This file contains hidden or 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,123 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import base64 | ||
import os | ||
import pytest | ||
|
||
from python.runfiles import Runfiles | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.wait import WebDriverWait | ||
|
||
|
||
EXTENSION_ID = "[email protected]" | ||
EXTENSION_PATH = "webextensions-selenium-example-signed" | ||
EXTENSION_ARCHIVE_PATH = "webextensions-selenium-example.xpi" | ||
|
||
# Use bazel Runfiles to locate the test extension directory | ||
r = Runfiles.Create() | ||
extensions = r.Rlocation("selenium/py/test/extensions") | ||
|
||
|
||
def install_extension(driver, **kwargs): | ||
result = driver.webextension.install(**kwargs) | ||
assert result.get("extension") == EXTENSION_ID | ||
return result | ||
|
||
|
||
def verify_extension_injection(driver, pages): | ||
pages.load("blank.html") | ||
injected = WebDriverWait(driver, timeout=2).until( | ||
lambda dr: dr.find_element(By.ID, "webextensions-selenium-example") | ||
) | ||
assert injected.text == "Content injected by webextensions-selenium-example" | ||
|
||
|
||
def uninstall_extension_and_verify_extension_uninstalled(driver, extension_info): | ||
driver.webextension.uninstall(extension_info) | ||
|
||
context_id = driver.current_window_handle | ||
driver.browsing_context.reload(context_id) | ||
assert len(driver.find_elements(By.ID, "webextensions-selenium-example")) == 0 | ||
|
||
|
||
def test_webextension_initialized(driver): | ||
"""Test that the webextension module is initialized properly.""" | ||
assert driver.webextension is not None | ||
|
||
|
||
@pytest.mark.xfail_chrome | ||
@pytest.mark.xfail_edge | ||
def test_install_extension_path(driver, pages): | ||
"""Test installing an extension from a directory path.""" | ||
path = os.path.join(extensions, EXTENSION_PATH) | ||
|
||
ext_info = install_extension(driver, path=path) | ||
verify_extension_injection(driver, pages) | ||
uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | ||
|
||
|
||
@pytest.mark.xfail_chrome | ||
@pytest.mark.xfail_edge | ||
def test_install_archive_extension_path(driver, pages): | ||
"""Test installing an extension from an archive path.""" | ||
path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | ||
|
||
ext_info = install_extension(driver, archive_path=path) | ||
verify_extension_injection(driver, pages) | ||
uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | ||
|
||
|
||
@pytest.mark.xfail_chrome | ||
@pytest.mark.xfail_edge | ||
def test_install_base64_extension_path(driver, pages): | ||
"""Test installing an extension from a base64 encoded string.""" | ||
path = os.path.join(extensions, EXTENSION_ARCHIVE_PATH) | ||
|
||
with open(path, "rb") as file: | ||
base64_encoded = base64.b64encode(file.read()).decode("utf-8") | ||
|
||
ext_info = install_extension(driver, base64_value=base64_encoded) | ||
|
||
# TODO: the extension is installed but the script is not injected, check and fix | ||
# verify_extension_injection(driver, pages) | ||
|
||
uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | ||
|
||
|
||
@pytest.mark.xfail_chrome | ||
@pytest.mark.xfail_edge | ||
def test_install_unsigned_extension(driver, pages): | ||
"""Test installing an unsigned extension.""" | ||
path = os.path.join(extensions, "webextensions-selenium-example") | ||
|
||
ext_info = install_extension(driver, path=path) | ||
verify_extension_injection(driver, pages) | ||
uninstall_extension_and_verify_extension_uninstalled(driver, ext_info) | ||
|
||
|
||
@pytest.mark.xfail_chrome | ||
@pytest.mark.xfail_edge | ||
def test_install_with_extension_id_uninstall(driver, pages): | ||
"""Test uninstalling an extension using just the extension ID.""" | ||
path = os.path.join(extensions, EXTENSION_PATH) | ||
|
||
ext_info = install_extension(driver, path=path) | ||
extension_id = ext_info.get("extension") | ||
|
||
# Uninstall using the extension ID | ||
uninstall_extension_and_verify_extension_uninstalled(driver, extension_id) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.