-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix(canvas snapshots): position mismatch in headless mode #33575
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
868a95e
fix(canvas snapshots): fix position mismatch with headless screenshots
Skn0tt 690ca79
remove border
Skn0tt b918f13
add threshold
Skn0tt 4af8593
wrong option!
Skn0tt 0a4c4f5
use snapshot-time bounding rect instead
Skn0tt 2a2a95f
remove test
Skn0tt 79ad3e2
remove for good
Skn0tt 85fd54a
address feedback
Skn0tt 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 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 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 |
---|---|---|
|
@@ -427,25 +427,26 @@ function snapshotScript(...targetIds: (string | undefined)[]) { | |
for (const canvas of canvasElements) { | ||
const context = canvas.getContext('2d')!; | ||
|
||
const boundingRect = canvas.getBoundingClientRect(); | ||
const xStart = boundingRect.left / window.innerWidth; | ||
const yStart = boundingRect.top / window.innerHeight; | ||
const xEnd = boundingRect.right / window.innerWidth; | ||
const yEnd = boundingRect.bottom / window.innerHeight; | ||
|
||
const partiallyUncaptured = xEnd > 1 || yEnd > 1; | ||
const fullyUncaptured = xStart > 1 || yStart > 1; | ||
drawCheckerboard(context, canvas); | ||
|
||
const boundingRectAttribute = canvas.getAttribute('__playwright_bounding_rect__'); | ||
canvas.removeAttribute('__playwright_bounding_rect__'); | ||
if (!boundingRectAttribute) | ||
continue; | ||
|
||
const boundingRect = JSON.parse(boundingRectAttribute) as { left: number, top: number, right: number, bottom: number }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try/catch parsing just in case. |
||
|
||
const partiallyUncaptured = boundingRect.right > 1 || boundingRect.bottom > 1; | ||
const fullyUncaptured = boundingRect.left > 1 || boundingRect.top > 1; | ||
if (fullyUncaptured) { | ||
canvas.title = `Playwright couldn't capture canvas contents because it's located outside the viewport.`; | ||
continue; | ||
} | ||
|
||
drawCheckerboard(context, canvas); | ||
|
||
context.drawImage(img, xStart * img.width, yStart * img.height, (xEnd - xStart) * img.width, (yEnd - yStart) * img.height, 0, 0, canvas.width, canvas.height); | ||
context.drawImage(img, boundingRect.left * img.width, boundingRect.top * img.height, (boundingRect.right - boundingRect.left) * img.width, (boundingRect.bottom - boundingRect.top) * img.height, 0, 0, canvas.width, canvas.height); | ||
if (isUnderTest) | ||
// eslint-disable-next-line no-console | ||
console.log(`canvas drawn:`, JSON.stringify([xStart, yStart, xEnd, yEnd].map(v => Math.floor(v * 100)))); | ||
console.log(`canvas drawn:`, JSON.stringify([boundingRect.left, boundingRect.top, (boundingRect.right - boundingRect.left), (boundingRect.bottom - boundingRect.top)].map(v => Math.floor(v * 100)))); | ||
|
||
if (partiallyUncaptured) | ||
canvas.title = `Playwright couldn't capture full canvas contents because it's located partially outside the viewport.`; | ||
|
This file contains 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.
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.
Let's keep not drawing the checkerboard for
fullyUncaptured
canvases, to avoid any performance penalties.