fix(screenshotter): only wait for for document.fonts.ready on locator frame / page main frame (#31295)

**Investigation**

~~We use `nonStallingEvaluateInExistingContext` as of today, which does
`eval()` inside (from our utilityScript) which breaks for some sites. It
causes a hang, since the returned `Promise` of `eval()` hangs. We don't
know as of today why this happens. Without wrapping it ini `eval()` it
does not hang.~~

~~Workaround: Do a plain Runtime.evaluate instead.~~

workaround: Only wait on main frame.

Relates https://github.com/microsoft/playwright/issues/28995 (keeping it
open until they confirm that it helps)
This commit is contained in:
Max Schmitt 2024-06-14 01:22:14 +02:00 committed by GitHub
parent 4fff548fc6
commit a9200be0af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -204,7 +204,7 @@ export class Screenshotter {
return this._queue.postTask(async () => {
progress.log('taking page screenshot');
const { viewportSize } = await this._originalViewportSize(progress);
await this._preparePageForScreenshot(progress, options.style, options.caret !== 'initial', options.animations === 'disabled');
await this._preparePageForScreenshot(progress, this._page.mainFrame(), options.style, options.caret !== 'initial', options.animations === 'disabled');
progress.throwIfAborted(); // Avoid restoring after failure - should be done by cleanup.
if (options.fullPage) {
@ -233,7 +233,7 @@ export class Screenshotter {
progress.log('taking element screenshot');
const { viewportSize } = await this._originalViewportSize(progress);
await this._preparePageForScreenshot(progress, options.style, options.caret !== 'initial', options.animations === 'disabled');
await this._preparePageForScreenshot(progress, handle._frame, options.style, options.caret !== 'initial', options.animations === 'disabled');
progress.throwIfAborted(); // Do not do extra work.
await handle._waitAndScrollIntoViewIfNeeded(progress, true /* waitForVisible */);
@ -257,7 +257,7 @@ export class Screenshotter {
});
}
async _preparePageForScreenshot(progress: Progress, screenshotStyle: string | undefined, hideCaret: boolean, disableAnimations: boolean) {
async _preparePageForScreenshot(progress: Progress, frame: Frame, screenshotStyle: string | undefined, hideCaret: boolean, disableAnimations: boolean) {
if (disableAnimations)
progress.log(' disabled all CSS animations');
const syncAnimations = this._page._delegate.shouldToggleStyleSheetToSyncAnimations();
@ -266,9 +266,7 @@ export class Screenshotter {
}));
if (!process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY) {
progress.log('waiting for fonts to load...');
await Promise.all(this._page.frames().map(async frame => {
await frame.nonStallingEvaluateInExistingContext('document.fonts.ready', false, 'utility').catch(() => {});
}));
await frame.nonStallingEvaluateInExistingContext('document.fonts.ready', false, 'utility').catch(() => {});
progress.log('fonts loaded');
}
progress.cleanupWhenAborted(() => this._restorePageAfterScreenshot());