Skip to content

Commit 263a0fd

Browse files
authored
fix: evaluate in utility for screenshots (#6364)
We use `waitForFunctionValue` in the main world that may be corrupted. References #6356.
1 parent a456131 commit 263a0fd

File tree

8 files changed

+17
-10
lines changed

8 files changed

+17
-10
lines changed

src/server/firefox/ffPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export class FFPage implements PageDelegate {
398398

399399
async takeScreenshot(progress: Progress, format: 'png' | 'jpeg', documentRect: types.Rect | undefined, viewportRect: types.Rect | undefined, quality: number | undefined): Promise<Buffer> {
400400
if (!documentRect) {
401-
const scrollOffset = await this._page.mainFrame().waitForFunctionValue(progress, () => ({ x: window.scrollX, y: window.scrollY }));
401+
const scrollOffset = await this._page.mainFrame().waitForFunctionValueInUtility(progress, () => ({ x: window.scrollX, y: window.scrollY }));
402402
documentRect = {
403403
x: viewportRect!.x + scrollOffset.x,
404404
y: viewportRect!.y + scrollOffset.y,

src/server/frames.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ export class Frame extends SdkObject {
10921092
}, this._page._timeoutSettings.timeout(options));
10931093
}
10941094

1095-
async _waitForFunctionExpression<R>(metadata: CallMetadata, expression: string, isFunction: boolean | undefined, arg: any, options: types.WaitForFunctionOptions = {}): Promise<js.SmartHandle<R>> {
1095+
async _waitForFunctionExpression<R>(metadata: CallMetadata, expression: string, isFunction: boolean | undefined, arg: any, options: types.WaitForFunctionOptions, world: types.World = 'main'): Promise<js.SmartHandle<R>> {
10961096
const controller = new ProgressController(metadata, this);
10971097
if (typeof options.pollingInterval === 'number')
10981098
assert(options.pollingInterval > 0, 'Cannot poll with non-positive interval: ' + options.pollingInterval);
@@ -1116,19 +1116,18 @@ export class Frame extends SdkObject {
11161116
return injectedScript.pollInterval(polling, (progress, continuePolling) => predicate(arg) || continuePolling);
11171117
}, { expression, isFunction, polling: options.pollingInterval, arg });
11181118
return controller.run(
1119-
progress => this._scheduleRerunnableHandleTask(progress, 'main', task),
1119+
progress => this._scheduleRerunnableHandleTask(progress, world, task),
11201120
this._page._timeoutSettings.timeout(options));
11211121
}
11221122

1123-
async waitForFunctionValue<R>(progress: Progress, pageFunction: js.Func1<any, R>) {
1123+
async waitForFunctionValueInUtility<R>(progress: Progress, pageFunction: js.Func1<any, R>) {
11241124
const expression = `() => {
11251125
const result = (${pageFunction})();
11261126
if (!result)
11271127
return result;
11281128
return JSON.stringify(result);
1129-
11301129
}`;
1131-
const handle = await this._page.mainFrame()._waitForFunctionExpression(internalCallMetadata(), expression, true, undefined, { timeout: progress.timeUntilDeadline() });
1130+
const handle = await this._waitForFunctionExpression(internalCallMetadata(), expression, true, undefined, { timeout: progress.timeUntilDeadline() }, 'utility');
11321131
return JSON.parse(handle.rawValue()) as R;
11331132
}
11341133

src/server/injected/selectorEvaluator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class SelectorEvaluatorImpl implements SelectorEvaluator {
7373
const parserNames = Array.from(customCSSNames).slice();
7474
parserNames.sort();
7575
if (allNames.join('|') !== parserNames.join('|'))
76-
throw new Error(`Please keep customCSSNames in sync with evaluator engines`);
76+
throw new Error(`Please keep customCSSNames in sync with evaluator engines: ${allNames.join('|')} vs ${parserNames.join('|')}`);
7777
}
7878

7979
begin() {

src/server/screenshotter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export class Screenshotter {
3535
const originalViewportSize = this._page.viewportSize();
3636
let viewportSize = originalViewportSize;
3737
if (!viewportSize)
38-
viewportSize = await this._page.mainFrame().waitForFunctionValue(progress, () => ({ width: window.innerWidth, height: window.innerHeight }));
38+
viewportSize = await this._page.mainFrame().waitForFunctionValueInUtility(progress, () => ({ width: window.innerWidth, height: window.innerHeight }));
3939
return { viewportSize, originalViewportSize };
4040
}
4141

4242
private async _fullPageSize(progress: Progress): Promise<types.Size> {
43-
const fullPageSize = await this._page.mainFrame().waitForFunctionValue(progress, () => {
43+
const fullPageSize = await this._page.mainFrame().waitForFunctionValueInUtility(progress, () => {
4444
if (!document.body || !document.documentElement)
4545
return null;
4646
return {
@@ -122,7 +122,7 @@ export class Screenshotter {
122122
}
123123

124124
progress.throwIfAborted(); // Avoid extra work.
125-
const scrollOffset = await this._page.mainFrame().waitForFunctionValue(progress, () => ({ x: window.scrollX, y: window.scrollY }));
125+
const scrollOffset = await this._page.mainFrame().waitForFunctionValueInUtility(progress, () => ({ x: window.scrollX, y: window.scrollY }));
126126
const documentRect = { ...boundingBox };
127127
documentRect.x += scrollOffset.x;
128128
documentRect.y += scrollOffset.y;
73.3 KB
Loading
54.5 KB
Loading
81.1 KB
Loading

tests/page-screenshot.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ it.describe('page screenshot', () => {
268268
await page.screenshot();
269269
expect(resizeTriggered).toBeFalsy();
270270
});
271+
272+
it('should work with Array deleted', async ({page, server}) => {
273+
await page.setViewportSize({width: 500, height: 500});
274+
await page.goto(server.PREFIX + '/grid.html');
275+
await page.evaluate(() => delete window.Array);
276+
const screenshot = await page.screenshot({ fullPage: true });
277+
expect(screenshot).toMatchSnapshot('screenshot-grid-fullpage.png');
278+
});
271279
});
272280

273281
browserTest.describe('page screenshot', () => {

0 commit comments

Comments
 (0)