playwright/imp/
utils.rs

1use crate::imp::prelude::*;
2
3#[derive(Debug, Deserialize, Clone, Serialize, PartialEq, Eq)]
4pub struct Viewport {
5    pub width: i32,
6    pub height: i32
7}
8
9#[skip_serializing_none]
10#[derive(Debug, Deserialize, Clone, Serialize)]
11pub struct ProxySettings {
12    /// Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example `http://myproxy.com:3128` or\n`socks5://myproxy.com:3128`. Short form `myproxy.com:3128` is considered an HTTP proxy.
13    pub server: String,
14    /// Optional coma-separated domains to bypass proxy, for example `\".com, chromium.org, .domain.com\"`.
15    pub bypass: Option<String>,
16    pub username: Option<String>,
17    pub password: Option<String>
18}
19
20#[skip_serializing_none]
21#[derive(Debug, Deserialize, Clone, Serialize)]
22pub struct Geolocation {
23    /// Latitude between -90 and 90.
24    pub latitude: f64,
25    /// Longitude between -180 and 180.
26    pub longitude: f64,
27    /// Non-negative accuracy value. Defaults to `0`.
28    pub accuracy: Option<f64>
29}
30
31#[derive(Debug, Deserialize, Clone, Serialize)]
32pub struct HttpCredentials {
33    pub username: String,
34    pub password: String
35}
36
37#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
38#[serde(rename_all = "snake_case")]
39pub enum ColorScheme {
40    Dark,
41    Light,
42    NoPreference
43}
44
45#[skip_serializing_none]
46#[derive(Debug, Deserialize, Serialize)]
47pub struct StorageState {
48    pub cookies: Option<Vec<Cookie>>,
49    pub origins: Option<Vec<OriginState>>
50}
51
52#[skip_serializing_none]
53#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
54#[serde(rename_all = "camelCase")]
55pub struct Cookie {
56    pub name: String,
57    pub value: String,
58    pub url: Option<String>,
59    pub domain: Option<String>,
60    pub path: Option<String>,
61    /// Optional Unix time in seconds.
62    pub expires: Option<f64>,
63    pub http_only: Option<bool>,
64    pub secure: Option<bool>,
65    pub same_site: Option<SameSite>
66}
67
68impl Cookie {
69    pub fn with_url<S: Into<String>>(name: S, value: S, url: S) -> Self {
70        Self {
71            name: name.into(),
72            value: value.into(),
73            url: Some(url.into()),
74            domain: None,
75            path: None,
76            expires: None,
77            http_only: None,
78            secure: None,
79            same_site: None
80        }
81    }
82
83    pub fn with_domain_path<S: Into<String>>(name: S, value: S, domain: S, path: S) -> Self {
84        Self {
85            name: name.into(),
86            value: value.into(),
87            url: None,
88            domain: Some(domain.into()),
89            path: Some(path.into()),
90            expires: None,
91            http_only: None,
92            secure: None,
93            same_site: None
94        }
95    }
96}
97
98#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
99pub enum SameSite {
100    Lax,
101    None,
102    Strict
103}
104
105#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
106#[serde(rename_all = "camelCase")]
107pub struct OriginState {
108    pub origin: String,
109    pub local_storage: Vec<LocalStorageEntry>
110}
111
112#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
113#[serde(rename_all = "camelCase")]
114pub struct LocalStorageEntry {
115    pub name: String,
116    pub value: String
117}
118
119#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy, Hash)]
120#[serde(rename_all = "lowercase")]
121pub enum DocumentLoadState {
122    Commit,  // Added in Playwright 1.50+
123    DomContentLoaded,
124    Load,
125    NetworkIdle
126}
127
128#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
129pub enum KeyboardModifier {
130    Alt,
131    Control,
132    Meta,
133    Shift
134}
135
136#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
137#[serde(rename_all = "lowercase")]
138pub enum MouseButton {
139    Left,
140    Middle,
141    Right
142}
143
144#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy)]
145pub struct Position {
146    pub x: f64,
147    pub y: f64
148}
149
150impl From<(f64, f64)> for Position {
151    fn from((x, y): (f64, f64)) -> Self { Self { x, y } }
152}
153
154#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy)]
155pub struct FloatRect {
156    /// the x coordinate of the element in pixels.
157    pub x: f64,
158    pub y: f64,
159    /// the width of the element in pixels.
160    pub width: f64,
161    pub height: f64
162}
163
164#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
165#[serde(rename_all = "lowercase")]
166pub enum ScreenshotType {
167    Jpeg,
168    Png
169}
170
171#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
172#[serde(rename_all = "lowercase")]
173pub enum ElementState {
174    Disabled,
175    Editable,
176    Enabled,
177    Hidden,
178    Stable,
179    Visible
180}
181
182#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
183#[serde(rename_all = "lowercase")]
184pub enum WaitForSelectorState {
185    Attached,
186    Detached,
187    Visible,
188    Hidden
189}
190
191#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
192pub struct Header {
193    pub name: String,
194    pub value: String
195}
196
197impl From<Header> for (String, String) {
198    fn from(Header { name, value }: Header) -> Self { (name, value) }
199}
200
201impl From<(String, String)> for Header {
202    fn from((k, v): (String, String)) -> Self { Self { name: k, value: v } }
203}
204
205#[derive(Debug, Serialize, PartialEq, Clone)]
206#[serde(untagged)]
207pub enum Length<'a> {
208    Value(f64),
209    WithUnit(&'a str)
210}
211
212impl<'a> From<f64> for Length<'a> {
213    fn from(x: f64) -> Self { Self::Value(x) }
214}
215
216impl<'a> From<&'a str> for Length<'a> {
217    fn from(x: &'a str) -> Self { Self::WithUnit(x) }
218}
219
220#[skip_serializing_none]
221#[derive(Debug, Serialize, PartialEq, Clone)]
222pub struct PdfMargins<'a, 'b, 'c, 'd> {
223    pub top: Option<Length<'a>>,
224    pub right: Option<Length<'b>>,
225    pub bottom: Option<Length<'c>>,
226    pub left: Option<Length<'d>>
227}
228
229#[derive(Debug, Serialize, PartialEq)]
230pub struct File {
231    pub name: String,
232    pub mime: String,
233    pub buffer: String
234}
235
236impl File {
237    pub fn new(name: String, mime: String, body: &[u8]) -> Self {
238        let buffer = base64::encode(body);
239        Self { name, mime, buffer }
240    }
241}
242/// Browser distribution channel.
243// TODO: kebab case
244#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone, Copy)]
245#[serde(rename_all = "kebab-case")]
246pub enum BrowserChannel {
247    Chrome,
248    ChromeBeta,
249    ChromeDev,
250    ChromeCanary,
251    Msedge,
252    MsedgeBeta,
253    MsedgeDev,
254    MsedgeCanary,
255    FirefoxStable
256}
257
258#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
259#[serde(rename_all = "camelCase")]
260pub struct SourceLocation {
261    pub url: String,
262    /// 0-based line number in the resource.
263    pub line_number: i32,
264    /// 0-based column number in the resource.
265    pub column_number: i32
266}
267
268#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
269#[serde(rename_all = "camelCase")]
270pub struct ResponseTiming {
271    /// Request start time in milliseconds elapsed since January 1, 1970 00:00:00 UTC
272    pub start_time: f64,
273    /// Time immediately before the browser starts the domain name lookup for the resource. The value is given in milliseconds\nrelative to `startTime`, -1 if not available.
274    pub domain_lookup_start: f64,
275    /// Time immediately after the browser starts the domain name lookup for the resource. The value is given in milliseconds\nrelative to `startTime`, -1 if not available.
276    pub domain_lookup_end: f64,
277    /// Time immediately before the user agent starts establishing the connection to the server to retrieve the resource. The\nvalue is given in milliseconds relative to `startTime`, -1 if not available.
278    pub connect_start: f64,
279    /// Time immediately before the browser starts the handshake process to secure the current connection. The value is given in\nmilliseconds relative to `startTime`, -1 if not available.
280    pub secure_connection_start: f64,
281    /// Time immediately before the user agent starts establishing the connection to the server to retrieve the resource. The\nvalue is given in milliseconds relative to `startTime`, -1 if not available.
282    pub connect_end: f64,
283    /// Time immediately before the browser starts requesting the resource from the server, cache, or local resource. The value\nis given in milliseconds relative to `startTime`, -1 if not available.
284    pub request_start: f64,
285    /// Time immediately after the browser starts requesting the resource from the server, cache, or local resource. The value\nis given in milliseconds relative to `startTime`, -1 if not available.
286    pub response_start: f64
287}