diff --git a/docs/src/dialogs.md b/docs/src/dialogs.md index 1d8938778c..89f90a41c7 100644 --- a/docs/src/dialogs.md +++ b/docs/src/dialogs.md @@ -5,7 +5,7 @@ title: "Dialogs" ## Introduction -Playwright can interact with the web page dialogs such as [`alert`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert), [`confirm`](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm), [`prompt`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) as well as [`beforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) confirmation. +Playwright can interact with the web page dialogs such as [`alert`](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert), [`confirm`](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm), [`prompt`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) as well as [`beforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) confirmation. For print dialogs, see [Print](#print-dialogs). ## alert(), confirm(), prompt() dialogs @@ -126,3 +126,59 @@ Page.Dialog += async (_, dialog) => }; await Page.CloseAsync(new() { RunBeforeUnload = true }); ``` + +## Print dialogs + +In order to assert that a print dialog via [`window.print`](https://developer.mozilla.org/en-US/docs/Web/API/Window/print) was opened, you can use the following snippet: + +```js +let resolveWaitForPrintDialog; +const waitForPrintDialog = new Promise(x => resolveWaitForPrintDialog = x); +await page.exposeBinding('print', resolveWaitForPrintDialog); + +await page.goto(''); +await page.getByText('Print it!').click(); + +await waitForPrintDialog; +``` + +```java +CompletableFuture waitForPrintDialog = new CompletableFuture<>(); +page.exposeBinding("print", () -> waitForPrintDialog.complete(null)); + +page.navigate(""); +page.getByText("Print it!").click(); + +waitForPrintDialog.get(); +``` + +```python async +import asyncio + +wait_for_dialog_fut = asyncio.Future() +page.expose_binding("print", lambda: wait_for_dialog_fut.set_result(None)) + +await page.goto("") +await page.get_by_text("Print it!").click() + +await wait_for_dialog_fut +``` + +```python sync +page.evaluate("window.waitForPrintDialog = new Promise(resolve => window.print = resolve);") + +page.goto("") +page.get_by_text("Print it!").click() + +page.evaluate("window.waitForPrintDialog") +``` + +```csharp +var waitForPrintDialog = new TaskCompletionSource(); +await Page.ExposeBindingAsync("print", () => waitForPrintDialog.SetResult(null)); + +await Page.GotoAsync(""); +await Page.GetByText("Print it!").ClickAsync(); + +await waitForPrintDialog.Task; +```