Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ This works out of the box in every mode, including the default persistent-profil
Record a session once with Playwright's codegen, then hand the file to the server:

```bash
npx playwright@1.61.1 codegen --save-storage=auth.json https://example.com/login
npx playwright@1.62.0 codegen --save-storage=auth.json https://example.com/login
```

Sign in in the opened browser, then close it — `auth.json` now holds the cookies and local storage.
Expand Down Expand Up @@ -571,6 +571,7 @@ Wait for text to appear/disappear or time to pass.
#### `browser_handle_dialog`
Handle browser dialogs (alerts, confirms, prompts).
- Parameters: `accept` (boolean), `promptText` (optional)
- If the dialog was already closed outside the session (e.g. dismissed manually in a headed browser), the call succeeds, reports the dialog as already closed, and clears its leftover state instead of failing.

#### `browser_file_upload`
Upload files to the page.
Expand Down
2 changes: 1 addition & 1 deletion SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Crawl and aggregate accessibility violations across multiple pages. Writes a JSO
- `excludePathPatterns` - Regex patterns to skip (default: `logout|signout`)
- `reportFile` - Custom output filename

**Behind a login:** the crawl shares the browser context, so signing in with `browser_navigate` / `browser_fill_form` first is enough. Alternatively record a session with `npx playwright@1.61.1 codegen --save-storage=auth.json <login-url>` (pinned to the server's own Playwright, so the recorded state matches what it replays) and start the server with `--storage-state ./auth.json` — every mode applies it (fresh contexts receive it at creation; the persistent profile runs in a fresh disposable profile built from the state and removed when the session closes, so it cannot combine with `--user-data-dir`; plain CDP attach installs it with `setStorageState()`, which resets cookies fully and origin storage for the state's origins plus any origins the connection has seen — origins from older history it never saw survive) except `--extension`, which errors out rather than wiping the browser you are already running. Extend `excludePathPatterns` with anything else that ends the session (account deletion, session revocation, account/locale switchers) — it replaces the default, so keep `logout|signout` in the list. If the crawl loses cookies it started with, the result opens with `WARNING: cookie(s) …` lines and the report carries a `sessionLosses` list naming, per lost cookie, the page reached after any redirect, even when that page failed to load; if one of them was the session cookie, everything scanned after that page was audited signed-out. Monitoring continues past the first loss (each cookie is reported once, where it vanished), and URLs discovered mid-crawl join cookie tracking before being visited. Cookie values are not compared and a cookie the browser dropped at its own expiry is ignored, but any other cookie the crawl started with and lost is reported — nothing marks a cookie as an authentication one.
**Behind a login:** the crawl shares the browser context, so signing in with `browser_navigate` / `browser_fill_form` first is enough. Alternatively record a session with `npx playwright@1.62.0 codegen --save-storage=auth.json <login-url>` (pinned to the server's own Playwright, so the recorded state matches what it replays) and start the server with `--storage-state ./auth.json` — every mode applies it (fresh contexts receive it at creation; the persistent profile runs in a fresh disposable profile built from the state and removed when the session closes, so it cannot combine with `--user-data-dir`; plain CDP attach installs it with `setStorageState()`, which resets cookies fully and origin storage for the state's origins plus any origins the connection has seen — origins from older history it never saw survive) except `--extension`, which errors out rather than wiping the browser you are already running. Extend `excludePathPatterns` with anything else that ends the session (account deletion, session revocation, account/locale switchers) — it replaces the default, so keep `logout|signout` in the list. If the crawl loses cookies it started with, the result opens with `WARNING: cookie(s) …` lines and the report carries a `sessionLosses` list naming, per lost cookie, the page reached after any redirect, even when that page failed to load; if one of them was the session cookie, everything scanned after that page was audited signed-out. Monitoring continues past the first loss (each cookie is reported once, where it vanished), and URLs discovered mid-crawl join cookie tracking before being visited. Cookie values are not compared and a cookie the browser dropped at its own expiry is ignored, but any other cookie the crawl started with and lost is reported — nothing marks a cookie as an authentication one.

### scan_page_matrix

Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"debug": "^4.4.3",
"dotenv": "^17.4.2",
"mime": "^4.1.0",
"playwright": "1.61.1",
"playwright-core": "1.61.1",
"playwright": "1.62.0",
"playwright-core": "1.62.0",
Comment on lines +53 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Pin a version that emits dialogclosed

When a dialog is dismissed manually or through a CDP side channel—the scenario this change is intended to fix—the pinned Playwright 1.62.0 runtime never emits dialogclosed (as the new comment in src/tab.ts also notes). Consequently, the new listener cannot clear the stale modal state and snapshot-bearing tools remain blocked. Pin both Playwright packages to a version that provides this event, such as 1.63+, or implement a fallback supported by 1.62.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 4ac5499, taking the fallback option rather than the version bump: dialogclosed only exists in 1.63.0-alpha builds (it merged upstream after the 1.62 branch cut — verified against the published npm packages, including the 1.62.1 betas), so pinning a version that emits it would mean shipping an alpha in a stable package.

The fallback works on the pinned 1.62.0: an out-of-band-closed dialog fails accept()/dismiss() with Protocol error (Page.handleJavaScriptDialog): No dialog is showing (or the already handled client-side guard on a repeat), both verified against 1.62.0 Chromium. browser_handle_dialog now treats those as a successful close — it reports the dialog as already closed and the modal state stays cleared, so snapshot-bearing tools unblock. A real-browser test in tests/tools-dialogs.test.ts reproduces the full scenario via a CDP side-channel close. The dialogclosed listener stays in place and takes over as the immediate path once the pin reaches 1.63+.


