1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! `DialogJs` — QuickJS binding for [`ferridriver::dialog::Dialog`].
//!
//! Mirrors Playwright's client-side `Dialog` class from
//! `/tmp/playwright/packages/playwright-core/src/client/dialog.ts`:
//! read-only accessors (`type()`, `message()`, `defaultValue()`) +
//! async `accept(promptText?)` / `dismiss()`.
use ferridriver::dialog::Dialog as CoreDialog;
use rquickjs::JsLifetime;
use rquickjs::class::Trace;
use rquickjs::function::Opt;
use crate::bindings::convert::FerriResultExt;
#[derive(JsLifetime, Trace)]
#[rquickjs::class(rename = "Dialog")]
pub struct DialogJs {
#[qjs(skip_trace)]
inner: CoreDialog,
}
impl DialogJs {
#[must_use]
pub fn new(inner: CoreDialog) -> Self {
Self { inner }
}
}
#[rquickjs::methods]
impl DialogJs {
/// Playwright: `dialog.type(): string` — `"alert"` / `"beforeunload"`
/// / `"confirm"` / `"prompt"`.
#[qjs(rename = "type")]
pub fn dialog_type(&self) -> String {
self.inner.dialog_type().as_str().to_string()
}
/// Playwright: `dialog.message(): string`.
#[qjs(rename = "message")]
pub fn message(&self) -> String {
self.inner.message().to_string()
}
/// Playwright: `dialog.defaultValue(): string`. Empty for non-prompt
/// dialogs.
#[qjs(rename = "defaultValue")]
pub fn default_value(&self) -> String {
self.inner.default_value().to_string()
}
/// Playwright: `dialog.page(): Page | null`. `null` when the dialog
/// opened before its page was available (early page initialization)
/// or the owning page has been closed.
#[qjs(rename = "page")]
pub fn page(&self) -> Option<crate::bindings::page::PageJs> {
self.inner.page().map(crate::bindings::page::PageJs::new)
}
/// Playwright: `dialog.accept(promptText?): Promise<void>`.
/// `promptText` is applied to `prompt` dialogs only; other types
/// ignore it. Double-accept / accept-after-dismiss rejects with the
/// Playwright-exact message `"Cannot accept dialog which is already
/// handled!"`.
#[qjs(rename = "accept")]
pub async fn accept(&self, prompt_text: Opt<String>) -> rquickjs::Result<()> {
self.inner.accept(prompt_text.0).await.into_js()
}
/// Playwright: `dialog.dismiss(): Promise<void>`.
#[qjs(rename = "dismiss")]
pub async fn dismiss(&self) -> rquickjs::Result<()> {
self.inner.dismiss().await.into_js()
}
}