-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Chromie/add page snapshot method #1487
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
Open
chromiebot
wants to merge
4
commits into
browserbase:main
Choose a base branch
from
chromiebot:chromie/add-page-snapshot-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3254746
test: add failing test for page.snapshot method
881efcd
feat: expose page.snapshot() method for hybrid snapshots
3a88c09
docs: add page.snapshot() to v3 reference documentation
de6bd5f
docs: simplify page.snapshot() documentation to match v3 reference style
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { test, expect } from "@playwright/test"; | ||
| import { V3 } from "../v3"; | ||
| import { v3TestConfig } from "./v3.config"; | ||
|
|
||
| test.describe("Page.snapshot", () => { | ||
| let v3: V3; | ||
|
|
||
| test.beforeEach(async () => { | ||
| v3 = new V3(v3TestConfig); | ||
| await v3.init(); | ||
| }); | ||
|
|
||
| test.afterEach(async () => { | ||
| await v3?.close?.().catch(() => {}); | ||
| }); | ||
|
|
||
| test("returns a hybrid snapshot with combined tree and maps", async () => { | ||
| const page = v3.context.pages()[0]; | ||
|
|
||
| const html = ` | ||
| <!doctype html> | ||
| <html> | ||
| <head><title>Snapshot Test</title></head> | ||
| <body> | ||
| <h1>Hello World</h1> | ||
| <button id="submit-btn">Submit</button> | ||
| <a href="https://example.com">Link</a> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| await page.goto("data:text/html," + encodeURIComponent(html)); | ||
|
|
||
| // Call the new snapshot method | ||
| const snapshot = await page.snapshot(); | ||
|
|
||
| // Verify structure matches HybridSnapshot type | ||
| expect(snapshot).toBeDefined(); | ||
| expect(typeof snapshot.combinedTree).toBe("string"); | ||
| expect(typeof snapshot.combinedXpathMap).toBe("object"); | ||
| expect(typeof snapshot.combinedUrlMap).toBe("object"); | ||
|
|
||
| // The combined tree should contain our page content | ||
| expect(snapshot.combinedTree).toContain("Hello World"); | ||
| expect(snapshot.combinedTree).toContain("Submit"); | ||
| expect(snapshot.combinedTree).toContain("Link"); | ||
|
|
||
| // XPath map should have entries | ||
| expect(Object.keys(snapshot.combinedXpathMap).length).toBeGreaterThan(0); | ||
|
|
||
| // URL map should contain the link URL | ||
| expect(Object.values(snapshot.combinedUrlMap)).toContain( | ||
| "https://example.com/", | ||
| ); | ||
| }); | ||
|
|
||
| test("supports focusSelector option to scope the snapshot", async () => { | ||
| const page = v3.context.pages()[0]; | ||
|
|
||
| const html = ` | ||
| <!doctype html> | ||
| <html> | ||
| <head><title>Scoped Snapshot Test</title></head> | ||
| <body> | ||
| <div id="outside">Outside Content</div> | ||
| <main id="main-content"> | ||
| <h2>Main Heading</h2> | ||
| <p>Main paragraph</p> | ||
| </main> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| await page.goto("data:text/html," + encodeURIComponent(html)); | ||
|
|
||
| // Snapshot with focusSelector should scope to that element | ||
| const scopedSnapshot = await page.snapshot({ | ||
| focusSelector: "#main-content", | ||
| }); | ||
|
|
||
| expect(scopedSnapshot).toBeDefined(); | ||
| expect(scopedSnapshot.combinedTree).toContain("Main Heading"); | ||
| expect(scopedSnapshot.combinedTree).toContain("Main paragraph"); | ||
| }); | ||
|
|
||
| test("supports XPath focusSelector", async () => { | ||
| const page = v3.context.pages()[0]; | ||
|
|
||
| const html = ` | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <div id="container"> | ||
| <span>Target Text</span> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| await page.goto("data:text/html," + encodeURIComponent(html)); | ||
|
|
||
| // Use XPath-style focusSelector | ||
| const snapshot = await page.snapshot({ | ||
| focusSelector: "//div[@id='container']", | ||
| }); | ||
|
|
||
| expect(snapshot).toBeDefined(); | ||
| expect(snapshot.combinedTree).toContain("Target Text"); | ||
| }); | ||
|
|
||
| test("returns perFrame data when available", async () => { | ||
| const page = v3.context.pages()[0]; | ||
|
|
||
| const html = ` | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <p>Simple page</p> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| await page.goto("data:text/html," + encodeURIComponent(html)); | ||
|
|
||
| const snapshot = await page.snapshot(); | ||
|
|
||
| // perFrame should be present and contain at least one frame entry | ||
| expect(snapshot.perFrame).toBeDefined(); | ||
| expect(Array.isArray(snapshot.perFrame)).toBe(true); | ||
| expect(snapshot.perFrame!.length).toBeGreaterThanOrEqual(1); | ||
|
|
||
| const mainFrame = snapshot.perFrame![0]; | ||
| expect(mainFrame.frameId).toBeDefined(); | ||
| expect(typeof mainFrame.outline).toBe("string"); | ||
| expect(typeof mainFrame.xpathMap).toBe("object"); | ||
| expect(typeof mainFrame.urlMap).toBe("object"); | ||
| }); | ||
| }); | ||
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
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
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.
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.
P2: Test verifies scoped content is present but doesn't verify that out-of-scope content (
#outside) is excluded. Add a negative assertion to actually test the scoping behavior, otherwise this test provides false confidence.Prompt for AI agents