thirtyfour 0.37.8

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
412
413
414
415
416
417
418
419
420
421
422
use std::ffi::OsString;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;
use tracing::{debug, warn};

use super::browser::BrowserKind;
use super::error::ManagerError;
use super::status::{
    DriverId, DriverLogLine, DriverLogSubscription, DriverStream, Emitter, LogSubscribers, Status,
};

/// How driver-process stdout/stderr is handled.
#[derive(Debug, Clone, Copy, Default)]
pub enum StdioMode {
    /// Pipe both streams and emit each line via `tracing::debug!` under the
    /// `thirtyfour::manager::driver` target. This is the default.
    #[default]
    Tracing,
    /// Inherit the parent process's stdio.
    Inherit,
    /// Discard both streams.
    Null,
}

impl StdioMode {
    fn to_stdio(self) -> Stdio {
        match self {
            StdioMode::Tracing => Stdio::piped(),
            StdioMode::Inherit => Stdio::inherit(),
            StdioMode::Null => Stdio::null(),
        }
    }
}

pub(crate) struct SpawnConfig {
    pub host: IpAddr,
    pub ready_timeout: Duration,
    pub stdio: StdioMode,
    pub args: Vec<OsString>,
    /// Windows only: skip `CREATE_NO_WINDOW` so the driver gets its own
    /// console window. Only read on Windows.
    #[cfg_attr(not(windows), allow(dead_code))]
    pub show_console_window: bool,
}

impl Default for SpawnConfig {
    fn default() -> Self {
        Self {
            host: IpAddr::V4(Ipv4Addr::LOCALHOST),
            ready_timeout: Duration::from_secs(30),
            stdio: StdioMode::default(),
            args: Vec::new(),
            show_console_window: false,
        }
    }
}

/// A spawned driver process. Killed on drop.
pub(crate) struct ManagedDriverProcess {
    pub host: IpAddr,
    pub port: u16,
    pub browser: BrowserKind,
    /// Resolved driver version; carried so `DriverShutdown` can name it.
    pub version: String,
    /// Synthetic identifier unique within the parent manager.
    pub driver_id: DriverId,
    /// Per-process log subscribers. Cloneable so [`crate::WebDriver`] can
    /// register subscribers via the `DriverGuard` slot it already holds.
    pub log_subscribers: LogSubscribers,
    /// `None` after `Drop` has run.
    child: Option<Child>,
    /// Set when shutdown is initiated, so the stdio pump tasks can exit cleanly.
    shutdown: Arc<AtomicBool>,
    /// Handles for the stdout / stderr pump tasks (when stdio is `Tracing`).
    /// `Drop` aborts these so the runtime can shut down promptly — Windows
    /// pipe semantics mean a stuck `next_line().await` would otherwise block
    /// runtime shutdown indefinitely after `start_kill`.
    pump_handles: Vec<JoinHandle<()>>,
    /// Status emitter so `Drop` can fire `DriverShutdown`.
    emitter: Emitter,
}

impl std::fmt::Debug for ManagedDriverProcess {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ManagedDriverProcess")
            .field("host", &self.host)
            .field("port", &self.port)
            .field("browser", &self.browser)
            .finish()
    }
}

/// Per-spawn context the manager threads in alongside the binary path.
pub(crate) struct SpawnContext<'a> {
    pub driver_id: DriverId,
    pub version: &'a str,
    pub emitter: &'a Emitter,
    /// Manager-wide log subscribers. The newly-spawned process retains a clone
    /// so its pump tasks can fan log lines out alongside per-process subscribers.
    pub manager_log_subscribers: LogSubscribers,
}

impl ManagedDriverProcess {
    /// Spawn the driver and poll until it reports ready.
    ///
    /// Retries are only attempted on the narrow case of a port-collision after
    /// the bind/release dance. Spawn failures (binary not found, exit on
    /// startup) and readiness timeouts surface immediately — retrying those
    /// would just multiply the wait by 3x for no benefit.
    pub(crate) async fn spawn(
        binary: &Path,
        browser: BrowserKind,
        cfg: &SpawnConfig,
        ctx: SpawnContext<'_>,
    ) -> Result<Self, ManagerError> {
        const MAX_PORT_ATTEMPTS: u8 = 3;
        let mut last_err: Option<ManagerError> = None;
        for attempt in 0..MAX_PORT_ATTEMPTS {
            let port = pick_port(cfg.host)?;
            match spawn_at_port(binary, browser, cfg, port, &ctx).await {
                Ok(p) => return Ok(p),
                Err(e) if is_port_in_use(&e) => {
                    debug!("driver port {port} already in use (attempt {attempt}): {e}");
                    last_err = Some(e);
                }
                Err(e) => return Err(e),
            }
        }
        Err(last_err.unwrap_or_else(|| ManagerError::Spawn("port allocation exhausted".into())))
    }

    /// Connection URL.
    pub(crate) fn url(&self) -> String {
        format!("http://{}:{}", self.host, self.port)
    }

