Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/playwright-core/src/client/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
context._forReuse = true;
if (options.logger)
context._logger = options.logger;
if (options.unhandledDialogCallback)
context._unhandledDialogCallback = options.unhandledDialogCallback;
await context._initializeHarFromOptions(options.recordHar);
await this._instrumentation.runAfterCreateBrowserContext(context);
return context;
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
_timeoutSettings: TimeoutSettings;
_ownerPage: Page | undefined;
_forReuse = false;
_unhandledDialogCallback: ((dialog: Dialog) => void) | undefined;

private _closedPromise: Promise<void>;
readonly _options: channels.BrowserNewContextParams;

Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export type BrowserContextOptions = Omit<channels.BrowserNewContextOptions, 'vie
contrast?: 'more' | 'no-preference' | null;
acceptDownloads?: boolean;
clientCertificates?: ClientCertificate[];
unhandledDialogCallback?: (dialog: import('./dialog').Dialog) => void;
};

type LaunchOverrides = {
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export abstract class BrowserContext extends SdkObject {
this.fetchRequest = new BrowserContextAPIRequestContext(this);
this.tracing = new Tracing(this, browser.options.tracesDir);
this.clock = new Clock(this);
this.dialogManager = new DialogManager(this.instrumentation);
this.dialogManager = new DialogManager(this.instrumentation, this._options.unhandledDialogCallback);
}

isPersistentContext(): boolean {
Expand Down
9 changes: 7 additions & 2 deletions packages/playwright-core/src/server/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ export class Dialog extends SdkObject {

export class DialogManager {
private _instrumentation: Instrumentation;
private _unhandledDialogCallback?: (dialog: Dialog) => void;
private _dialogHandlers = new Set<(dialog: Dialog) => boolean>();
private _openedDialogs = new Set<Dialog>();

constructor(instrumentation: Instrumentation) {
constructor(instrumentation: Instrumentation, unhandledDialogCallback?: (dialog: Dialog) => void) {
this._instrumentation = instrumentation;
this._unhandledDialogCallback = unhandledDialogCallback;
}

dialogDidOpen(dialog: Dialog) {
Expand All @@ -101,8 +103,11 @@ export class DialogManager {
if (handler(dialog))
hasHandlers = true;
}
if (!hasHandlers)
if (!hasHandlers) {
if (this._unhandledDialogCallback)
this._unhandledDialogCallback(dialog);
dialog.close().then(() => {});
}
}

dialogWillClose(dialog: Dialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,4 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
this._context.clock.resumeNoReply();
this._clockPaused = false;
}
}
}
1 change: 1 addition & 0 deletions packages/playwright-core/src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export type LaunchOptions = Omit<channels.BrowserTypeLaunchParams, 'timeout'> &
export type BrowserContextOptions = channels.BrowserNewContextOptions & {
proxyOverride?: ProxySettings;
internalIgnoreHTTPSErrors?: boolean;
unhandledDialogCallback?: (dialog: import('../server/dialog').Dialog) => void;

Choose a reason for hiding this comment

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

move the type here and then import Dialog into server/dialog

};

export type ProtocolLogger = (direction: 'send' | 'receive', message: object) => void;
Expand Down