playwright_rs/protocol/
screenshot.rs1use serde::Serialize;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
17#[serde(rename_all = "lowercase")]
18pub enum ScreenshotType {
19 Png,
21 Jpeg,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
42pub struct ScreenshotClip {
43 pub x: f64,
45 pub y: f64,
47 pub width: f64,
49 pub height: f64,
51}
52
53#[derive(Debug, Clone, Default)]
89pub struct ScreenshotOptions {
90 pub screenshot_type: Option<ScreenshotType>,
92 pub quality: Option<u8>,
94 pub full_page: Option<bool>,
96 pub clip: Option<ScreenshotClip>,
98 pub omit_background: Option<bool>,
100 pub timeout: Option<f64>,
102}
103
104impl ScreenshotOptions {
105 pub fn builder() -> ScreenshotOptionsBuilder {
107 ScreenshotOptionsBuilder::default()
108 }
109
110 pub(crate) fn to_json(&self) -> serde_json::Value {
112 let mut json = serde_json::json!({});
113
114 if let Some(screenshot_type) = &self.screenshot_type {
115 json["type"] = serde_json::to_value(screenshot_type)
116 .expect("serialization of ScreenshotType cannot fail");
117 }
118
119 if let Some(quality) = self.quality {
120 json["quality"] = serde_json::json!(quality);
121 }
122
123 if let Some(full_page) = self.full_page {
124 json["fullPage"] = serde_json::json!(full_page);
125 }
126
127 if let Some(clip) = &self.clip {
128 json["clip"] = serde_json::to_value(clip).expect("serialization of clip cannot fail");
129 }
130
131 if let Some(omit_background) = self.omit_background {
132 json["omitBackground"] = serde_json::json!(omit_background);
133 }
134
135 if let Some(timeout) = self.timeout {
137 json["timeout"] = serde_json::json!(timeout);
138 } else {
139 json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
140 }
141
142 json
143 }
144}
145
146#[derive(Debug, Clone, Default)]
150pub struct ScreenshotOptionsBuilder {
151 screenshot_type: Option<ScreenshotType>,
152 quality: Option<u8>,
153 full_page: Option<bool>,
154 clip: Option<ScreenshotClip>,
155 omit_background: Option<bool>,
156 timeout: Option<f64>,
157}
158
159impl ScreenshotOptionsBuilder {
160 pub fn screenshot_type(mut self, screenshot_type: ScreenshotType) -> Self {
162 self.screenshot_type = Some(screenshot_type);
163 self
164 }
165
166 pub fn quality(mut self, quality: u8) -> Self {
170 self.quality = Some(quality);
171 self
172 }
173
174 pub fn full_page(mut self, full_page: bool) -> Self {
176 self.full_page = Some(full_page);
177 self
178 }
179
180 pub fn clip(mut self, clip: ScreenshotClip) -> Self {
182 self.clip = Some(clip);
183 self
184 }
185
186 pub fn omit_background(mut self, omit_background: bool) -> Self {
188 self.omit_background = Some(omit_background);
189 self
190 }
191
192 pub fn timeout(mut self, timeout: f64) -> Self {
194 self.timeout = Some(timeout);
195 self
196 }
197
198 pub fn build(self) -> ScreenshotOptions {
200 ScreenshotOptions {
201 screenshot_type: self.screenshot_type,
202 quality: self.quality,
203 full_page: self.full_page,
204 clip: self.clip,
205 omit_background: self.omit_background,
206 timeout: self.timeout,
207 }
208 }
209}
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn test_screenshot_type_serialization() {
217 assert_eq!(
218 serde_json::to_string(&ScreenshotType::Png).unwrap(),
219 "\"png\""
220 );
221 assert_eq!(
222 serde_json::to_string(&ScreenshotType::Jpeg).unwrap(),
223 "\"jpeg\""
224 );
225 }
226
227 #[test]
228 fn test_builder_jpeg_with_quality() {
229 let options = ScreenshotOptions::builder()
230 .screenshot_type(ScreenshotType::Jpeg)
231 .quality(80)
232 .build();
233
234 let json = options.to_json();
235 assert_eq!(json["type"], "jpeg");
236 assert_eq!(json["quality"], 80);
237 }
238
239 #[test]
240 fn test_builder_full_page() {
241 let options = ScreenshotOptions::builder().full_page(true).build();
242
243 let json = options.to_json();
244 assert_eq!(json["fullPage"], true);
245 }
246
247 #[test]
248 fn test_builder_clip() {
249 let clip = ScreenshotClip {
250 x: 10.0,
251 y: 20.0,
252 width: 300.0,
253 height: 200.0,
254 };
255 let options = ScreenshotOptions::builder().clip(clip).build();
256
257 let json = options.to_json();
258 assert_eq!(json["clip"]["x"], 10.0);
259 assert_eq!(json["clip"]["y"], 20.0);
260 assert_eq!(json["clip"]["width"], 300.0);
261 assert_eq!(json["clip"]["height"], 200.0);
262 }
263
264 #[test]
265 fn test_builder_omit_background() {
266 let options = ScreenshotOptions::builder().omit_background(true).build();
267
268 let json = options.to_json();
269 assert_eq!(json["omitBackground"], true);
270 }
271
272 #[test]
273 fn test_builder_multiple_options() {
274 let options = ScreenshotOptions::builder()
275 .screenshot_type(ScreenshotType::Jpeg)
276 .quality(90)
277 .full_page(true)
278 .timeout(5000.0)
279 .build();
280
281 let json = options.to_json();
282 assert_eq!(json["type"], "jpeg");
283 assert_eq!(json["quality"], 90);
284 assert_eq!(json["fullPage"], true);
285 assert_eq!(json["timeout"], 5000.0);
286 }
287}