Skip to main content

browser_commander/browser/webdriver/
snapshot.rs

1//! Native WebDriver owns a copied live Chromium profile, never its source.
2use super::{launch_owned, ManagedWebDriver, WebDriverBrowser, WebDriverOptions};
3use crate::browser::snapshot::{copy_owned_snapshot, SnapshotOptions, SnapshotReport};
4use std::ops::Deref;
5
6pub struct WebDriverSnapshotResult {
7    pub browser: ManagedWebDriver,
8    pub snapshot: SnapshotReport,
9}
10impl Deref for WebDriverSnapshotResult {
11    type Target = ManagedWebDriver;
12    fn deref(&self) -> &Self::Target {
13        &self.browser
14    }
15}
16
17/// Copy a live Chromium profile and launch it through native Fantoccini.
18/// Firefox uses a different profile format and cannot consume Chromium copies.
19pub async fn launch_webdriver_snapshot(
20    source: SnapshotOptions,
21    mut options: WebDriverOptions,
22) -> anyhow::Result<WebDriverSnapshotResult> {
23    if options.browser != WebDriverBrowser::Chrome {
24        anyhow::bail!("Chromium snapshots require WebDriverBrowser::Chrome");
25    }
26    if options.user_data_dir.is_some()
27        || options
28            .args
29            .iter()
30            .any(|arg| arg.trim_start_matches('-').starts_with("profile-directory"))
31    {
32        anyhow::bail!(
33            "snapshot owns user_data_dir and profile-directory; use SnapshotOptions.profile"
34        );
35    }
36    let profile = source.profile.clone();
37    let mut copy = copy_owned_snapshot(source).await?;
38    options.user_data_dir = Some(copy.report.target.clone());
39    options.args.push(format!("--profile-directory={profile}"));
40    let browser = launch_owned(options, true).await?;
41    copy.armed = false;
42    Ok(WebDriverSnapshotResult {
43        browser,
44        snapshot: copy.report.clone(),
45    })
46}