thirtyfour 0.37.0

Thirtyfour is a Selenium / WebDriver library for Rust, for automated website UI testing. Tested on Chrome and Firefox, but any webdriver-capable browser should work.
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
//! `browser.*` — browser-wide control: shutdown, user contexts, OS-level
//! windows, and download behavior.
//!
//! "Browser" here means the running browser instance, not an individual
//! tab. Use this module to:
//!
//! - End the entire browser ([`Close`][close]).
//! - Manage [user contexts][user-context-spec] — BiDi's incognito-like
//!   isolation primitive ([`CreateUserContext`][cuc],
//!   [`GetUserContexts`][guc], [`RemoveUserContext`][ruc]).
//! - Inspect and manipulate OS-level browser windows
//!   ([`GetClientWindows`][gcw], [`SetClientWindowState`][scws]).
//! - Control how downloads are handled
//!   ([`SetDownloadBehavior`][sdb]).
//!
//! See the [W3C `browser` module specification][spec] for the canonical
//! definitions.
//!
//! [spec]: https://w3c.github.io/webdriver-bidi/#module-browser
//! [user-context-spec]: https://w3c.github.io/webdriver-bidi/#user-contexts
//! [close]: crate::bidi::modules::browser::Close
//! [cuc]: crate::bidi::modules::browser::CreateUserContext
//! [guc]: crate::bidi::modules::browser::GetUserContexts
//! [ruc]: crate::bidi::modules::browser::RemoveUserContext
//! [gcw]: crate::bidi::modules::browser::GetClientWindows
//! [scws]: crate::bidi::modules::browser::SetClientWindowState
//! [sdb]: crate::bidi::modules::browser::SetDownloadBehavior

use serde::{Deserialize, Serialize};

use crate::bidi::BiDi;
use crate::bidi::command::{BidiCommand, Empty};
use crate::bidi::error::BidiError;
use crate::bidi::ids::{ClientWindowId, UserContextId};
use crate::common::protocol::string_enum;

/// [`browser.close`][spec] — terminate every WebDriver session and shut
/// the browser process down.
///
/// After this command returns, the browser process exits and all
/// associated tabs/windows close without prompting to unload. The
/// behaviour when multiple WebDriver sessions are connected to the same
/// browser is implementation-defined — see the spec.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-close
#[derive(Debug, Clone, Default, Serialize)]
pub struct Close;

impl BidiCommand for Close {
    const METHOD: &'static str = "browser.close";
    type Returns = Empty;
}

/// [`browser.createUserContext`][spec] — open a new
/// [user context][user-context-spec] (an isolated cookie / storage jar,
/// roughly equivalent to a Chrome profile or a private-browsing window).
///
/// Per-user-context overrides for `acceptInsecureCerts`, proxy
/// configuration, and unhandled-prompt behavior may be supplied; if
/// omitted the session-level defaults apply.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-createUserContext
/// [user-context-spec]: https://w3c.github.io/webdriver-bidi/#user-contexts
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateUserContext {
    /// Override the session's accept-insecure-certs flag for this user
    /// context. Returns `unsupported operation` on drivers that can't
    /// scope TLS handling per user context.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accept_insecure_certs: Option<bool>,
    /// Per-user-context [`session.ProxyConfiguration`][proxy] (passed
    /// through as JSON). Returns `unsupported operation` when not
    /// supported by the driver.
    ///
    /// [proxy]: https://w3c.github.io/webdriver-bidi/#type-session-ProxyConfiguration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxy: Option<serde_json::Value>,
    /// Per-user-context [`session.UserPromptHandler`][handler] override.
    ///
    /// [handler]: https://w3c.github.io/webdriver-bidi/#type-session-UserPromptHandler
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unhandled_prompt_behavior: Option<serde_json::Value>,
}

impl BidiCommand for CreateUserContext {
    const METHOD: &'static str = "browser.createUserContext";
    type Returns = UserContextInfo;
}

/// One user context. See [`type-browser-UserContextInfo`][spec].
///
/// Returned by [`CreateUserContext`] and as elements of
/// [`GetUserContextsResult::user_contexts`].
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#type-browser-UserContextInfo
#[derive(Debug, Clone, Deserialize)]
pub struct UserContextInfo {
    /// User context id. The default user context is always called
    /// `"default"` and cannot be removed.
    #[serde(rename = "userContext")]
    pub user_context: UserContextId,
}

/// [`browser.getUserContexts`][spec] — list every known user context.
///
/// The default user context is always present in the result.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-getUserContexts
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetUserContexts;

