Skip to content

Commit 44157e4

Browse files
committed
add sync extension tests
1 parent 2b0de14 commit 44157e4

7 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('hey from the content-script');
2+
self.thisIsTheContentScript = true;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Mock script for background extension
2+
globalThis.MAGIC = 42;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Simple extension",
3+
"version": "0.1",
4+
"background": {
5+
"service_worker": "index.js"
6+
},
7+
"content_scripts": [{
8+
"matches": ["<all_urls>"],
9+
"css": [],
10+
"js": ["content-script.js"]
11+
}],
12+
"permissions": ["background", "activeTab"],
13+
"manifest_version": 3
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
console.log("Service worker script loaded");
2+
3+
chrome.runtime.onInstalled.addListener(() => {
4+
console.log("Extension installed");
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Test console log from a third-party execution context");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Console Log Extension",
4+
"version": "1.0",
5+
"background": {
6+
"service_worker": "background.js"
7+
},
8+
"permissions": [
9+
"tabs"
10+
],
11+
"content_scripts": [
12+
{
13+
"matches": ["<all_urls>"],
14+
"js": ["content.js"]
15+
}
16+
]
17+
}

tests/sync/test_extension.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pathlib import Path
16+
from typing import Any, Callable, Dict, Generator, Optional
17+
18+
import pytest
19+
20+
from playwright.sync_api import BrowserContext, BrowserType
21+
22+
23+
@pytest.fixture()
24+
def launch_persistent_context(
25+
browser_type: BrowserType,
26+
browser_channel: Optional[str],
27+
tmp_path: Path,
28+
launch_arguments: Dict[str, Any],
29+
is_headless_shell: bool,
30+
) -> Generator[Callable[[str], BrowserContext], None, None]:
31+
if browser_channel and browser_channel.startswith("chrome"):
32+
pytest.skip(
33+
"--load-extension is not supported in Chrome anymore. https://groups.google.com/a/chromium.org/g/chromium-extensions/c/1-g8EFx2BBY/m/S0ET5wPjCAAJ"
34+
)
35+
if is_headless_shell:
36+
pytest.skip("Headless Shell has no support for extensions")
37+
38+
def launch(extension_path: str, **kwargs: Any) -> BrowserContext:
39+
context = browser_type.launch_persistent_context(
40+
str(tmp_path),
41+
**launch_arguments,
42+
**kwargs,
43+
args=[
44+
f"--disable-extensions-except={extension_path}",
45+
f"--load-extension={extension_path}",
46+
],
47+
)
48+
return context
49+
50+
yield launch
51+
52+
53+
@pytest.mark.only_browser("chromium")
54+
def test_should_give_access_to_the_service_worker(
55+
launch_persistent_context: Any,
56+
assetdir: Path,
57+
) -> None:
58+
extension_path = str(assetdir / "extension-mv3-simple")
59+
context: BrowserContext = launch_persistent_context(extension_path)
60+
service_workers = context.service_workers
61+
service_worker = (
62+
service_workers[0]
63+
if len(service_workers)
64+
else context.wait_for_event("backgroundpage")
65+
)
66+
assert service_worker
67+
assert service_worker in context.service_workers
68+
assert service_worker.evaluate("globalThis.MAGIC") == 42
69+
context.close()
70+
assert len(context.background_pages) == 0
71+
72+
73+
@pytest.mark.only_browser("chromium")
74+
def test_should_give_access_to_the_service_worker_when_recording_video(
75+
launch_persistent_context: Any,
76+
tmp_path: Path,
77+
assetdir: Path,
78+
) -> None:
79+
extension_path = str(assetdir / "extension-mv3-simple")
80+
context: BrowserContext = launch_persistent_context(
81+
extension_path, record_video_dir=(tmp_path / "videos")
82+
)
83+
service_workers = context.service_workers
84+
service_worker = (
85+
service_workers[0]
86+
if len(service_workers)
87+
else context.wait_for_event("backgroundpage")
88+
)
89+
assert service_worker
90+
assert service_worker in context.service_workers
91+
assert service_worker.evaluate("globalThis.MAGIC") == 42
92+
context.close()
93+
assert len(context.background_pages) == 0

0 commit comments

Comments
 (0)