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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Screenshot types and options
//
// Provides configuration for page and element screenshots, matching Playwright's API.
use serde::Serialize;
/// Screenshot image format
///
/// # Example
///
/// ```ignore
/// use playwright_rs::protocol::ScreenshotType;
///
/// let screenshot_type = ScreenshotType::Jpeg;
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ScreenshotType {
/// PNG format (lossless, supports transparency)
Png,
/// JPEG format (lossy compression, smaller file size)
Jpeg,
}
/// Clip region for screenshot
///
/// Specifies a rectangular region to capture.
///
/// # Example
///
/// ```ignore
/// use playwright_rs::protocol::ScreenshotClip;
///
/// let clip = ScreenshotClip {
/// x: 10.0,
/// y: 20.0,
/// width: 300.0,
/// height: 200.0,
/// };
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct ScreenshotClip {
/// X coordinate of clip region origin
pub x: f64,
/// Y coordinate of clip region origin
pub y: f64,
/// Width of clip region
pub width: f64,
/// Height of clip region
pub height: f64,
}
/// Screenshot options
///
/// Configuration options for page and element screenshots.
///
/// Use the builder pattern to construct options:
///
/// # Example
///
/// ```ignore
/// use playwright_rs::protocol::{ScreenshotOptions, ScreenshotType, ScreenshotClip};
///
/// // JPEG with quality
/// let options = ScreenshotOptions::builder()
/// .screenshot_type(ScreenshotType::Jpeg)
/// .quality(80)
/// .build();
///
/// // Full page screenshot
/// let options = ScreenshotOptions::builder()
/// .full_page(true)
/// .build();
///
/// // Clip region
/// let clip = ScreenshotClip {
/// x: 10.0,
/// y: 10.0,
/// width: 200.0,
/// height: 100.0,
/// };
/// let options = ScreenshotOptions::builder()
/// .clip(clip)
/// .build();
/// ```
///
/// See: <https://playwright.dev/docs/api/class-page#page-screenshot>
#[derive(Debug, Clone, Default)]
pub struct ScreenshotOptions {
/// Image format (png or jpeg)
pub screenshot_type: Option<ScreenshotType>,
/// JPEG quality (0-100), only applies to jpeg format
pub quality: Option<u8>,
/// Capture full scrollable page
pub full_page: Option<bool>,
/// Clip region to capture
pub clip: Option<ScreenshotClip>,
/// Hide default white background (PNG only)
pub omit_background: Option<bool>,
/// Screenshot timeout in milliseconds
pub timeout: Option<f64>,
}
impl ScreenshotOptions {
/// Create a new builder for ScreenshotOptions
pub fn builder() -> ScreenshotOptionsBuilder {
ScreenshotOptionsBuilder::default()
}
/// Convert options to JSON value for protocol
pub(crate) fn to_json(&self) -> serde_json::Value {
let mut json = serde_json::json!({});
if let Some(screenshot_type) = &self.screenshot_type {
json["type"] = serde_json::to_value(screenshot_type)
.expect("serialization of ScreenshotType cannot fail");
}
if let Some(quality) = self.quality {
json["quality"] = serde_json::json!(quality);
}
if let Some(full_page) = self.full_page {
json["fullPage"] = serde_json::json!(full_page);
}
if let Some(clip) = &self.clip {
json["clip"] = serde_json::to_value(clip).expect("serialization of clip cannot fail");
}
if let Some(omit_background) = self.omit_background {
json["omitBackground"] = serde_json::json!(omit_background);
}
// Timeout is required in Playwright 1.56.1+
if let Some(timeout) = self.timeout {
json["timeout"] = serde_json::json!(timeout);
} else {
json["timeout"] = serde_json::json!(crate::DEFAULT_TIMEOUT_MS);
}
json
}
}
/// Builder for ScreenshotOptions
///
/// Provides a fluent API for constructing screenshot options.
#[derive(Debug, Clone, Default)]
pub struct ScreenshotOptionsBuilder {
screenshot_type: Option<ScreenshotType>,
quality: Option<u8>,
full_page: Option<bool>,
clip: Option<ScreenshotClip>,
omit_background: Option<bool>,
timeout: Option<f64>,
}
impl ScreenshotOptionsBuilder {
/// Set the screenshot format (png or jpeg)
pub fn screenshot_type(mut self, screenshot_type: ScreenshotType) -> Self {
self.screenshot_type = Some(screenshot_type);
self
}
/// Set JPEG quality (0-100)
///
/// Only applies when screenshot_type is Jpeg.
pub fn quality(mut self, quality: u8) -> Self {
self.quality = Some(quality);
self
}
/// Capture full scrollable page beyond viewport
pub fn full_page(mut self, full_page: bool) -> Self {
self.full_page = Some(full_page);
self
}
/// Set clip region to capture
pub fn clip(mut self, clip: ScreenshotClip) -> Self {
self.clip = Some(clip);
self
}
/// Hide default white background (creates transparent PNG)
pub fn omit_background(mut self, omit_background: bool) -> Self {
self.omit_background = Some(omit_background);
self
}
/// Set screenshot timeout in milliseconds
pub fn timeout(mut self, timeout: f64) -> Self {
self.timeout = Some(timeout);
self
}
/// Build the ScreenshotOptions
pub fn build(self) -> ScreenshotOptions {
ScreenshotOptions {
screenshot_type: self.screenshot_type,
quality: self.quality,
full_page: self.full_page,
clip: self.clip,
omit_background: self.omit_background,
timeout: self.timeout,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_screenshot_type_serialization() {
assert_eq!(
serde_json::to_string(&ScreenshotType::Png).unwrap(),
"\"png\""
);
assert_eq!(
serde_json::to_string(&ScreenshotType::Jpeg).unwrap(),
"\"jpeg\""
);
}
#[test]
fn test_builder_jpeg_with_quality() {
let options = ScreenshotOptions::builder()
.screenshot_type(ScreenshotType::Jpeg)
.quality(80)
.build();
let json = options.to_json();
assert_eq!(json["type"], "jpeg");
assert_eq!(json["quality"], 80);
}
#[test]
fn test_builder_full_page() {
let options = ScreenshotOptions::builder().full_page(true).build();
let json = options.to_json();
assert_eq!(json["fullPage"], true);
}
#[test]
fn test_builder_clip() {
let clip = ScreenshotClip {
x: 10.0,
y: 20.0,
width: 300.0,
height: 200.0,
};
let options = ScreenshotOptions::builder().clip(clip).build();
let json = options.to_json();
assert_eq!(json["clip"]["x"], 10.0);
assert_eq!(json["clip"]["y"], 20.0);
assert_eq!(json["clip"]["width"], 300.0);
assert_eq!(json["clip"]["height"], 200.0);
}
#[test]
fn test_builder_omit_background() {
let options = ScreenshotOptions::builder().omit_background(true).build();
let json = options.to_json();
assert_eq!(json["omitBackground"], true);
}
#[test]
fn test_builder_multiple_options() {
let options = ScreenshotOptions::builder()
.screenshot_type(ScreenshotType::Jpeg)
.quality(90)
.full_page(true)
.timeout(5000.0)
.build();
let json = options.to_json();
assert_eq!(json["type"], "jpeg");
assert_eq!(json["quality"], 90);
assert_eq!(json["fullPage"], true);
assert_eq!(json["timeout"], 5000.0);
}
}