impl BidiCommand for GetUserContexts {
    const METHOD: &'static str = "browser.getUserContexts";
    type Returns = GetUserContextsResult;
}

/// Response for [`GetUserContexts`].
#[derive(Debug, Clone, Deserialize)]
pub struct GetUserContextsResult {
    /// All user contexts, including `"default"`.
    #[serde(rename = "userContexts")]
    pub user_contexts: Vec<UserContextInfo>,
}

/// [`browser.removeUserContext`][spec] — close a user context, including
/// every navigable inside it (without firing `beforeunload`).
///
/// The default user context cannot be removed; passing it returns
/// `invalid argument`.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-removeUserContext
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoveUserContext {
    /// User context to remove.
    pub user_context: UserContextId,
}

impl BidiCommand for RemoveUserContext {
    const METHOD: &'static str = "browser.removeUserContext";
    type Returns = Empty;
}

string_enum! {
    /// Window state. Used by [`ClientWindowInfo::state`] and as a
    /// parameter to [`SetClientWindowState::state`].
    ///
    /// See [`type-browser-ClientWindowInfo`][spec] in the spec.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#type-browser-ClientWindowInfo
    pub enum ClientWindowState {
        /// Fills the entire screen with no chrome.
        Fullscreen = "fullscreen",
        /// Maximised within the OS desktop.
        Maximized = "maximized",
        /// Minimised to the dock/taskbar.
        Minimized = "minimized",
        /// Normal (windowed) state. Allows custom width/height/x/y.
        Normal = "normal",
    }
}

/// Properties of an OS-level browser window.
///
/// Mirrors the spec's [`browser.ClientWindowInfo`][spec] type.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#type-browser-ClientWindowInfo
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClientWindowInfo {
    /// Whether this window currently receives keyboard input from the OS.
    /// Note that this is OS focus, not BiDi document focus — the active
    /// document of an inactive window can still be queried.
    pub active: bool,
    /// Stable window id. Use it with [`SetClientWindowState`].
    pub client_window: ClientWindowId,
    /// Window height in CSS pixels.
    pub height: u32,
    /// Window width in CSS pixels.
    pub width: u32,
    /// Window x-coordinate in screen pixels.
    pub x: i32,
    /// Window y-coordinate in screen pixels.
    pub y: i32,
    /// Current window state.
    pub state: ClientWindowState,
}

/// [`browser.getClientWindows`][spec] — enumerate every OS-level browser
/// window.
///
/// One window can host many tabs (top-level browsing contexts); use
/// [`browsing_context::GetTree`](crate::bidi::modules::browsing_context::GetTree)
/// to list those.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-getClientWindows
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetClientWindows;

impl BidiCommand for GetClientWindows {
    const METHOD: &'static str = "browser.getClientWindows";
    type Returns = GetClientWindowsResult;
}

/// Response for [`GetClientWindows`].
#[derive(Debug, Clone, Deserialize)]
pub struct GetClientWindowsResult {
    /// All client windows.
    #[serde(rename = "clientWindows")]
    pub client_windows: Vec<ClientWindowInfo>,
}

/// [`browser.setClientWindowState`][spec] — change a window's state and
/// (when `state == Normal`) optionally its position/size.
///
/// `width`/`height`/`x`/`y` are only respected when `state` is
/// [`ClientWindowState::Normal`]; for `Maximized`, `Minimized`, or
/// `Fullscreen` they are ignored. The driver returns the (possibly
/// adjusted) post-transition window info as a [`ClientWindowInfo`].
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-setClientWindowState
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetClientWindowState {
    /// Window to update.
    pub client_window: ClientWindowId,
    /// New state.
    pub state: ClientWindowState,
    /// New width in CSS pixels (only respected when `state == Normal`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,
    /// New height in CSS pixels (only respected when `state == Normal`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,
    /// New x-coordinate (only respected when `state == Normal`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub x: Option<i32>,
    /// New y-coordinate (only respected when `state == Normal`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub y: Option<i32>,
}

impl BidiCommand for SetClientWindowState {
    const METHOD: &'static str = "browser.setClientWindowState";
    type Returns = ClientWindowInfo;
}

/// Download behaviour for [`SetDownloadBehavior::download_behavior`].
///
/// Matches the spec's `browser.DownloadBehavior` union of `"allowed"` and
/// `"denied"`.
///
/// See [`SetDownloadBehavior`] for usage.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum DownloadBehavior {
    /// Allow downloads, saving each file to `destination_folder`.
    Allowed {
        /// Destination folder on the host filesystem.
        #[serde(rename = "destinationFolder")]
        destination_folder: String,
    },
    /// Refuse all downloads. The browser cancels each one.
    Denied,
}

