Skip to content

Commit 563254a

Browse files
authored
docs: add Page.onceDialog for java (#5706) (#5710)
1 parent c6a2901 commit 563254a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/src/api/java.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
## method: Page.onceDialog
2+
* langs: java
3+
4+
Adds one-off [Dialog] handler. The handler will be removed immediately after next [Dialog] is created.
5+
```java
6+
page.onceDialog(dialog -> {
7+
dialog.accept("foo");
8+
});
9+
10+
// prints 'foo'
11+
System.out.println(page.evaluate("prompt('Enter string:')"));
12+
13+
// prints 'null' as the dialog will be auto-dismissed because there are no handlers.
14+
System.out.println(page.evaluate("prompt('Enter string:')"));
15+
```
16+
17+
This code above is equivalent to:
18+
```java
19+
Consumer<Dialog> handler = new Consumer<Dialog>() {
20+
@Override
21+
public void accept(Dialog dialog) {
22+
dialog.accept("foo");
23+
page.offDialog(this);
24+
}
25+
};
26+
page.onDialog(handler);
27+
28+
// prints 'foo'
29+
System.out.println(page.evaluate("prompt('Enter string:')"));
30+
31+
// prints 'null' as the dialog will be auto-dismissed because there are no handlers.
32+
System.out.println(page.evaluate("prompt('Enter string:')"));
33+
```
34+
35+
### param: Page.onceDialog.handler
36+
- `handler` <[function]\([Dialog]\)>
37+
38+
Receives the [Dialog] object, it **must** either [`method: Dialog.accept`] or [`method: Dialog.dismiss`] the dialog - otherwise
39+
the page will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#never_blocking) waiting for the dialog,
40+
and actions like click will never finish.
41+
142
## method: Playwright.close
243
* langs: java
344

0 commit comments

Comments
 (0)