    /// Subscribe to log lines from just this driver process. Returns an RAII
    /// guard whose drop unsubscribes; `mem::forget` keeps the subscriber alive
    /// for the rest of the process's lifetime.
    pub(crate) fn subscribe_log<F>(&self, f: F) -> DriverLogSubscription
    where
        F: Fn(&DriverLogLine) + Send + Sync + 'static,
    {
        self.log_subscribers.add(f)
    }

    /// Kill the driver and wait until the OS has reaped it.
    ///
    /// This consumes the process after the final managed-session lease is
    /// released. [`Drop`] remains the non-blocking fallback when explicit
    /// asynchronous cleanup is skipped or cancelled.
    pub(crate) async fn shutdown(mut self) -> std::io::Result<()> {
        self.shutdown.store(true, Ordering::Relaxed);
        for handle in self.pump_handles.drain(..) {
            handle.abort();
        }
        if let Some(mut child) = self.child.take() {
            child.kill().await?;
        }
        Ok(())
    }
}

/// Heuristic: did this error come from the OS reporting that the chosen port
/// was already bound? Both "Address already in use" and Windows's "Only one
/// usage of each socket address" land here.
fn is_port_in_use(err: &ManagerError) -> bool {
    let msg = match err {
        ManagerError::Spawn(s) => s.as_str(),
        _ => return false,
    };
    let lower = msg.to_ascii_lowercase();
    lower.contains("address already in use")
        || lower.contains("only one usage of each socket address")
        || lower.contains("addrinuse")
}

async fn spawn_at_port(
    binary: &Path,
    browser: BrowserKind,
    cfg: &SpawnConfig,
    port: u16,
    ctx: &SpawnContext<'_>,
) -> Result<ManagedDriverProcess, ManagerError> {
    // chromedriver, geckodriver, msedgedriver, and safaridriver all accept --port=N.
    let mut cmd = Command::new(binary);
    cmd.args(&cfg.args);
    cmd.arg(format!("--port={port}"));
    cmd.stdout(cfg.stdio.to_stdio());
    cmd.stderr(cfg.stdio.to_stdio());
    cmd.stdin(Stdio::null());
    cmd.kill_on_drop(true);

    // On Windows a console subprocess pops up its own console window whenever
    // the parent has none (GUI apps built with `windows_subsystem =
    // "windows"`). Suppress it with CREATE_NO_WINDOW unless the user opted in
    // via `show_console_window`. Explicitly-set stdio handles still work under
    // CREATE_NO_WINDOW, so every `StdioMode` behaves the same either way.
    #[cfg(windows)]
    if !cfg.show_console_window {
        const CREATE_NO_WINDOW: u32 = 0x0800_0000;
        cmd.creation_flags(CREATE_NO_WINDOW);
    }

    // chromedriver / msedgedriver bind only to loopback by default; pass
    // --allowed-ips when the user has configured a non-loopback host.
    if matches!(browser, BrowserKind::Chrome | BrowserKind::Edge)
        && cfg.host != IpAddr::V4(Ipv4Addr::LOCALHOST)
    {
        cmd.arg(format!("--allowed-ips={}", cfg.host));
    }

    // geckodriver's WebDriver-BiDi WebSocket binds Firefox's RemoteAgent
    // server to a fixed default port (9222). When the manager runs many
    // Firefox sessions back-to-back the new Firefox can't bind 9222 if the
    // previous Firefox hasn't fully released it yet — and the resulting
    // `webSocketUrl` capability still reports 9222, so connecting fails
    // with HTTP 404. Pass `--websocket-port=0` to ask geckodriver to pick a
    // free port; it propagates through to RemoteAgent and the returned
    // `webSocketUrl` reflects the actual bound port.
    if matches!(browser, BrowserKind::Firefox) {
        cmd.arg("--websocket-port=0");
    }

    let mut child = cmd
        .spawn()
        .map_err(|e| ManagerError::Spawn(format!("spawn {}: {}", binary.display(), e)))?;
    let pid = child.id().unwrap_or(0);
    ctx.emitter.emit(Status::DriverProcessSpawned {
        browser,
        version: ctx.version.to_string(),
        pid,
        port,
        binary: PathBuf::from(binary),
    });

    let shutdown = Arc::new(AtomicBool::new(false));
    let log_subscribers = LogSubscribers::new();
    let mut pump_handles = Vec::new();
    if matches!(cfg.stdio, StdioMode::Tracing) {
        let line_ctx = LogLineContext {
            driver_id: ctx.driver_id,
            browser,
            version: ctx.version.to_string(),
            port,
        };
        if let Some(stdout) = child.stdout.take() {
            pump_handles.push(spawn_pump(
                DriverStream::Stdout,
                stdout,
                Arc::clone(&shutdown),
                line_ctx.clone(),
                log_subscribers.clone(),
                ctx.manager_log_subscribers.clone(),
            ));
        }
        if let Some(stderr) = child.stderr.take() {
            pump_handles.push(spawn_pump(
                DriverStream::Stderr,
                stderr,
                Arc::clone(&shutdown),
                line_ctx,
                log_subscribers.clone(),
                ctx.manager_log_subscribers.clone(),
            ));
        }
    }

    let ready_started = Instant::now();
    if let Err(e) = wait_until_ready(cfg.host, port, cfg.ready_timeout).await {
        let _ = child.kill().await;
        for h in &pump_handles {
            h.abort();
        }
        return Err(e);
    }
    let url = format!("http://{}:{}", cfg.host, port);
    ctx.emitter.emit(Status::DriverReady {
        browser,
        version: ctx.version.to_string(),
        url,
        elapsed: ready_started.elapsed(),
    });

    Ok(ManagedDriverProcess {
        host: cfg.host,
        port,
        browser,
        version: ctx.version.to_string(),
        driver_id: ctx.driver_id,
        log_subscribers,
        child: Some(child),
        shutdown,
        pump_handles,
        emitter: ctx.emitter.clone(),
    })
}

