-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[test]: add tests DOM session builders #1448
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
[test]: add tests DOM session builders #1448
Conversation
|
Greptile SummaryThis PR adds comprehensive test coverage for DOM snapshot builder functions used by
All tests are well-structured with proper handler mocks for the CDP session. The test coverage addresses edge cases like CBOR stack limit failures and owner lookup errors, which are important for robustness in complex DOM traversals. Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Test as Test Code
participant Session as MockCDPSession
participant Handler as Handler Functions
Test->>Session: new MockCDPSession({handlers})
Test->>Session: hydrateDomTree(session, root, pierce)
Session->>Session: Check if node needs expansion
Session->>Handler: send("DOM.describeNode", {depth, ...})
Handler-->>Session: Return expanded node
Session-->>Test: Return hydrated tree
Test->>Session: getDomTreeWithFallback(session, pierce)
loop Retry with reduced depths
Session->>Handler: send("DOM.getDocument", {depth, ...})
alt CBOR Error
Handler-->>Session: Error: "CBOR: stack limit exceeded"
Session->>Session: Try next depth
else Success
Handler-->>Session: Return DOM tree
Session->>Session: hydrateDomTree if needed
Session-->>Test: Return root node
end
end
Test->>Session: buildSessionDomIndex(session, pierce)
Session->>Handler: send("DOM.enable")
Session->>Session: getDomTreeWithFallback()
Session->>Handler: send("DOM.describeNode") [if needed]
Handler-->>Session: Return tree with metadata
Session-->>Test: Return SessionDomIndex
Test->>Session: domMapsForSession(session, frameId, pierce, encode, attemptOwnerLookup)
Session->>Handler: send("DOM.enable")
Session->>Session: getDomTreeWithFallback()
alt attemptOwnerLookup enabled
Session->>Handler: send("DOM.getFrameOwner")
alt Success
Handler-->>Session: Return ownerBackendId
Session->>Session: Find iframe's contentDocument
else Failure
Handler-->>Session: Throw error
Session->>Session: Fall back to root document
end
end
Session->>Session: Traverse startNode and build maps
Session-->>Test: Return {tagNameMap, xpathMap, scrollableMap}
|
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.
No issues found across 3 files
Architecture diagram
sequenceDiagram
participant Test as Test Suite
participant Builder as DOM Builder Logic
participant MockCDP as NEW: MockCDPSession
Note over Test,MockCDP: Scenario 1: CBOR Error Recovery (getDomTreeWithFallback)
Test->>Builder: getDomTreeWithFallback()
loop Depth Retry Strategy
Builder->>MockCDP: send("DOM.getDocument", { depth: N })
alt NEW: CBOR/Stack Limit Error
MockCDP-->>Builder: Throw Error
Builder->>Builder: Catch & Reduce Depth (e.g. -1 -> 256)
else Success
MockCDP-->>Builder: Return Root Node
end
end
Note over Test,MockCDP: Scenario 2: Hydrating Truncated Nodes (hydrateDomTree)
Builder->>Builder: Scan tree for truncated nodes
opt Node has childNodeCount > children.length
Builder->>MockCDP: send("DOM.describeNode", { nodeId })
MockCDP-->>Builder: Return full node data
Builder->>Builder: Populate missing children
end
Note over Test,MockCDP: Scenario 3: Frame Relative Mapping (domMapsForSession)
Test->>Builder: domMapsForSession(frameId)
Builder->>MockCDP: send("DOM.getFrameOwner", { frameId })
alt Owner Found (Iframe)
MockCDP-->>Builder: { backendNodeId }
Builder->>Builder: NEW: Remap XPaths relative to Iframe Content Doc
else Owner Lookup Failed (Fallback)
MockCDP-->>Builder: Throw Error
Builder->>Builder: NEW: Fallback to Root Document context
end
Builder-->>Test: Return { xpathMap, tagMap, scrollableMap }
| import { StagehandDomProcessError } from "../lib/v3/types/public/sdkErrors"; | ||
| import { MockCDPSession } from "./helpers/mockCDPSession"; | ||
|
|
||
| let nextNodeId = 1; |
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.
You might want to reset this in a beforeEach? Not sure.
why
captureHybridSnapshot()depends onwhat changed
helpers/mockCDPSession.tsso that we can re use the mocked CDP session across testssnapshot-cbor.test.tsto use the shared CDP session mockadded
snapshot-dom-session-builders.test.tswhich holds tests for the following:hydrateDomTree()StagehandDomProcessErrorgetDomTreeWithFallback()StagehandDomProcessErrorbuildSessionDomIndex()domMapsForSession()test plan
Summary by cubic
Add unit tests for DOM session builder functions used by captureHybridSnapshot and introduce a reusable MockCDPSession helper. Tests cover CBOR retry logic, DOM hydration, error propagation, iframe/content-document indexing, scrollability, and frame‑relative maps with fallback behavior.
hydrateDomTree: expands truncated nodes via DOM.describeNode, retries on CBOR errors, errors after max attempts
getDomTreeWithFallback: retries DOM.getDocument with depth sequencing on CBOR errors, propagates non-CBOR errors, errors after all attempts
buildSessionDomIndex: records absolute xpaths, scrollability, per-document roots, and iframe content-document links
domMapsForSession: builds frame‑relative tag/xpath/scrollable maps, remaps via frame owner, falls back to root on owner lookup failure
Refactors
Written for commit adec18d. Summary will update automatically on new commits.