interop/build/web/
webdriver.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17use crate::TEST_SERVER_URI;
18use crate::util::RunningProcess;
19use color_eyre::eyre::Result;
20
21pub(crate) async fn setup_webdriver(force: bool) -> Result<()> {
22    let mut spinner = RunningProcess::new("Setting up WebDriver & co...", false);
23
24    let wd_dir = dirs::home_dir().unwrap().join(".webdrivers");
25    let chrome = webdriver_installation::WebdriverKind::Chrome;
26
27    if force {
28        spinner.update("FORCE_WEBDRIVER_INSTALL is set. Forcefully removing webdrivers...");
29        std::fs::remove_dir(&wd_dir)?;
30    }
31
32    if !wd_dir.join(chrome.as_exe_name()).exists() {
33        spinner.update("Chrome WebDriver isn't installed. Installing...");
34        chrome.install_webdriver(&wd_dir, force).await?;
35    }
36
37    spinner.update("Chrome WebDriver installed");
38
39    spinner.success("WebDriver setup [OK]");
40
41    Ok(())
42}
43
44pub(crate) async fn start_webdriver_chrome(addr: &std::net::SocketAddr) -> Result<tokio::process::Child> {
45    let wd_dir = dirs::home_dir().unwrap().join(".webdrivers");
46
47    Ok(tokio::process::Command::new(wd_dir.join("chromedriver"))
48        .arg(format!("--port={}", addr.port()))
49        .stdout(std::process::Stdio::null())
50        .stderr(std::process::Stdio::null())
51        .spawn()?)
52}
53
54pub(crate) async fn setup_browser(addr: &std::net::SocketAddr, folder: &str) -> Result<fantoccini::Client> {
55    let spinner = RunningProcess::new("Starting Fantoccini remote browser...", false);
56    let mut caps_json = serde_json::json!({
57        "goog:chromeOptions": {
58            "args": [
59                "headless",
60                "disable-dev-shm-usage",
61                "no-sandbox"
62            ]
63        }
64    });
65
66    if let Ok(chrome_path) = std::env::var("CHROME_PATH") {
67        caps_json["goog:chromeOptions"]["binary"] = chrome_path.into();
68    }
69
70    let serde_json::Value::Object(caps) = caps_json else {
71        unreachable!("`serde_json::json!()` did not produce an object when provided an object. Something is broken.")
72    };
73
74    let browser = fantoccini::ClientBuilder::native()
75        .capabilities(caps)
76        .connect(&format!("http://{addr}"))
77        .await?;
78    browser.goto(&format!("{TEST_SERVER_URI}/{folder}/index.html")).await?;
79
80    spinner.success("Browser [OK]");
81    Ok(browser)
82}