Skip to main content

playwright_rs/protocol/
device.rs

1// Copyright 2026 Paul Adamson
2// Licensed under the Apache License, Version 2.0
3//
4// Device descriptor types for browser emulation.
5//
6// See: <https://playwright.dev/docs/emulation>
7
8use serde::Deserialize;
9
10/// Viewport dimensions for a device descriptor.
11#[derive(Clone, Debug, Deserialize)]
12pub struct DeviceViewport {
13    /// The viewport width in CSS pixels.
14    pub width: i32,
15    /// The viewport height in CSS pixels.
16    pub height: i32,
17}
18
19/// Describes a device for browser emulation.
20///
21/// Use with `BrowserContext::new_context()` options to emulate a specific device,
22/// matching the behavior of `playwright.devices["iPhone 13"]` in Python/JS.
23///
24/// Device descriptors are accessed by name from [`crate::protocol::Playwright::devices`].
25/// The name is the map key, not a field of the descriptor itself.
26///
27/// # Example
28///
29/// ```ignore
30/// use playwright_rs::protocol::Playwright;
31///
32/// #[tokio::main]
33/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
34///     let playwright = Playwright::launch().await?;
35///
36///     let iphone = &playwright.devices()["iPhone 13"];
37///     assert!(iphone.is_mobile);
38///     assert!(iphone.has_touch);
39///     assert_eq!(iphone.default_browser_type, "webkit");
40///
41///     playwright.shutdown().await?;
42///     Ok(())
43/// }
44/// ```
45///
46/// See: <https://playwright.dev/docs/api/class-playwright#playwright-devices>
47#[derive(Clone, Debug, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct DeviceDescriptor {
50    /// The user-agent string for the device.
51    pub user_agent: String,
52    /// The viewport dimensions.
53    pub viewport: DeviceViewport,
54    /// The device pixel ratio (e.g., `3.0` for Retina displays).
55    pub device_scale_factor: f64,
56    /// Whether the device is a mobile device.
57    pub is_mobile: bool,
58    /// Whether the device supports touch input.
59    pub has_touch: bool,
60    /// The default browser type for the device: `"chromium"`, `"firefox"`, or `"webkit"`.
61    pub default_browser_type: String,
62}