-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add web platform tests for WebExtensions API in WebDriver Classic #49786
Open
elijahsawyers
wants to merge
1
commit into
web-platform-tests:master
Choose a base branch
from
elijahsawyers:web_extensions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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
Empty file.
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,143 @@ | ||
import os | ||
import pytest | ||
|
||
from base64 import b64encode | ||
from shutil import copyfileobj | ||
from tempfile import mkdtemp | ||
from tests.support.asserts import assert_error, assert_success | ||
from zipfile import ZipFile | ||
|
||
def load_extension(session, parameters): | ||
return session.transport.send( | ||
"POST", "session/%s/webextension" % session.session_id, | ||
{"type": parameters.get("type"), "value": parameters.get("value")}) | ||
|
||
|
||
def copy_dir_into_zip(source, dest): | ||
with ZipFile(dest, "w") as zf: | ||
for root, _, files in os.walk(source): | ||
for f in files: | ||
path = os.path.join(root, f) | ||
with open(path, "rb") as fd: | ||
with zf.open(os.path.relpath(path, source), "w") as fd2: | ||
copyfileobj(fd, fd2) | ||
|
||
|
||
@pytest.fixture(params=[ | ||
("path", os.path.join(os.path.dirname(__file__), "resources/extension/")), | ||
("archivePath", os.path.join(os.path.dirname(__file__), "resources/extension/")), | ||
("base64", os.path.join(os.path.dirname(__file__), "resources/extension/")) | ||
]) | ||
def extension(request): | ||
if request.param[0] == "path": | ||
return { | ||
"type": request.param[0], | ||
"value": request.param[1], | ||
} | ||
|
||
elif request.param[0] == "archivePath": | ||
zip_path = os.path.join(mkdtemp(), "extension.zip") | ||
copy_dir_into_zip(request.param[1], zip_path) | ||
return { | ||
"type": request.param[0], | ||
"value": zip_path, | ||
} | ||
|
||
elif request.param[0] == "base64": | ||
zip_path = os.path.join(mkdtemp(), "extension.zip") | ||
copy_dir_into_zip(request.param[1], zip_path) | ||
with open(zip_path, "rb") as f: | ||
encoded = b64encode(f.read()).decode("utf-8") | ||
|
||
return { | ||
"type": request.param[0], | ||
"value": encoded, | ||
} | ||
|
||
else: | ||
raise NotImplementedError( | ||
"Unexpected param, unable to return requested extension" | ||
) | ||
|
||
|
||
def test_no_top_browsing_context(session, closed_window): | ||
extension = { | ||
"type": "path", | ||
"value": "/" | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "no such window") | ||
|
||
|
||
def test_null_type_parameter(session): | ||
extension = { | ||
"value": "/" | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "invalid argument") | ||
|
||
|
||
def test_invalid_type_parameter(session): | ||
extension = { | ||
"type": "invalid", | ||
"value": "/" | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "invalid argument") | ||
|
||
|
||
def test_null_value_parameter(session): | ||
extension = { | ||
"type": "path" | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "invalid argument") | ||
|
||
|
||
def test_invalid_zip(session): | ||
extension = { | ||
"type": "archivePath", | ||
"value": os.path.join(os.path.dirname(__file__), "resources/invalid_extension.zip") | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "unable to load extension") | ||
|
||
|
||
def test_invalid_base64(session): | ||
extension = { | ||
"type": "base64", | ||
"value": "invalid base64" | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "unable to load extension") | ||
|
||
|
||
@pytest.mark.parametrize("hint,value", [ | ||
("path", "/invalid/path/"), | ||
("archivePath", "/invalid/path/extension.zip") | ||
]) | ||
def test_invalid_path(session, hint, value): | ||
extension = { | ||
"type": hint, | ||
"value": value | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_error(response, "unable to load extension") | ||
|
||
|
||
def test_load_extension(session, extension): | ||
response = load_extension(session, extension) | ||
assert_success(response) | ||
|
||
assert "value" in response.body | ||
assert isinstance(response.body["value"], dict) | ||
assert "extensionId" in response.body["value"] | ||
assert isinstance(response.body["value"]["extensionId"], str) | ||
assert response.body["value"]["extensionId"] |
1 change: 1 addition & 0 deletions
1
webdriver/tests/classic/web_extensions/resources/extension/background.js
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 @@ | ||
console.log("Hello, world!"); | ||
9 changes: 9 additions & 0 deletions
9
webdriver/tests/classic/web_extensions/resources/extension/manifest.json
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,9 @@ | ||
{ | ||
"manifest_version": 3, | ||
"name": "WPT Test Extension", | ||
"description": "This extension is to be used in web platform tests.", | ||
"version": "1.0", | ||
"background": { | ||
"scripts": [ "background.js" ] | ||
} | ||
} |
Binary file added
BIN
+1.16 KB
webdriver/tests/classic/web_extensions/resources/invalid_extension.zip
Binary file not shown.
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,38 @@ | ||
import os | ||
import pytest | ||
|
||
from tests.support.asserts import assert_error, assert_success | ||
from urllib.parse import quote | ||
|
||
def load_extension(session, parameters): | ||
return session.transport.send( | ||
"POST", "session/%s/webextension" % session.session_id, | ||
{"type": parameters.get("type"), "value": parameters.get("value")}) | ||
|
||
|
||
def unload_extension(session, extension_id): | ||
return session.transport.send("DELETE", "session/%s/webextension/%s" % (session.session_id, extension_id)) | ||
|
||
|
||
def test_no_top_browsing_context(session, closed_window): | ||
response = unload_extension(session, "extension_id") | ||
assert_error(response, "no such window") | ||
|
||
|
||
def test_unload_extension_invalid_extension_id(session): | ||
response = unload_extension(session, "extension_id") | ||
assert_error(response, "no such extension") | ||
|
||
|
||
def test_unload_extension(session): | ||
extension = { | ||
"type": "path", | ||
"value": os.path.join(os.path.dirname(__file__), "resources/extension/") | ||
} | ||
|
||
response = load_extension(session, extension) | ||
assert_success(response) | ||
|
||
extension_id = quote(response.body["value"]["extensionId"]) | ||
response = unload_extension(session, extension_id) | ||
assert_success(response) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is minor, but it might be better to use the
browser.test.log
method — as we plan to use the rest of thetest
namespace in these test extensions.