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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! State assertions for locators.
//!
//! This module contains assertions for checking element state
//! such as visibility, enabled/disabled status, and checked state.
use std::time::Duration;
use viewpoint_core::Locator;
use crate::error::AssertionError;
/// State assertion methods for locators.
///
/// These methods check the state of elements (visible, enabled, checked, etc.)
pub struct StateAssertions<'a> {
locator: &'a Locator<'a>,
timeout: Duration,
is_negated: bool,
}
impl<'a> StateAssertions<'a> {
/// Create a new `StateAssertions`.
pub fn new(locator: &'a Locator<'a>, timeout: Duration, is_negated: bool) -> Self {
Self {
locator,
timeout,
is_negated,
}
}
/// Assert that the element is visible.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_be_visible(&self) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let is_visible = self.locator.is_visible().await.map_err(|e| {
AssertionError::new("Failed to check visibility", "visible", e.to_string())
})?;
let expected = !self.is_negated;
if is_visible == expected {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not be visible"
} else {
"Element should be visible"
},
if expected { "visible" } else { "hidden" },
if is_visible { "visible" } else { "hidden" },
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that the element is hidden.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_be_hidden(&self) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let is_visible = self.locator.is_visible().await.map_err(|e| {
AssertionError::new("Failed to check visibility", "hidden", e.to_string())
})?;
let expected_hidden = !self.is_negated;
let is_hidden = !is_visible;
if is_hidden == expected_hidden {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not be hidden"
} else {
"Element should be hidden"
},
if expected_hidden { "hidden" } else { "visible" },
if is_hidden { "hidden" } else { "visible" },
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that the element is enabled.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_be_enabled(&self) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let is_enabled = super::locator_helpers::is_enabled(self.locator).await?;
let expected_enabled = !self.is_negated;
if is_enabled == expected_enabled {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not be enabled"
} else {
"Element should be enabled"
},
if expected_enabled {
"enabled"
} else {
"disabled"
},
if is_enabled { "enabled" } else { "disabled" },
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that the element is disabled.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_be_disabled(&self) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let is_enabled = super::locator_helpers::is_enabled(self.locator).await?;
let expected_disabled = !self.is_negated;
let is_disabled = !is_enabled;
if is_disabled == expected_disabled {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not be disabled"
} else {
"Element should be disabled"
},
if expected_disabled {
"disabled"
} else {
"enabled"
},
if is_disabled { "disabled" } else { "enabled" },
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that the element is checked (for checkboxes/radios).
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_be_checked(&self) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let is_checked = self.locator.is_checked().await.map_err(|e| {
AssertionError::new("Failed to check checked state", "checked", e.to_string())
})?;
let expected_checked = !self.is_negated;
if is_checked == expected_checked {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not be checked"
} else {
"Element should be checked"
},
if expected_checked {
"checked"
} else {
"unchecked"
},
if is_checked { "checked" } else { "unchecked" },
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}