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"] = serde_json::to_value(state).unwrap();
71
72 if let Some(timeout) = self.timeout {
74 json["timeout"] = serde_json::json!(timeout);
75 } else {
76 json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
77 }
78
79 json
80 }
81}
82
83#[derive(Debug, Clone, Default)]
87pub struct WaitForOptionsBuilder {
88 state: Option<WaitForState>,
89 timeout: Option<f64>,
90}
91
92impl WaitForOptionsBuilder {
93 pub fn state(mut self, state: WaitForState) -> Self {
95 self.state = Some(state);
96 self
97 }
98
99 pub fn timeout(mut self, timeout: f64) -> Self {
101 self.timeout = Some(timeout);
102 self
103 }
104
105 pub fn build(self) -> WaitForOptions {
107 WaitForOptions {
108 state: self.state,
109 timeout: self.timeout,
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_wait_for_state_serialization() {
120 assert_eq!(
121 serde_json::to_string(&WaitForState::Attached).unwrap(),
122 "\"attached\""
123 );
124 assert_eq!(
125 serde_json::to_string(&WaitForState::Detached).unwrap(),
126 "\"detached\""
127 );
128 assert_eq!(
129 serde_json::to_string(&WaitForState::Visible).unwrap(),
130 "\"visible\""
131 );
132 assert_eq!(
133 serde_json::to_string(&WaitForState::Hidden).unwrap(),
134 "\"hidden\""
135 );
136 }
137
138 #[test]
139 fn test_wait_for_options_default_state() {
140 let options = WaitForOptions::builder().build();
142 let json = options.to_json();
143 assert_eq!(json["state"], "visible");
144 assert!(json["timeout"].is_number());
145 }
146
147 #[test]
148 fn test_wait_for_options_all_states() {
149 for (state, expected) in &[
150 (WaitForState::Attached, "attached"),
151 (WaitForState::Detached, "detached"),
152 (WaitForState::Visible, "visible"),
153 (WaitForState::Hidden, "hidden"),
154 ] {
155 let options = WaitForOptions::builder().state(*state).build();
156 let json = options.to_json();
157 assert_eq!(json["state"], *expected);
158 }
159 }
160
161 #[test]
162 fn test_wait_for_options_timeout() {
163 let options = WaitForOptions::builder().timeout(5000.0).build();
164 let json = options.to_json();
165 assert_eq!(json["timeout"], 5000.0);
166 }
167}