-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(selenium-devtools-py): register the collector at document start via BiDi preload #317
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
4 commits
Select commit
Hold shift + click to select a range
3c0b23c
fix(examples): restore the login example's flash assertion
vishnuv688 1d9f7bd
feat(selenium-devtools-py): register the collector at document start …
vishnuv688 478170d
fix(selenium-devtools-py): skip the selenium surface test when seleni…
vishnuv688 70a6223
fix(selenium-devtools-py): recover a document the preload did not reach
vishnuv688 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
68 changes: 68 additions & 0 deletions
68
packages/selenium-devtools-py/src/selenium_devtools/bidi_preload.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,68 @@ | ||
| """Document-start registration of the collector via a BiDi preload script. | ||
|
|
||
| The ``<script>``-append path in ``snapshot.py`` can only instrument the document | ||
| that exists when it runs, and a ``<script>`` dies with its document. So every | ||
| navigation produces a document we learn about afterwards, and everything built on | ||
| that — when to re-inject, when to drain, which action owns the new DOM — is | ||
| reconstruction, and races. A preload registered for the session runs in EVERY | ||
| document before any of that document's own script, so each one instruments itself | ||
| and anchors its own DOM at its own ``performance.timeOrigin``. Nothing to detect, | ||
| nothing to attribute. | ||
|
|
||
| Mirrors ``core/bidi-preload.ts``, which the JS adapters use for the same reason. | ||
|
|
||
| Requires a session created with ``webSocketUrl: true``; ``driver.script`` raises | ||
| without it, so this reports False and the caller keeps the ``<script>`` path. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Any | ||
|
|
||
| from .constants import BIDI_CAPABILITY, LOGGER_NAME | ||
|
|
||
| _log = logging.getLogger(f"{LOGGER_NAME}.preload") | ||
|
|
||
|
|
||
| def as_function_declaration(source: str) -> str: | ||
| """Wrap raw collector source as the function declaration BiDi expects. | ||
|
|
||
| Not the ``wrap_injectable`` IIFE: a preload script IS a function, so the | ||
| bundle's top-level await works directly in its body. | ||
| """ | ||
| return f"async () => {{ {source} }}" | ||
|
|
||
|
|
||
| def register_collector_preload(driver: Any, source: str) -> bool: | ||
| """Register the collector to run at document-start in every document of this | ||
| session. False means the caller must fall back to ``<script>`` injection. | ||
|
|
||
| Registered with NO browsing-context id, which is what makes it global — | ||
| contexts created later are covered, which is exactly the set this exists to | ||
| catch. `driver.script.pin` is the public API for it; its docstring says | ||
| "current browsing context", but it forwards no `contexts` argument, and BiDi | ||
| treats that as every context. That reliance is pinned by a test. | ||
| """ | ||
| caps = getattr(driver, "caps", None) | ||
| if not (isinstance(caps, dict) and caps.get(BIDI_CAPABILITY)): | ||
| # Not a warning: the caller already reports the missing capability once, | ||
| # and the `<script>` path still captures DOM — just with the races this | ||
| # module exists to remove. | ||
| _log.debug( | ||
| "%s not set on the session — no document-start preload, using " | ||
| "per-document injection", | ||
| BIDI_CAPABILITY, | ||
| ) | ||
| return False | ||
| try: | ||
| script = driver.script | ||
| script.pin(as_function_declaration(source)) | ||
| except Exception as exc: # noqa: BLE001 — any selenium/BiDi failure degrades | ||
| _log.warning( | ||
| "BiDi preload unavailable, falling back to per-document injection: %s", | ||
| exc, | ||
| ) | ||
| return False | ||
| _log.info("collector registered at document-start (BiDi preload)") | ||
| return True |
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
Oops, something went wrong.
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.