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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use super::{Element, Page};
use crate::action::ActionCall;
use crate::error::{Error, Result};
use crate::{Action, LocatorSpec};
impl Page {
/// Run `action` with this page's [`crate::ActionDefaults`], discarding the receipt.
pub(crate) async fn act_default(&self, action: Action) -> Result<()> {
self.act(action, self.action_options()).await.map(|_| ())
}
/// Drag horizontally from the selected element's center.
/// For explicit cancellation cleanup, retain a [`crate::stealth::Human`] helper.
pub fn human_drag(&self, selector: &str, dx: f64) -> ActionCall {
ActionCall::new(
self,
Action::Drag {
target: LocatorSpec::css(selector),
dx,
dy: 0.0,
},
)
}
/// Find the first element matching a CSS selector, in document order.
/// Several matches are not an error. Top frame only.
pub async fn find(&self, selector: &str) -> Result<Element> {
crate::action::first_element(self, LocatorSpec::css(selector))
.await?
.ok_or_else(|| Error::element_not_found(selector.to_string()))
}
/// Find all elements matching a CSS selector, in document order.
pub async fn find_all(&self, selector: &str) -> Result<Vec<Element>> {
crate::action::all_elements(self, &LocatorSpec::css(selector)).await
}
/// Check if an element exists
#[must_use = "returns true if element exists"]
pub async fn exists(&self, selector: &str) -> bool {
self.find(selector).await.is_ok()
}
/// Find the first element whose text contains `text` (case-insensitive).
///
/// Interactive elements (a, button, input) win over static ones; among
/// static elements the innermost match is returned, never a container
/// that merely includes the text. Matching is the same as
/// [`Page::locator_text`], but several matches are not an error.
pub async fn find_by_text(&self, text: &str) -> Result<Element> {
crate::action::first_element(self, LocatorSpec::text(text))
.await?
.ok_or_else(|| Error::element_not_found(format!("text: {text}")))
}
/// Click an element by selector. Awaiting proves dispatch only; add
/// [`ActionCall::expecting`] to prove the page reacted.
pub fn click(&self, selector: &str) -> ActionCall {
ActionCall::new(self, Action::click(LocatorSpec::css(selector), false))
}
/// Type text into an element by selector. Awaiting proves dispatch only; see [`ActionCall`].
pub fn type_into(&self, selector: &str, text: &str) -> ActionCall {
ActionCall::new(self, Action::type_text(LocatorSpec::css(selector), text))
}
/// Fill a form field: click, clear, type, and verify the resulting value.
///
/// Range inputs are assigned through their DOM property rather than text
/// insertion. The requested value must be finite and satisfy the range's
/// min/max/step constraints; accepted range values dispatch bubbling
/// `input` and `change` events.
pub async fn fill(&self, selector: &str, value: &str) -> Result<()> {
self.act_default(Action::fill(LocatorSpec::css(selector), value))
.await
}
/// Upload file(s) to a file input element
pub async fn upload_file(&self, selector: &str, path: &str) -> Result<()> {
self.upload_files(selector, &[path]).await
}
/// Upload multiple files to a file input element
pub async fn upload_files(&self, selector: &str, paths: &[&str]) -> Result<()> {
self.act_default(Action::Upload {
target: LocatorSpec::css(selector),
files: paths.iter().map(|v| (*v).into()).collect(),
})
.await
}
/// Select option by value
pub async fn select(&self, selector: &str, value: &str) -> Result<()> {
self.act_default(Action::Select {
target: LocatorSpec::css(selector),
values: vec![value.into()],
})
.await
}
/// Select option by visible text
pub async fn select_by_text(&self, selector: &str, text: &str) -> Result<()> {
self.act_default(Action::SelectText {
target: LocatorSpec::css(selector),
text: text.into(),
})
.await
}
/// Select multiple options by values
pub async fn select_multiple(&self, selector: &str, values: &[&str]) -> Result<()> {
self.act_default(Action::Select {
target: LocatorSpec::css(selector),
values: values.iter().map(|v| (*v).into()).collect(),
})
.await
}
/// Hover over element (for revealing menus)
pub async fn hover(&self, selector: &str) -> Result<()> {
self.act_default(Action::hover(LocatorSpec::css(selector)))
.await
}
}