Generated by Claude Code

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
"re2": "^1.24.1",
"ws": "^8.21.1",
"zod": "^4.4.3"
Expand Down
13 changes: 13 additions & 0 deletions src/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export class Tab extends EventEmitter<TabEventsInterface> {
});
});
listen('dialog', dialog => this._dialogShown(dialog));
// Fires on Playwright >= 1.63 when a dialog is closed out of band (e.g.
// dismissed manually in headed mode or via a CDP side-channel); inert on
// the pinned 1.62.0, which never emits it. Until then a stale dialog
// modal state blocks every snapshot-bearing tool until the user re-issues
// browser_handle_dialog, whose already-closed fallback (tools/dialogs.ts)
// recovers the session on the pinned version.
listen('dialogclosed', dialog => this._dialogClosed(dialog));
listen('download', download => {
void this._downloadStarted(download);
});
Expand Down Expand Up @@ -123,6 +130,12 @@ export class Tab extends EventEmitter<TabEventsInterface> {
});
}

private _dialogClosed(dialog: playwright.Dialog) {
const state = this._modalStates.find(state => state.type === 'dialog' && state.dialog === dialog);
if (state)
this.clearModalState(state);
}

private async _downloadStarted(download: playwright.Download) {
const entry = {
download,
Expand Down
22 changes: 18 additions & 4 deletions src/tools/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
import { z } from 'zod';
import { defineTabTool } from './tool.js';

// A dialog closed out of band (dismissed manually in headed mode, or through a
// CDP side-channel) fails accept()/dismiss() with one of these messages — the
// protocol error on the first attempt, the client-side guard on a repeat
// (both verified against Playwright 1.62.0 Chromium). The pinned Playwright
// has no dialogclosed event to observe the close directly, so this error is
// the only remaining signal that the dialog is already gone.
const dialogAlreadyClosedError = /no dialog is showing|dialog which is already handled/i;

const handleDialog = defineTabTool({
capability: 'core',

Expand All @@ -40,10 +48,16 @@ const handleDialog = defineTabTool({

tab.clearModalState(dialogState);
await tab.waitForCompletion(async () => {
if (params.accept)
await dialogState.dialog.accept(params.promptText);
else
await dialogState.dialog.dismiss();
try {
if (params.accept)
await dialogState.dialog.accept(params.promptText);
else
await dialogState.dialog.dismiss();
} catch (error) {
if (!(error instanceof Error) || !dialogAlreadyClosedError.test(error.message))
throw error;
response.addResult('The dialog was already closed out of band; cleared its leftover state.');
}
});
},

Expand Down
92 changes: 92 additions & 0 deletions tests/tab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,98 @@ describe('Tab', () => {
});
});

describe('out-of-band dialog close', () => {
const makeDialog = (message = 'Hello') => ({
type: () => 'alert',
message: () => message,
}) as any;

it('sets a dialog modal state when a dialog opens', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
mockPage.emit('dialog', makeDialog());
expect(tab.modalStates()).toHaveLength(1);
expect(tab.modalStates()[0].type).toBe('dialog');
});

it('clears the dialog modal state when the dialog closes out of band', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
const dialog = makeDialog();
mockPage.emit('dialog', dialog);
expect(tab.modalStates()).toHaveLength(1);

mockPage.emit('dialogclosed', dialog);
expect(tab.modalStates()).toEqual([]);
});

it('unblocks snapshots after the dialog closes out of band', async () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
const dialog = makeDialog();
mockPage.emit('dialog', dialog);

const blocked = await tab.captureSnapshot();
expect(blocked.ariaSnapshot).toBe('');
expect(blocked.modalStates).toHaveLength(1);

mockPage.emit('dialogclosed', dialog);

const unblocked = await tab.captureSnapshot();
expect(unblocked.ariaSnapshot).toBe('button "Submit" [ref=1]');
expect(unblocked.modalStates).toEqual([]);
});

it('only clears the state of the dialog that actually closed', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
const first = makeDialog('first');
const second = makeDialog('second');
mockPage.emit('dialog', first);
mockPage.emit('dialog', second);
expect(tab.modalStates()).toHaveLength(2);

mockPage.emit('dialogclosed', first);
expect(tab.modalStates()).toHaveLength(1);
expect(tab.modalStates()[0].description).toContain('second');
});

it('ignores a close event for a dialog it never tracked', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
const tracked = makeDialog();
mockPage.emit('dialog', tracked);

mockPage.emit('dialogclosed', makeDialog('untracked'));
expect(tab.modalStates()).toHaveLength(1);
});

it('leaves non-dialog modal states alone', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
mockPage.emit('filechooser', {});
const dialog = makeDialog();
mockPage.emit('dialog', dialog);

mockPage.emit('dialogclosed', dialog);
expect(tab.modalStates()).toHaveLength(1);
expect(tab.modalStates()[0].type).toBe('fileChooser');
});

it('is a no-op when no modal states exist', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
expect(() => mockPage.emit('dialogclosed', makeDialog())).not.toThrow();
expect(tab.modalStates()).toEqual([]);
});

it('stops listening after dispose', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
const dialog = makeDialog();
mockPage.emit('dialog', dialog);
expect(mockPage.listenerCount('dialogclosed')).toBe(1);

tab.dispose();
expect(mockPage.listenerCount('dialogclosed')).toBe(0);

mockPage.emit('dialogclosed', dialog);
expect(tab.modalStates()).toHaveLength(1);
});
});

describe('isCurrentTab', () => {
it('should return true when tab is current', () => {
const tab = new Tab(mockContext, mockPage as any, onPageClose);
Expand Down
Loading