/// [`browser.setDownloadBehavior`][spec] — configure how the browser
/// handles downloads, either globally or per user context.
///
/// Pass `download_behavior: None` to clear any previously-set override.
/// `user_contexts: None` (or omitted) sets the default behaviour;
/// `user_contexts: Some(ids)` scopes the override to those contexts.
///
/// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-setDownloadBehavior
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetDownloadBehavior {
    /// New behaviour, or `None` to clear any prior override.
    pub download_behavior: Option<DownloadBehavior>,
    /// Restrict to these user contexts. `None` / empty → set the global
    /// default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_contexts: Option<Vec<UserContextId>>,
}

impl BidiCommand for SetDownloadBehavior {
    const METHOD: &'static str = "browser.setDownloadBehavior";
    type Returns = Empty;
}

/// Convenience facade for the `browser.*` module.
///
/// Returned by [`BiDi::browser`](crate::bidi::BiDi::browser). Methods on
/// this facade cover the common, unscoped form of each command. For
/// scoping (e.g. setting download behaviour for one user context only)
/// build the command struct directly and send it via
/// [`BiDi::send`](crate::bidi::BiDi::send).
#[derive(Debug)]
pub struct BrowserModule<'a> {
    bidi: &'a BiDi,
}

impl<'a> BrowserModule<'a> {
    pub(crate) fn new(bidi: &'a BiDi) -> Self {
        Self {
            bidi,
        }
    }

    /// Run [`browser.close`][spec] — terminate every WebDriver session
    /// and shut the browser process down.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-close
    pub async fn close(&self) -> Result<(), BidiError> {
        self.bidi.send(Close).await?;
        Ok(())
    }

    /// Create a new user context with default settings via
    /// [`browser.createUserContext`][spec].
    ///
    /// For per-context proxy / TLS / prompt-handler overrides, build the
    /// [`CreateUserContext`] struct directly.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-createUserContext
    pub async fn create_user_context(&self) -> Result<UserContextInfo, BidiError> {
        self.bidi.send(CreateUserContext::default()).await
    }

    /// List every user context via [`browser.getUserContexts`][spec].
    ///
    /// The default user context is always included in the result.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-getUserContexts
    pub async fn get_user_contexts(&self) -> Result<GetUserContextsResult, BidiError> {
        self.bidi.send(GetUserContexts).await
    }

    /// Remove a user context via [`browser.removeUserContext`][spec].
    ///
    /// All top-level traversables inside the context are closed without
    /// firing `beforeunload`. Returns `invalid argument` if `user_context`
    /// is the default user context, and `no such user context` if it
    /// doesn't exist.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-removeUserContext
    pub async fn remove_user_context(&self, user_context: UserContextId) -> Result<(), BidiError> {
        self.bidi
            .send(RemoveUserContext {
                user_context,
            })
            .await?;
        Ok(())
    }

    /// List every OS-level browser window via
    /// [`browser.getClientWindows`][spec].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-getClientWindows
    pub async fn get_client_windows(&self) -> Result<GetClientWindowsResult, BidiError> {
        self.bidi.send(GetClientWindows).await
    }

    /// Change a window's state via
    /// [`browser.setClientWindowState`][spec], without changing position
    /// or size.
    ///
    /// To resize or reposition a window in [`ClientWindowState::Normal`],
    /// build the [`SetClientWindowState`] struct directly and supply
    /// `width` / `height` / `x` / `y`.
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-setClientWindowState
    pub async fn set_client_window_state(
        &self,
        client_window: ClientWindowId,
        state: ClientWindowState,
    ) -> Result<ClientWindowInfo, BidiError> {
        self.bidi
            .send(SetClientWindowState {
                client_window,
                state,
                width: None,
                height: None,
                x: None,
                y: None,
            })
            .await
    }

    /// Configure the global download behaviour via
    /// [`browser.setDownloadBehavior`][spec].
    ///
    /// Pass `None` to clear any prior override. To scope the override to
    /// specific user contexts, build the [`SetDownloadBehavior`] struct
    /// directly and supply [`user_contexts`][SetDownloadBehavior::user_contexts].
    ///
    /// [spec]: https://w3c.github.io/webdriver-bidi/#command-browser-setDownloadBehavior
    pub async fn set_download_behavior(
        &self,
        download_behavior: Option<DownloadBehavior>,
    ) -> Result<(), BidiError> {
        self.bidi
            .send(SetDownloadBehavior {
                download_behavior,
                user_contexts: None,
            })
            .await?;
        Ok(())
    }
}