49 lines
2.2 KiB
Python
49 lines
2.2 KiB
Python
"""Disposable Chromium fixture with real browser zoom, never a user profile.
|
|
|
|
chrome.tabs.setZoom changes layout zoom (innerWidth halves at 200%, DPR doubles).
|
|
CSS zoom and CDP page-scale emulation do not establish this acceptance criterion.
|
|
Use only with explicit loopback canary origins and clean up the context afterward.
|
|
"""
|
|
from contextlib import contextmanager
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
@contextmanager
|
|
def zoom_browser(playwright, width=1440, height=900):
|
|
with tempfile.TemporaryDirectory(prefix="technical-light-zoom-") as temporary:
|
|
root = Path(temporary)
|
|
extension = root / "extension"
|
|
extension.mkdir()
|
|
(extension / "manifest.json").write_text(json.dumps({
|
|
"manifest_version": 3, "name": "Local canary browser zoom", "version": "1.0",
|
|
"permissions": ["tabs"], "background": {"service_worker": "worker.js"},
|
|
}))
|
|
(extension / "worker.js").write_text("chrome.runtime.onInstalled.addListener(() => {});")
|
|
context = playwright.chromium.launch_persistent_context(
|
|
str(root / "profile"), channel="chromium", headless=True,
|
|
args=[f"--disable-extensions-except={extension}", f"--load-extension={extension}"],
|
|
viewport={"width": width, "height": height})
|
|
try:
|
|
worker = context.service_workers[0] if context.service_workers else context.wait_for_event("serviceworker")
|
|
yield context, worker
|
|
finally:
|
|
context.close()
|
|
|
|
|
|
def set_browser_zoom(worker, page, factor):
|
|
parsed = urlsplit(page.url)
|
|
if parsed.hostname not in {"127.0.0.1", "localhost", "::1"}:
|
|
raise ValueError("Browser zoom fixture only permits explicit loopback hosts")
|
|
if factor not in {1, 2}:
|
|
raise ValueError("Only 100% and 200% acceptance zoom are supported")
|
|
return worker.evaluate("""async ({url, factor}) => {
|
|
const tabs = await chrome.tabs.query({});
|
|
const target = tabs.find(tab => tab.url === url);
|
|
if (!target) throw new Error('Canary tab not found');
|
|
await chrome.tabs.setZoom(target.id, factor);
|
|
return await chrome.tabs.getZoom(target.id);
|
|
}""", {"url": page.url, "factor": factor})
|