Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playwright-core/src/tools/backend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export class Context {
await this.newTab();
if (crashed)
this._currentTab!.logErrorMessage('Page crashed and was reset to about:blank.');
await this._currentTab!.waitForInitialized();
return this._currentTab!;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-core/src/tools/backend/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export class Tab extends EventEmitter<TabEventsInterface> {
this._consoleLog.stop();
}

async waitForInitialized() {
await this._initializedPromise;
}

static forPage(page: playwright.Page): Tab | undefined {
// eslint-disable-next-line no-restricted-syntax
return (page as any)[tabSymbol];
Expand Down
21 changes: 21 additions & 0 deletions tests/mcp/init-page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ test('init-page surfaces load errors instead of silently dropping them', async (
expect(JSON.stringify(response.content)).toContain('boom from initPage');
});

test('init-page is applied before the first page screenshot', async ({ startClient }) => {
const initPagePath = test.info().outputPath('screenshotInitPage.ts');
await fs.promises.writeFile(initPagePath, `
export default async ({ page }) => {
page.screenshot = async () => {
throw new Error('initPage screenshot hook applied');
};
};
`);

const { client } = await startClient({
args: [`--init-page=${initPagePath}`],
});
const response: any = await client.callTool({
name: 'browser_take_screenshot',
arguments: {},
});
expect(response.isError).toBe(true);
expect(JSON.stringify(response.content)).toContain('initPage screenshot hook applied');
});

test('--init-page w/ --init-script', async ({ startClient, server }) => {
server.setContent('/', `
<div>Hello world</div>
Expand Down
Loading