playwright_rs/protocol/
wait_for.rs1use serde::Serialize;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
13#[serde(rename_all = "lowercase")]
14pub enum WaitForState {
15 Attached,
17 Detached,
19 Visible,
21 Hidden,
23}
24
25#[derive(Debug, Clone, Default)]
51pub struct WaitForOptions {
52 pub state: Option<WaitForState>,
54 pub timeout: Option<f64>,
56}
57
58impl WaitForOptions {
59 pub fn builder() -> WaitForOptionsBuilder {
61 WaitForOptionsBuilder::default()
62 }
63
64 pub(crate) fn to_json(&self) -> serde_json::Value {
66 let mut json = serde_json::json!({});
67
68 let state = self.state.unwrap_or(WaitForState::Visible);
70 json["state"] =
71 serde_json::to_value(state).expect("serialization of WaitForState cannot fail");
72
73 if let Some(timeout) = self.timeout {
75 json["timeout"] = serde_json::json!(timeout);
76 } else {
77 json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
78 }
79
80 json
81 }
82}
83
84#[derive(Debug, Clone, Default)]
88pub struct WaitForOptionsBuilder {
89 state: Option<WaitForState>,
90 timeout: Option<f64>,
91}
92
93impl WaitForOptionsBuilder {
94 pub fn state(mut self, state: WaitForState) -> Self {
96 self.state = Some(state);
97 self
98 }
99
100 pub fn timeout(mut self, timeout: f64) -> Self {
102 self.timeout = Some(timeout);
103 self
104 }
105
106 pub fn build(self) -> WaitForOptions {
108 WaitForOptions {
109 state: self.state,
110 timeout: self.timeout,
111 }
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test_wait_for_state_serialization() {
121 assert_eq!(
122 serde_json::to_string(&WaitForState::Attached).unwrap(),
123 "\"attached\""
124 );
125 assert_eq!(
126 serde_json::to_string(&WaitForState::Detached).unwrap(),
127 "\"detached\""
128 );
129 assert_eq!(
130 serde_json::to_string(&WaitForState::Visible).unwrap(),
131 "\"visible\""
132 );
133 assert_eq!(
134 serde_json::to_string(&WaitForState::Hidden).unwrap(),
135 "\"hidden\""
136 );
137 }
138
139 #[test]
140 fn test_wait_for_options_default_state() {
141 let options = WaitForOptions::builder().build();
143 let json = options.to_json();
144 assert_eq!(json["state"], "visible");
145 assert!(json["timeout"].is_number());
146 }
147
148 #[test]
149 fn test_wait_for_options_all_states() {
150 for (state, expected) in &[
151 (WaitForState::Attached, "attached"),
152 (WaitForState::Detached, "detached"),
153 (WaitForState::Visible, "visible"),
154 (WaitForState::Hidden, "hidden"),
155 ] {
156 let options = WaitForOptions::builder().state(*state).build();
157 let json = options.to_json();
158 assert_eq!(json["state"], *expected);
159 }
160 }
161
162 #[test]
163 fn test_wait_for_options_timeout() {
164 let options = WaitForOptions::builder().timeout(5000.0).build();
165 let json = options.to_json();
166 assert_eq!(json["timeout"], 5000.0);
167 }
168}