@@ -46,6 +46,67 @@ export class Page {
46
46
return this . #protocol. socket ;
47
47
}
48
48
49
+ /**
50
+ * Tells Sinco you are expecting a dialog, so Sinco can listen for the event,
51
+ * and when `.dialog()` is called, Sinco can accept or decline it at the right time
52
+ *
53
+ * @example
54
+ * ```js
55
+ * // Note that if `.click()` produces a dialog, do not await it.
56
+ * await page.expectDialog();
57
+ * await elem.click();
58
+ * await page.dialog(true, "my username is Sinco");
59
+ * ```
60
+ */
61
+ public expectDialog ( ) {
62
+ this . #protocol. notifications . set (
63
+ "Page.javascriptDialogOpening" ,
64
+ deferred ( ) ,
65
+ ) ;
66
+ }
67
+
68
+ /**
69
+ * Interact with a dialog.
70
+ *
71
+ * Will throw if `.expectDialog()` was not called before.
72
+ * This is so Sino doesn't try to accept/decline a dialog before
73
+ * it opens.
74
+ *
75
+ * @example
76
+ * ```js
77
+ * // Note that if `.click()` produces a dialog, do not await it.
78
+ * await page.expectDialog();
79
+ * elem.click();
80
+ * await page.dialog(true, "my username is Sinco");
81
+ * ```
82
+ *
83
+ * @param accept - Whether to accept or dismiss the dialog
84
+ * @param promptText - The text to enter into the dialog prompt before accepting. Used only if this is a prompt dialog.
85
+ */
86
+ public async dialog ( accept : boolean , promptText ?: string ) {
87
+ const p = this . #protocol. notifications . get ( "Page.javascriptDialogOpening" ) ;
88
+ if ( ! p ) {
89
+ throw new Error (
90
+ `Trying to accept or decline a dialog without you expecting one. ".expectDialog()" was not called beforehand.` ,
91
+ ) ;
92
+ }
93
+ await p ;
94
+ const method = "Page.javascriptDialogClosed" ;
95
+ this . #protocol. notifications . set ( method , deferred ( ) ) ;
96
+ const body : Protocol . Page . HandleJavaScriptDialogRequest = {
97
+ accept,
98
+ } ;
99
+ if ( promptText ) {
100
+ body . promptText = promptText ;
101
+ }
102
+ await this . #protocol. send <
103
+ Protocol . Page . HandleJavaScriptDialogRequest ,
104
+ null
105
+ > ( "Page.handleJavaScriptDialog" , body ) ;
106
+ const closedPromise = this . #protocol. notifications . get ( method ) ;
107
+ await closedPromise ;
108
+ }
109
+
49
110
/**
50
111
* Closes the page. After, you will not be able to interact with it
51
112
*/
0 commit comments