From 563254a968fa117e23b6595858983bc79cfcb80e Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 3 Mar 2021 14:30:08 -0800 Subject: [PATCH] docs: add Page.onceDialog for java (#5706) (#5710) --- docs/src/api/java.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/docs/src/api/java.md b/docs/src/api/java.md index 6a09f453481f8..fba3bccf85d8f 100644 --- a/docs/src/api/java.md +++ b/docs/src/api/java.md @@ -1,3 +1,44 @@ +## method: Page.onceDialog +* langs: java + +Adds one-off [Dialog] handler. The handler will be removed immediately after next [Dialog] is created. +```java +page.onceDialog(dialog -> { + dialog.accept("foo"); +}); + +// prints 'foo' +System.out.println(page.evaluate("prompt('Enter string:')")); + +// prints 'null' as the dialog will be auto-dismissed because there are no handlers. +System.out.println(page.evaluate("prompt('Enter string:')")); +``` + +This code above is equivalent to: +```java +Consumer handler = new Consumer() { + @Override + public void accept(Dialog dialog) { + dialog.accept("foo"); + page.offDialog(this); + } +}; +page.onDialog(handler); + +// prints 'foo' +System.out.println(page.evaluate("prompt('Enter string:')")); + +// prints 'null' as the dialog will be auto-dismissed because there are no handlers. +System.out.println(page.evaluate("prompt('Enter string:')")); +``` + +### param: Page.onceDialog.handler +- `handler` <[function]\([Dialog]\)> + +Receives the [Dialog] object, it **must** either [`method: Dialog.accept`] or [`method: Dialog.dismiss`] the dialog - otherwise +the page will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog, +and actions like click will never finish. + ## method: Playwright.close * langs: java