fantoccini 0.22.1

High-level API for programmatically interacting with web pages through WebDriver.
Documentation
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::ops::RangeInclusive;

use webdriver::command::PrintParameters;

use crate::error::PrintConfigurationError;

/// The builder of [`PrintConfiguration`].
#[derive(Debug)]
pub struct PrintConfigurationBuilder {
    orientation: PrintOrientation,
    scale: f64,
    background: bool,
    size: PrintSize,
    margins: PrintMargins,
    page_ranges: Vec<PrintPageRange>,
    shrink_to_fit: bool,
}

impl Default for PrintConfigurationBuilder {
    fn default() -> Self {
        Self {
            orientation: PrintOrientation::default(),
            scale: 1.0,
            background: false,
            size: PrintSize::default(),
            margins: PrintMargins::default(),
            page_ranges: Vec::default(),
            shrink_to_fit: true,
        }
    }
}

impl PrintConfigurationBuilder {
    /// Builds the [`PrintConfiguration`].
    pub fn build(self) -> Result<PrintConfiguration, PrintConfigurationError> {
        let must_be_finite_and_positive = [
            self.scale,
            self.margins.top,
            self.margins.left,
            self.margins.right,
            self.margins.bottom,
            self.size.width,
            self.size.height,
        ];
        if !must_be_finite_and_positive
            .into_iter()
            .all(|n| n.is_finite())
        {
            return Err(PrintConfigurationError::NonFiniteDimensions);
        }
        if !must_be_finite_and_positive
            .into_iter()
            .all(|n| n.is_sign_positive())
        {
            return Err(PrintConfigurationError::NegativeDimensions);
        }

        if self.size.height < PrintSize::MIN.height || self.size.width < PrintSize::MIN.width {
            return Err(PrintConfigurationError::PrintSizeTooSmall);
        }

        if (self.margins.top + self.margins.bottom) >= self.size.height
            || (self.margins.left + self.margins.right) >= self.size.width
        {
            return Err(PrintConfigurationError::DimensionsOverflow);
        }

        Ok(PrintConfiguration {
            orientation: self.orientation,
            scale: self.scale,
            background: self.background,
            size: self.size,
            margins: self.margins,
            page_ranges: self.page_ranges,
            shrink_to_fit: self.shrink_to_fit,
        })
    }

    /// Sets the orientation of the printed page.
    ///
    /// Default: [`PrintOrientation::Portrait`].
    pub fn orientation(mut self, orientation: PrintOrientation) -> Self {
        self.orientation = orientation;

        self
    }

    /// Sets the scale of the printed page.
    ///
    /// Default: 1.
    pub fn scale(mut self, scale: f64) -> Self {
        self.scale = scale;

        self
    }

    /// Sets whether or not to print the backgrounds of the page.
    ///
    /// Default: false.
    pub fn background(mut self, background: bool) -> Self {
        self.background = background;

        self
    }

    /// Sets the size of the printed page.
    ///
    /// Default: [`PrintSize::A4`].
    pub fn size(mut self, size: PrintSize) -> Self {
        self.size = size;

        self
    }

    /// Sets the margins of the printed page.
    ///
    /// Default: `1x1x1x1cm`.
    pub fn margins(mut self, margins: PrintMargins) -> Self {
        self.margins = margins;

        self
    }

    /// Sets ranges of pages to print.
    ///
    /// An empty `ranges` prints all pages, which is the default.
    pub fn page_ranges(mut self, ranges: Vec<PrintPageRange>) -> Self {
        self.page_ranges = ranges;

        self
    }

    /// Sets whether or not to resize the content to fit the page width,
    /// overriding any page width specified in the content of pages to print.
    ///
    /// Default: true.
    pub fn shrink_to_fit(mut self, shrink_to_fit: bool) -> Self {
        self.shrink_to_fit = shrink_to_fit;

        self
    }
}

/// The print configuration.
#[derive(Debug, Clone, PartialEq)]
pub struct PrintConfiguration {
    orientation: PrintOrientation,
    scale: f64,
    background: bool,
    size: PrintSize,
    margins: PrintMargins,
    page_ranges: Vec<PrintPageRange>,
    shrink_to_fit: bool,
}

impl PrintConfiguration {
    /// Creates a [`PrintConfigurationBuilder`] to configure a [`PrintConfiguration`].
    pub fn builder() -> PrintConfigurationBuilder {
        PrintConfigurationBuilder::default()
    }

    pub(crate) fn into_params(self) -> PrintParameters {
        PrintParameters {
            orientation: self.orientation.into_params(),
            scale: self.scale,
            background: self.background,
            page: self.size.into_params(),
            margin: self.margins.into_params(),
            page_ranges: self
                .page_ranges
                .into_iter()
                .map(|page_range| page_range.into_params())
                .collect(),
            shrink_to_fit: self.shrink_to_fit,
        }
    }
}

impl Default for PrintConfiguration {
    fn default() -> Self {
        PrintConfigurationBuilder::default()
            .build()
            .expect("default configuration is buildable")
    }
}

/// The orientation of the print.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PrintOrientation {
    /// Landscape orientation.
    Landscape,
    #[default]
    /// Portrait orientation.
    Portrait,
}

impl PrintOrientation {
    pub(crate) fn into_params(self) -> webdriver::command::PrintOrientation {
        match self {
            Self::Landscape => webdriver::command::PrintOrientation::Landscape,
            Self::Portrait => webdriver::command::PrintOrientation::Portrait,
        }
    }
}

