|
| 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, Awaitable, Callable, Dict, Generator, Optional |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from playwright.async_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], Awaitable[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 | + async def launch(extension_path: str, **kwargs: Any) -> BrowserContext: |
| 39 | + context = await 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 | +async 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 = await 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 await context.wait_for_event("backgroundpage") |
| 65 | + ) |
| 66 | + assert service_worker |
| 67 | + assert service_worker in context.service_workers |
| 68 | + assert await service_worker.evaluate("globalThis.MAGIC") == 42 |
| 69 | + await context.close() |
| 70 | + assert len(context.background_pages) == 0 |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.only_browser("chromium") |
| 74 | +async 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 = await 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 await context.wait_for_event("backgroundpage") |
| 88 | + ) |
| 89 | + assert service_worker |
| 90 | + assert service_worker in context.service_workers |
| 91 | + assert await service_worker.evaluate("globalThis.MAGIC") == 42 |
| 92 | + await context.close() |
| 93 | + assert len(context.background_pages) == 0 |
0 commit comments