| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { expect, browserTest } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2022-04-18 19:20:49 -08:00
										 |  |  | import { PNG } from 'playwright-core/lib/utilsBundle'; | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { verifyViewport } from '../config/utils'; | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | browserTest.describe('page screenshot', () => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   browserTest.skip(({ browserName, headless }) => browserName === 'firefox' && !headless, 'Firefox headed produces a different image.'); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should run in parallel in multiple pages', async ({ server, contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const context = await contextFactory(); | 
					
						
							|  |  |  |     const N = 5; | 
					
						
							|  |  |  |     const pages = await Promise.all(Array(N).fill(0).map(async () => { | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |       return page; | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |     const promises = []; | 
					
						
							|  |  |  |     for (let i = 0; i < N; ++i) | 
					
						
							|  |  |  |       promises.push(pages[i].screenshot({ clip: { x: 50 * (i % 2), y: 0, width: 50, height: 50 } })); | 
					
						
							|  |  |  |     const screenshots = await Promise.all(promises); | 
					
						
							|  |  |  |     for (let i = 0; i < N; ++i) | 
					
						
							|  |  |  |       expect(screenshots[i]).toMatchSnapshot(`grid-cell-${i % 2}.png`); | 
					
						
							|  |  |  |     await Promise.all(pages.map(page => page.close())); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should work with a mobile viewport', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, isMobile: true }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/overflow.html'); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-mobile.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should work with a mobile viewport and clip', async ({ browser, server, browserName, channel }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/overflow.html'); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot({ clip: { x: 10, y: 10, width: 100, height: 150 } }); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-mobile-clip.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should work with a mobile viewport and fullPage', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/overflow-large.html'); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot({ fullPage: true }); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-mobile-fullpage.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 23:48:28 -08:00
										 |  |  |   browserTest('should work with device scale factor', async ({ browser, server, isMac, browserName }) => { | 
					
						
							|  |  |  |     browserTest.fixme(isMac && browserName === 'webkit'); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, deviceScaleFactor: 2 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-device-scale-factor.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |   browserTest('should work with device scale factor and clip', async ({ browser, server }) => { | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 500, height: 500 }, deviceScaleFactor: 3 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot({ clip: { x: 50, y: 100, width: 150, height: 100 } }); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-device-scale-factor-clip.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |   browserTest('should work with device scale factor and scale:css', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, deviceScaleFactor: 2 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |     const screenshot = await page.screenshot({ scale: 'css' }); | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     expect(screenshot).toMatchSnapshot('screenshot-device-scale-factor-css-size.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |   browserTest('should work with device scale factor, clip and scale:css', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 500, height: 500 }, deviceScaleFactor: 3 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |     const screenshot = await page.screenshot({ clip: { x: 50, y: 100, width: 150, height: 100 }, scale: 'css' }); | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     expect(screenshot).toMatchSnapshot('screenshot-device-scale-factor-clip-css-size.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 18:15:49 -07:00
										 |  |  |   browserTest('should throw if screenshot size is too large with device scale factor', async ({ browser, browserName, isMac }) => { | 
					
						
							|  |  |  |     browserTest.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16727' }); | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 500, height: 500 }, deviceScaleFactor: 2 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       await page.setContent(`<style>body {margin: 0; padding: 0;}</style><div style='min-height: 16383px; background: red;'></div>`); | 
					
						
							|  |  |  |       const result = await page.screenshot({ fullPage: true }); | 
					
						
							|  |  |  |       expect(result).toBeTruthy(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       await page.setContent(`<style>body {margin: 0; padding: 0;}</style><div style='min-height: 16384px; background: red;'></div>`); | 
					
						
							|  |  |  |       const exception = await page.screenshot({ fullPage: true }).catch(e => e); | 
					
						
							|  |  |  |       if (browserName === 'firefox' || (browserName === 'webkit' && !isMac)) | 
					
						
							|  |  |  |         expect(exception.message).toContain('Cannot take screenshot larger than 32767'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const image = await page.screenshot({ fullPage: true, scale: 'css' }); | 
					
						
							|  |  |  |       expect(image).toBeTruthy(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   browserTest('should work with large size', async ({ browserName, headless, platform, contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2023-01-30 09:53:28 +01:00
										 |  |  |     browserTest.fixme(browserName === 'chromium' && !headless && platform === 'linux', 'Chromium has gpu problems on linux with large screenshots'); | 
					
						
							| 
									
										
										
										
											2021-05-20 09:51:09 -07:00
										 |  |  |     browserTest.slow(true, 'Large screenshot is slow'); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const context = await contextFactory(); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.setViewportSize({ width: 1280, height: 800 }); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       document.body.style.margin = '0'; | 
					
						
							|  |  |  |       document.body.style.padding = '0'; | 
					
						
							|  |  |  |       document.documentElement.style.margin = '0'; | 
					
						
							|  |  |  |       document.documentElement.style.padding = '0'; | 
					
						
							|  |  |  |       const div = document.createElement('div'); | 
					
						
							|  |  |  |       div.style.width = '1250px'; | 
					
						
							|  |  |  |       div.style.height = '8440px'; | 
					
						
							|  |  |  |       div.style.background = 'linear-gradient(red, blue)'; | 
					
						
							|  |  |  |       document.body.appendChild(div); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const buffer = await page.screenshot({ fullPage: true }); | 
					
						
							|  |  |  |     const decoded = PNG.sync.read(buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const pixel = (x: number, y: number) => { | 
					
						
							|  |  |  |       const dst = new PNG({ width: 1, height: 1 }); | 
					
						
							|  |  |  |       PNG.bitblt(decoded, dst, x, y, 1, 1); | 
					
						
							|  |  |  |       const pixels = dst.data; | 
					
						
							|  |  |  |       return { r: pixels[0], g: pixels[1], b: pixels[2], a: pixels[3] }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(pixel(0, 0).r).toBeGreaterThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 0).b).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 8339).r).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 8339).b).toBeGreaterThan(128); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-11-30 14:11:15 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   browserTest('should handle vh units ', async ({ contextFactory }) => { | 
					
						
							|  |  |  |     const context = await contextFactory(); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.setViewportSize({ width: 800, height: 500 }); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       document.body.style.margin = '0'; | 
					
						
							|  |  |  |       document.body.style.padding = '0'; | 
					
						
							|  |  |  |       document.documentElement.style.margin = '0'; | 
					
						
							|  |  |  |       document.documentElement.style.padding = '0'; | 
					
						
							|  |  |  |       const div = document.createElement('div'); | 
					
						
							|  |  |  |       div.style.width = '100%'; | 
					
						
							|  |  |  |       div.style.borderTop = '100vh solid red'; | 
					
						
							|  |  |  |       div.style.borderBottom = '100vh solid blue'; | 
					
						
							|  |  |  |       document.body.appendChild(div); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const buffer = await page.screenshot({ fullPage: true }); | 
					
						
							|  |  |  |     const decoded = PNG.sync.read(buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const pixel = (x: number, y: number) => { | 
					
						
							|  |  |  |       const dst = new PNG({ width: 1, height: 1 }); | 
					
						
							|  |  |  |       PNG.bitblt(decoded, dst, x, y, 1, 1); | 
					
						
							|  |  |  |       const pixels = dst.data; | 
					
						
							|  |  |  |       return { r: pixels[0], g: pixels[1], b: pixels[2], a: pixels[3] }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(pixel(0, 0).r).toBeGreaterThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 0).b).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 999).r).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 999).b).toBeGreaterThan(128); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-02 16:55:32 +01:00
										 |  |  | browserTest.describe('element screenshot', () => { | 
					
						
							| 
									
										
										
										
											2021-05-13 10:22:23 -07:00
										 |  |  |   browserTest.skip(({ browserName, headless }) => browserName === 'firefox' && !headless); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('element screenshot should work with a mobile viewport', async ({ browser, server, browserName }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(browserName === 'firefox'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, isMobile: true }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     await page.evaluate(() => window.scrollBy(50, 100)); | 
					
						
							|  |  |  |     const elementHandle = await page.$('.box:nth-of-type(3)'); | 
					
						
							|  |  |  |     const screenshot = await elementHandle.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-element-mobile.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 23:48:28 -08:00
										 |  |  |   browserTest('element screenshot should work with device scale factor', async ({ browser, server, browserName, isMac }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(browserName === 'firefox'); | 
					
						
							| 
									
										
										
										
											2022-12-29 23:48:28 -08:00
										 |  |  |     browserTest.fixme(isMac && browserName === 'webkit'); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 320, height: 480 }, deviceScaleFactor: 2 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     await page.evaluate(() => window.scrollBy(50, 100)); | 
					
						
							|  |  |  |     const elementHandle = await page.$('.box:nth-of-type(3)'); | 
					
						
							|  |  |  |     const screenshot = await elementHandle.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('screenshot-element-mobile-dsf.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should take screenshots when default viewport is null', async ({ server, browser }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const context = await browser.newContext({ viewport: null }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.setContent(`<div style='height: 10000px; background: red'></div>`); | 
					
						
							|  |  |  |     const windowSize = await page.evaluate(() => ({ width: window.innerWidth * window.devicePixelRatio, height: window.innerHeight * window.devicePixelRatio })); | 
					
						
							|  |  |  |     const sizeBefore = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const screenshot = await page.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toBeInstanceOf(Buffer); | 
					
						
							|  |  |  |     const decoded = PNG.sync.read(screenshot); | 
					
						
							|  |  |  |     expect(decoded.width).toBe(windowSize.width); | 
					
						
							|  |  |  |     expect(decoded.height).toBe(windowSize.height); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const sizeAfter = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  |     expect(sizeBefore.width).toBe(sizeAfter.width); | 
					
						
							|  |  |  |     expect(sizeBefore.height).toBe(sizeAfter.height); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should take fullPage screenshots when default viewport is null', async ({ server, browser }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const context = await browser.newContext({ viewport: null }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     const sizeBefore = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot({ | 
					
						
							|  |  |  |       fullPage: true | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     expect(screenshot).toBeInstanceOf(Buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const sizeAfter = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  |     expect(sizeBefore.width).toBe(sizeAfter.width); | 
					
						
							|  |  |  |     expect(sizeBefore.height).toBe(sizeAfter.height); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   browserTest('should restore default viewport after fullPage screenshot', async ({ browser }) => { | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 456, height: 789 } }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await verifyViewport(page, 456, 789); | 
					
						
							|  |  |  |     const screenshot = await page.screenshot({ fullPage: true }); | 
					
						
							|  |  |  |     expect(screenshot).toBeInstanceOf(Buffer); | 
					
						
							|  |  |  |     await verifyViewport(page, 456, 789); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   browserTest('should restore viewport after page screenshot and exception', async ({ browser, server, mode }) => { | 
					
						
							|  |  |  |     browserTest.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 350, height: 360 } }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     const __testHookBeforeScreenshot = () => { throw new Error('oh my'); }; | 
					
						
							|  |  |  |     const error = await page.screenshot({ fullPage: true, __testHookBeforeScreenshot } as any).catch(e => e); | 
					
						
							|  |  |  |     expect(error.message).toContain('oh my'); | 
					
						
							|  |  |  |     await verifyViewport(page, 350, 360); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   browserTest('should restore viewport after page screenshot and timeout', async ({ browser, server, mode }) => { | 
					
						
							|  |  |  |     browserTest.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 350, height: 360 } }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |     const __testHookAfterScreenshot = () => new Promise(f => setTimeout(f, 5000)); | 
					
						
							|  |  |  |     const error = await page.screenshot({ fullPage: true, __testHookAfterScreenshot, timeout: 3000 } as any).catch(e => e); | 
					
						
							|  |  |  |     expect(error.message).toContain('page.screenshot: Timeout 3000ms exceeded'); | 
					
						
							|  |  |  |     await verifyViewport(page, 350, 360); | 
					
						
							|  |  |  |     await page.setViewportSize({ width: 400, height: 400 }); | 
					
						
							|  |  |  |     await page.waitForTimeout(3000); // Give it some time to wrongly restore previous viewport.
 | 
					
						
							|  |  |  |     await verifyViewport(page, 400, 400); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should take element screenshot when default viewport is null and restore back', async ({ server, browser }) => { | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: null }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.setContent(`
 | 
					
						
							|  |  |  |       <div style="height: 14px">oooo</div> | 
					
						
							|  |  |  |       <style> | 
					
						
							|  |  |  |       div.to-screenshot { | 
					
						
							|  |  |  |         border: 1px solid blue; | 
					
						
							|  |  |  |         width: 600px; | 
					
						
							|  |  |  |         height: 600px; | 
					
						
							|  |  |  |         margin-left: 50px; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       ::-webkit-scrollbar{ | 
					
						
							|  |  |  |         display: none; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       </style> | 
					
						
							|  |  |  |       <div class="to-screenshot"></div> | 
					
						
							|  |  |  |       <div class="to-screenshot"></div> | 
					
						
							|  |  |  |       <div class="to-screenshot"></div> | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |     const sizeBefore = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  |     const elementHandle = await page.$('div.to-screenshot'); | 
					
						
							|  |  |  |     const screenshot = await elementHandle.screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toBeInstanceOf(Buffer); | 
					
						
							|  |  |  |     const sizeAfter = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); | 
					
						
							|  |  |  |     expect(sizeBefore.width).toBe(sizeAfter.width); | 
					
						
							|  |  |  |     expect(sizeBefore.height).toBe(sizeAfter.height); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   browserTest('should restore viewport after element screenshot and exception', async ({ browser, mode }) => { | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  |     browserTest.skip(mode !== 'default'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 350, height: 360 } }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.setContent(`<div style="width:600px;height:600px;"></div>`); | 
					
						
							|  |  |  |     const elementHandle = await page.$('div'); | 
					
						
							|  |  |  |     const __testHookBeforeScreenshot = () => { throw new Error('oh my'); }; | 
					
						
							|  |  |  |     const error = await elementHandle.screenshot({ __testHookBeforeScreenshot } as any).catch(e => e); | 
					
						
							|  |  |  |     expect(error.message).toContain('oh my'); | 
					
						
							|  |  |  |     await verifyViewport(page, 350, 360); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-11-02 16:55:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 14:11:15 -08:00
										 |  |  |   browserTest('element screenshots should handle vh units ', async ({ contextFactory }) => { | 
					
						
							|  |  |  |     const context = await contextFactory(); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.setViewportSize({ width: 800, height: 500 }); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       const div = document.createElement('div'); | 
					
						
							|  |  |  |       div.style.width = '100%'; | 
					
						
							|  |  |  |       div.style.borderTop = '100vh solid red'; | 
					
						
							|  |  |  |       div.style.borderBottom = '100vh solid blue'; | 
					
						
							|  |  |  |       document.body.appendChild(div); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const elementHandle = await page.$('div'); | 
					
						
							|  |  |  |     const buffer = await elementHandle.screenshot(); | 
					
						
							|  |  |  |     const decoded = PNG.sync.read(buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const pixel = (x: number, y: number) => { | 
					
						
							|  |  |  |       const dst = new PNG({ width: 1, height: 1 }); | 
					
						
							|  |  |  |       PNG.bitblt(decoded, dst, x, y, 1, 1); | 
					
						
							|  |  |  |       const pixels = dst.data; | 
					
						
							|  |  |  |       return { r: pixels[0], g: pixels[1], b: pixels[2], a: pixels[3] }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(pixel(0, 0).r).toBeGreaterThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 0).b).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 999).r).toBeLessThan(128); | 
					
						
							|  |  |  |     expect(pixel(0, 999).b).toBeGreaterThan(128); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-02 16:55:32 +01:00
										 |  |  |   browserTest('should work if the main resource hangs', async ({ browser, browserName, mode, server }) => { | 
					
						
							|  |  |  |     browserTest.skip(mode !== 'default'); | 
					
						
							| 
									
										
										
										
											2023-01-27 21:52:34 -08:00
										 |  |  |     browserTest.skip(browserName === 'chromium', 'https://github.com/microsoft/playwright/issues/9757'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-02 16:55:32 +01:00
										 |  |  |     const page = await browser.newPage(); | 
					
						
							|  |  |  |     server.setRoute('/slow', (req, res) => { | 
					
						
							|  |  |  |       res.writeHead(200, { | 
					
						
							|  |  |  |         'content-length': 4096, | 
					
						
							|  |  |  |         'content-type': 'text/html', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/slow', { timeout: 1000 }).catch(() => {}); | 
					
						
							|  |  |  |       const screenshot = await page.screenshot(); | 
					
						
							|  |  |  |       expect(screenshot).toMatchSnapshot('hanging-main-resource.png'); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await page.close(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   browserTest('should capture full element when larger than viewport with device scale factor', async ({ browser }) => { | 
					
						
							|  |  |  |     const context = await browser.newContext({ viewport: { width: 501, height: 501 }, deviceScaleFactor: 2.5 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.setContent(`
 | 
					
						
							|  |  |  |       <div style="height: 14px">oooo</div> | 
					
						
							|  |  |  |       <style> | 
					
						
							|  |  |  |       div.to-screenshot { | 
					
						
							|  |  |  |         border: 4px solid red; | 
					
						
							|  |  |  |         box-sizing: border-box; | 
					
						
							|  |  |  |         width: 600px; | 
					
						
							|  |  |  |         height: 600px; | 
					
						
							|  |  |  |         margin-left: 50px; | 
					
						
							|  |  |  |         background: rgb(0, 100, 200); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       ::-webkit-scrollbar{ | 
					
						
							|  |  |  |         display: none; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       </style> | 
					
						
							|  |  |  |       <div class="to-screenshot"></div> | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |     const screenshot = await page.locator('div.to-screenshot').screenshot(); | 
					
						
							|  |  |  |     expect(screenshot).toMatchSnapshot('element-larger-than-viewport-dsf.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |   browserTest('should capture full element when larger than viewport with device scale factor and scale:css', async ({ browser }) => { | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     const context = await browser.newContext({ viewport: { width: 501, height: 501 }, deviceScaleFactor: 2.5 }); | 
					
						
							|  |  |  |     const page = await context.newPage(); | 
					
						
							|  |  |  |     await page.setContent(`
 | 
					
						
							|  |  |  |       <div style="height: 14px">oooo</div> | 
					
						
							|  |  |  |       <style> | 
					
						
							|  |  |  |       div.to-screenshot { | 
					
						
							|  |  |  |         border: 4px solid red; | 
					
						
							|  |  |  |         box-sizing: border-box; | 
					
						
							|  |  |  |         width: 600px; | 
					
						
							|  |  |  |         height: 600px; | 
					
						
							|  |  |  |         margin-left: 50px; | 
					
						
							|  |  |  |         background: rgb(0, 100, 200); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       ::-webkit-scrollbar{ | 
					
						
							|  |  |  |         display: none; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       </style> | 
					
						
							|  |  |  |       <div class="to-screenshot"></div> | 
					
						
							|  |  |  |     `);
 | 
					
						
							| 
									
										
										
										
											2022-04-01 12:28:40 -07:00
										 |  |  |     const screenshot = await page.locator('div.to-screenshot').screenshot({ scale: 'css' }); | 
					
						
							| 
									
										
										
										
											2022-03-10 13:07:10 -08:00
										 |  |  |     expect(screenshot).toMatchSnapshot('element-larger-than-viewport-dsf-css-size.png'); | 
					
						
							|  |  |  |     await context.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-05-05 19:10:28 -07:00
										 |  |  | }); |