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
//! Text assertions for locators.
//!
//! This module contains assertions for checking text content
//! of elements matched by a locator.
use std::time::Duration;
use viewpoint_core::Locator;
use crate::error::AssertionError;
/// Text assertion methods for locators.
///
/// These methods are implemented separately and called via `LocatorAssertions`.
pub struct TextAssertions<'a> {
locator: &'a Locator<'a>,
timeout: Duration,
is_negated: bool,
}
impl<'a> TextAssertions<'a> {
/// Create a new `TextAssertions`.
pub fn new(locator: &'a Locator<'a>, timeout: Duration, is_negated: bool) -> Self {
Self {
locator,
timeout,
is_negated,
}
}
/// Assert that the element has the exact text content.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_have_text(&self, expected: &str) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let text = self.locator.text_content().await.map_err(|e| {
AssertionError::new("Failed to get text content", expected, e.to_string())
})?;
let actual = text.as_deref().unwrap_or("");
let matches = actual.trim() == expected;
let expected_match = !self.is_negated;
if matches == expected_match {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not have text"
} else {
"Element should have text"
},
if self.is_negated {
format!("not \"{expected}\"")
} else {
format!("\"{expected}\"")
},
format!("\"{actual}\""),
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that the element contains the specified text.
///
/// # Errors
///
/// Returns an error if the assertion fails or the element cannot be queried.
pub async fn to_contain_text(&self, expected: &str) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let text = self.locator.text_content().await.map_err(|e| {
AssertionError::new("Failed to get text content", expected, e.to_string())
})?;
let actual = text.as_deref().unwrap_or("");
let contains = actual.contains(expected);
let expected_match = !self.is_negated;
if contains == expected_match {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Element should not contain text"
} else {
"Element should contain text"
},
if self.is_negated {
format!("not containing \"{expected}\"")
} else {
format!("containing \"{expected}\"")
},
format!("\"{actual}\""),
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that all elements have the specified texts (in order).
///
/// # Errors
///
/// Returns an error if the assertion fails or the elements cannot be queried.
pub async fn to_have_texts(&self, expected: &[&str]) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let actual = self.locator.all_text_contents().await.map_err(|e| {
AssertionError::new(
"Failed to get text contents",
format!("{expected:?}"),
e.to_string(),
)
})?;
let actual_trimmed: Vec<&str> = actual.iter().map(|s| s.trim()).collect();
let matches = actual_trimmed.len() == expected.len()
&& actual_trimmed
.iter()
.zip(expected.iter())
.all(|(a, e)| a == e);
let expected_match = !self.is_negated;
if matches == expected_match {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Elements should not have texts"
} else {
"Elements should have texts"
},
format!("{expected:?}"),
format!("{actual_trimmed:?}"),
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// Assert that all elements contain the specified texts (in order).
///
/// # Errors
///
/// Returns an error if the assertion fails or the elements cannot be queried.
pub async fn to_contain_texts(&self, expected: &[&str]) -> Result<(), AssertionError> {
let start = std::time::Instant::now();
loop {
let actual = self.locator.all_text_contents().await.map_err(|e| {
AssertionError::new(
"Failed to get text contents",
format!("{expected:?}"),
e.to_string(),
)
})?;
let matches = actual.len() == expected.len()
&& actual
.iter()
.zip(expected.iter())
.all(|(a, e)| a.contains(e));
let expected_match = !self.is_negated;
if matches == expected_match {
return Ok(());
}
if start.elapsed() >= self.timeout {
return Err(AssertionError::new(
if self.is_negated {
"Elements should not contain texts"
} else {
"Elements should contain texts"
},
format!("{expected:?}"),
format!("{actual:?}"),
));
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}