fn pick_port(host: IpAddr) -> Result<u16, ManagerError> {
    let listener = TcpListener::bind(SocketAddr::new(host, 0))
        .map_err(|e| ManagerError::Spawn(format!("bind ephemeral port: {e}")))?;
    let port =
        listener.local_addr().map_err(|e| ManagerError::Spawn(format!("local_addr: {e}")))?.port();
    drop(listener);
    Ok(port)
}

/// Context shared between every line dispatched by one pump task.
#[derive(Clone)]
struct LogLineContext {
    driver_id: DriverId,
    browser: BrowserKind,
    version: String,
    port: u16,
}

fn spawn_pump<R>(
    stream: DriverStream,
    reader: R,
    shutdown: Arc<AtomicBool>,
    ctx: LogLineContext,
    process_subs: LogSubscribers,
    manager_subs: LogSubscribers,
) -> JoinHandle<()>
where
    R: tokio::io::AsyncRead + Unpin + Send + 'static,
{
    let stream_label = match stream {
        DriverStream::Stdout => "stdout",
        DriverStream::Stderr => "stderr",
    };
    tokio::spawn(async move {
        let mut lines = BufReader::new(reader).lines();
        loop {
            if shutdown.load(Ordering::Relaxed) {
                break;
            }
            match lines.next_line().await {
                Ok(Some(line)) => {
                    debug!(target: "thirtyfour::manager::driver", stream = stream_label, line = %line);
                    let log = DriverLogLine {
                        driver_id: ctx.driver_id,
                        browser: ctx.browser,
                        version: ctx.version.clone(),
                        port: ctx.port,
                        stream,
                        line,
                    };
                    process_subs.dispatch(&log);
                    manager_subs.dispatch(&log);
                }
                Ok(None) => break,
                Err(e) => {
                    warn!(target: "thirtyfour::manager::driver", stream = stream_label, error = %e);
                    break;
                }
            }
        }
    })
}

async fn wait_until_ready(host: IpAddr, port: u16, timeout: Duration) -> Result<(), ManagerError> {
    let url = format!("http://{host}:{port}/status");
    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(2))
        .build()
        .map_err(|e| ManagerError::Spawn(e.to_string()))?;

    let deadline = Instant::now() + timeout;
    while Instant::now() < deadline {
        if let Ok(resp) = client.get(&url).send().await
            && resp.status().is_success()
            && let Ok(body) = resp.json::<serde_json::Value>().await
            && body
                .get("value")
                .and_then(|v| v.get("ready"))
                .and_then(|v| v.as_bool())
                .unwrap_or(false)
        {
            return Ok(());
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
    Err(ManagerError::DriverNotReady(timeout))
}

impl Drop for ManagedDriverProcess {
    fn drop(&mut self) {
        self.shutdown.store(true, Ordering::Relaxed);
        // Sync drop, so we can't await. `start_kill` issues SIGKILL (or
        // TerminateProcess on Windows) without blocking; `kill_on_drop(true)`
        // asks Tokio to reap the child on a best-effort basis when the Child
        // handle drops. Explicit `WebDriver::quit().await` uses `shutdown()`
        // above and waits for reaping instead.
        if let Some(mut child) = self.child.take()
            && let Err(e) = child.start_kill()
        {
            warn!(target: "thirtyfour::manager", error = %e, "failed to kill driver");
        }
        // Abort the stdio pump tasks. Without this, on Windows the pumps can
        // remain stuck on `next_line().await` after the child is killed
        // (anonymous pipe semantics don't always surface EOF cleanly), and the
        // multi-thread Tokio runtime drops by waiting for all spawned tasks to
        // finish — blocking process exit indefinitely.
        for h in self.pump_handles.drain(..) {
            h.abort();
        }
        self.emitter.emit(Status::DriverShutdown {
            browser: self.browser,
            version: self.version.clone(),
            port: self.port,
        });
    }
}