/// The size of the printed page in centimeters.
///
/// Default: [`PrintSize::A4`].
#[derive(Debug, Clone, PartialEq)]
pub struct PrintSize {
    /// The width in centimeters.
    pub width: f64,
    /// The height in centimeters.
    pub height: f64,
}

impl PrintSize {
    /// The standard A4 paper size, which has the dimension of `21.0x29.7cm`.
    pub const A4: Self = Self {
        width: 21.,
        height: 29.7,
    };

    /// The standard US letter paper size, which has the dimension of `21.59x27.94cm`.
    pub const US_LETTER: Self = Self {
        width: 21.59,
        height: 27.94,
    };

    /// The standard US legal paper size, which has the dimension of `21.59x35.56cm`.
    pub const US_LEGAL: Self = Self {
        width: 21.59,
        height: 35.56,
    };

    /// The minimum page size allowed by the Webdriver2 standard, which is `2.54/72cm`.
    pub const MIN: Self = Self {
        // FIXME: use 2.54/72.0 with MSRV >= 1.82
        width: 0.036,
        height: 0.036,
    };

    pub(crate) fn into_params(self) -> webdriver::command::PrintPage {
        webdriver::command::PrintPage {
            width: self.width,
            height: self.height,
        }
    }
}

impl Default for PrintSize {
    fn default() -> Self {
        Self::A4
    }
}

/// The range of the pages to print.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrintPageRange {
    range: RangeInclusive<u64>,
}

impl PrintPageRange {
    /// A single page to print.
    pub const fn single(page: u64) -> Self {
        Self { range: page..=page }
    }

    /// A range of pages to print.
    ///
    /// Returns None if the range start is greater than the range end.
    pub const fn range(range: RangeInclusive<u64>) -> Option<Self> {
        if *range.start() <= *range.end() {
            Some(Self { range })
        } else {
            None
        }
    }

    pub(crate) fn into_params(self) -> webdriver::command::PrintPageRange {
        let (start, end) = self.range.into_inner();

        if start == end {
            webdriver::command::PrintPageRange::Integer(start)
        } else {
            webdriver::command::PrintPageRange::Range(format!("{start}-{end}"))
        }
    }
}

/// The margins of the printed page in centimeters.
///
/// Default: `1x1x1x1cm`.
#[derive(Debug, Clone, PartialEq)]
pub struct PrintMargins {
    /// The top margin in centimeters.
    pub top: f64,
    /// The bottom margin in centimeters.
    pub bottom: f64,
    /// The left margin in centimeters.
    pub left: f64,
    /// The right margin in centimeters.
    pub right: f64,
}

impl PrintMargins {
    pub(crate) fn into_params(self) -> webdriver::command::PrintMargins {
        webdriver::command::PrintMargins {
            top: self.top,
            bottom: self.bottom,
            left: self.left,
            right: self.right,
        }
    }
}

impl Default for PrintMargins {
    fn default() -> Self {
        Self {
            top: 1.0,
            bottom: 1.0,
            left: 1.0,
            right: 1.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::f64::{INFINITY, NAN};

    use crate::{
        error::PrintConfigurationError,
        wd::{PrintConfiguration, PrintMargins, PrintSize},
    };

    #[test]
    fn negative_print_configuration_dimensions() {
        let margins = PrintConfiguration::builder()
            .margins(PrintMargins {
                top: -1.0,
                bottom: 0.0,
                left: 1.0,
                right: 5.4,
            })
            .build();

        let size = PrintConfiguration::builder()
            .size(PrintSize {
                width: -1.0,
                height: 1.0,
            })
            .build();

        assert_eq!(margins, Err(PrintConfigurationError::NegativeDimensions));
        assert_eq!(size, Err(PrintConfigurationError::NegativeDimensions));
    }

    #[test]
    fn non_finite_print_configuration_dimensions() {
        let nan_margins = PrintConfiguration::builder()
            .margins(PrintMargins {
                top: NAN,
                bottom: 0.0,
                left: 1.0,
                right: 5.4,
            })
            .build();

        let nan_size = PrintConfiguration::builder()
            .size(PrintSize {
                width: NAN,
                height: 1.0,
            })
            .build();

        let infinite_margins = PrintConfiguration::builder()
            .margins(PrintMargins {
                top: INFINITY,
                bottom: 0.0,
                left: 1.0,
                right: 5.4,
            })
            .build();

        let infinite_size = PrintConfiguration::builder()
            .size(PrintSize {
                width: INFINITY,
                height: 1.0,
            })
            .build();

        assert_eq!(
            nan_margins,
            Err(PrintConfigurationError::NonFiniteDimensions)
        );
        assert_eq!(nan_size, Err(PrintConfigurationError::NonFiniteDimensions));
        assert_eq!(
            infinite_margins,
            Err(PrintConfigurationError::NonFiniteDimensions)
        );
        assert_eq!(
            infinite_size,
            Err(PrintConfigurationError::NonFiniteDimensions)
        );
    }

    #[test]
    fn overflow_print_configuration_dimensions() {
        let overflow = PrintConfiguration::builder()
            .size(PrintSize {
                width: 10.0,
                height: 5.0,
            })
            .margins(PrintMargins {
                top: 1.0,
                bottom: 1.0,
                left: 5.0,
                right: 5.0,
            })
            .build();

        assert_eq!(overflow, Err(PrintConfigurationError::DimensionsOverflow));
    }

    #[test]
    fn too_small_print_configuration_dimensions() {
        let size_to_small = PrintConfiguration::builder()
            .size(PrintSize {
                width: 0.01,
                height: 5.0,
            })
            .build();

        assert_eq!(
            size_to_small,
            Err(PrintConfigurationError::PrintSizeTooSmall)
        );
    }
}