new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · all subjects

dialogs

11 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Dialog auto-dismissal default behavior

By default, dialogs are auto-dismissed by Playwright, so you do not have to handle them explicitly.

Dialog handler must handle dialog or action will hang

If you register a dialog listener with page.on('dialog', ...), the listener must call either dialog.accept() or dialog.dismiss() to handle the dialog. Otherwise, the action that triggered the dialog will hang because web dialogs are modals that block further page execution until handled. If there is no listener for the dialog event, all dialogs are automatically dismissed.

Dialog handler example with accept

This example shows how to register a dialog handler that accepts all dialogs: ```js page.on('dialog', dialog => dialog.accept()); await page.getByRole('button').click(); ```

Pitfall: dialog handler that does not accept or dismiss will hang

Do not register a dialog handler that only logs the message without accepting or dismissing the dialog. The following snippet will never resolve: ```js page.on('dialog', dialog => console.log(dialog.message())); await page.getByRole('button').click(); // Will hang here ```

beforeunload dialog handling

To handle a beforeunload dialog, register a dialog listener and dismiss it. When page.close() is invoked with runBeforeUnload set to true, the page runs its unload handlers and the beforeunload dialog may appear. This is the only case when page.close() does not wait for the page to actually close, because the page might stay open after the operation.

beforeunload dialog handler example

This example shows how to handle a beforeunload dialog: ```js page.on('dialog', async dialog => { assert(dialog.type() === 'beforeunload'); await dialog.dismiss(); }); await page.close({ runBeforeUnload: true }); ```

Print dialog detection technique

To assert that a print dialog via window.print() was triggered, you can override window.print to resolve a promise, then use page.waitForFunction() to wait for that promise. The script must be evaluated before clicking the button that triggers the print dialog.

Print dialog detection example

This example shows how to detect and wait for a print dialog: ```js await page.goto('<url>'); await page.evaluate('(() => {window.waitForPrintDialog = new Promise(f => window.print = f);})()'); await page.getByText('Print it!').click(); await page.waitForFunction('window.waitForPrintDialog'); ```

Wait for popup window using waitForEvent

Use page.waitForEvent('popup') to wait for a popup window to open. Start the wait before the action that triggers the popup. The method returns a promise that resolves to the Page object for the popup. Example: const popupPromise = page.waitForEvent('popup'); await page.getByText('open the popup').click(); const popup = await popupPromise; await popup.goto('https://wikipedia.org');

One-off event listener using page.once

Use page.once(eventName, callback) to register a callback that fires only once for the specified event. After handling the event, the listener is automatically removed. Example: page.once('dialog', dialog => dialog.accept('2021')); await page.evaluate("prompt('Enter a number:')");

Page dialogs auto-dismissed by default

As of Playwright 1.9, page dialogs are auto-dismissed during execution unless a listener for the `dialog` event is configured.

Give your agent this brain