@@ -150,19 +150,85 @@ const SNAPSHOT_SCRIPT = `(() => {
150150 };
151151})()` ;
152152
153+ /**
154+ * Width and height from a PNG's header, so the reported size is the image's own. Null for data
155+ * that is not a PNG.
156+ */
157+ const pngSize = ( base64 : string ) => {
158+ const header = Buffer . from ( base64 . slice ( 0 , 32 ) , "base64" ) ;
159+ return header . length >= 24 && header . toString ( "latin1" , 12 , 16 ) === "IHDR"
160+ ? { width : header . readUInt32BE ( 16 ) , height : header . readUInt32BE ( 20 ) }
161+ : null ;
162+ } ;
163+
164+ /** A page's viewport in device-independent pixels, the units a capture clip is given in. */
165+ type Viewport = { readonly width : number ; readonly height : number } ;
166+
167+ /**
168+ * A page's display ratio and CSS viewport, and its zoom. The ratio and viewport are read in an
169+ * isolated world, since page script can reassign `devicePixelRatio`, `innerWidth`, and
170+ * `innerHeight` in its own.
171+ */
172+ export const readPageMetrics = async ( cdp : CDPSession ) => {
173+ const { frameTree } = await cdp . send ( "Page.getFrameTree" ) ;
174+ const { executionContextId } = await cdp . send ( "Page.createIsolatedWorld" , {
175+ frameId : frameTree . frame . id ,
176+ // Chromium reuses a named world; an unnamed one is created again on each call.
177+ worldName : "t3-preview-metrics" ,
178+ } ) ;
179+ const readZoom = async ( ) =>
180+ ( await cdp . send ( "Page.getLayoutMetrics" ) ) . cssVisualViewport . zoom ?? 1 ;
181+ // The desktop applies a published zoom on its own schedule. Reading the zoom on both sides of
182+ // the ratio and viewport read catches a change in between, which would pair old CSS sizes
183+ // with the new zoom.
184+ let zoom = await readZoom ( ) ;
185+ for ( let attempt = 1 ; ; attempt += 1 ) {
186+ const { result } = await cdp . send ( "Runtime.evaluate" , {
187+ expression : "({ ratio: devicePixelRatio, width: innerWidth, height: innerHeight })" ,
188+ contextId : executionContextId ,
189+ returnByValue : true ,
190+ } ) ;
191+ const zoomAfter = await readZoom ( ) ;
192+ if ( zoomAfter === zoom ) {
193+ return {
194+ ...( result . value as {
195+ readonly ratio : number ;
196+ readonly width : number ;
197+ readonly height : number ;
198+ } ) ,
199+ zoom,
200+ } ;
201+ }
202+ // A capture sized from a torn read is cropped and oversized, so fail instead.
203+ if ( attempt === 3 ) {
204+ throw new Error (
205+ "The page's zoom kept changing while the snapshot read its size. Try the snapshot again." ,
206+ ) ;
207+ }
208+ zoom = zoomAfter ;
209+ }
210+ } ;
211+
153212// Scaled captures repaint live screencasts, so callers pause them. Clips use document offsets.
154213export const captureViewport = async (
155214 page : Page ,
156215 cdp : CDPSession ,
157- options : { readonly format : "png" | "jpeg" ; readonly quality ?: number ; readonly scale : number } ,
216+ options : {
217+ readonly format : "png" | "jpeg" ;
218+ readonly quality ?: number ;
219+ readonly scale : number ;
220+ readonly viewport ?: Viewport ;
221+ } ,
158222) => {
159223 let clip ;
160224 if ( options . scale < 1 ) {
161- const viewport = page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
225+ const viewport = options . viewport ?? page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
162226 const { cssVisualViewport } = await cdp . send ( "Page.getLayoutMetrics" ) ;
227+ // The offsets are CSS pixels; a clip is in device-independent pixels, which page zoom scales.
228+ const zoom = cssVisualViewport . zoom ?? 1 ;
163229 clip = {
164- x : cssVisualViewport . pageX ,
165- y : cssVisualViewport . pageY ,
230+ x : cssVisualViewport . pageX * zoom ,
231+ y : cssVisualViewport . pageY * zoom ,
166232 ...viewport ,
167233 scale : options . scale ,
168234 } ;
@@ -179,11 +245,13 @@ export const snapshot = async (input: {
179245 readonly page : Page ;
180246 readonly cdp : CDPSession ;
181247 readonly renderScale : number ;
248+ /** For a page Playwright did not size, such as one the desktop draws. */
249+ readonly viewport ?: Viewport ;
182250 readonly consoleEntries : ReadonlyArray < PreviewAutomationConsoleEntry > ;
183251 readonly networkEntries : ReadonlyArray < PreviewAutomationNetworkEntry > ;
184252 readonly actionTimeline : PreviewAutomationSnapshot [ "actionTimeline" ] ;
185253} ) : Promise < PreviewAutomationSnapshot > => {
186- const viewport = input . page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
254+ const viewport = input . viewport ?? input . page . viewportSize ( ) ?? { width : 1280 , height : 800 } ;
187255 const scale = Math . min ( 1 , MAX_SCREENSHOT_WIDTH / ( viewport . width * input . renderScale ) ) ;
188256 const state = refsFor ( input . page ) ;
189257 invalidateRefs ( input . page ) ;
@@ -196,7 +264,7 @@ export const snapshot = async (input: {
196264 >
197265 > ,
198266 input . page . ariaSnapshot ( { mode : "ai" , boxes : true , timeout : DEFAULT_TIMEOUT_MS } ) ,
199- captureViewport ( input . page , input . cdp , { format : "png" , scale } ) ,
267+ captureViewport ( input . page , input . cdp , { format : "png" , scale, viewport } ) ,
200268 ] ) ;
201269 if ( state . generation !== generation ) {
202270 throw new ServerBrowserOperationError (
@@ -220,8 +288,10 @@ export const snapshot = async (input: {
220288 screenshot : {
221289 mimeType : "image/png" ,
222290 data,
223- width : Math . round ( viewport . width * input . renderScale * scale ) ,
224- height : Math . round ( viewport . height * input . renderScale * scale ) ,
291+ ...( pngSize ( data ) ?? {
292+ width : Math . round ( viewport . width * input . renderScale * scale ) ,
293+ height : Math . round ( viewport . height * input . renderScale * scale ) ,
294+ } ) ,
225295 } ,
226296 } ;
227297} ;
0 commit comments