Skip to content

Commit

Permalink
docs: add Page.onceDialog for java (microsoft#5706)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed Mar 3, 2021
1 parent f41d000 commit 35c7b68
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/src/api/java.md
Original file line number Diff line number Diff line change
@@ -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<Dialog> handler = new Consumer<Dialog>() {
@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

Expand Down

0 comments on commit 35c7b68

Please sign in to comment.