1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
34// 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.
89// 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.
1314// 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/.
1617use crate::TEST_SERVER_URI;
18use crate::util::RunningProcess;
19use color_eyre::eyre::Result;
2021pub(crate) async fn setup_webdriver(force: bool) -> Result<()> {
22let mut spinner = RunningProcess::new("Setting up WebDriver & co...", false);
2324let wd_dir = dirs::home_dir().unwrap().join(".webdrivers");
25let chrome = webdriver_installation::WebdriverKind::Chrome;
2627if force {
28 spinner.update("FORCE_WEBDRIVER_INSTALL is set. Forcefully removing webdrivers...");
29 std::fs::remove_dir(&wd_dir)?;
30 }
3132if !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 }
3637 spinner.update("Chrome WebDriver installed");
3839 spinner.success("WebDriver setup [OK]");
4041Ok(())
42}
4344pub(crate) async fn start_webdriver_chrome(addr: &std::net::SocketAddr) -> Result<tokio::process::Child> {
45let wd_dir = dirs::home_dir().unwrap().join(".webdrivers");
4647Ok(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}
5354pub(crate) async fn setup_browser(addr: &std::net::SocketAddr, folder: &str) -> Result<fantoccini::Client> {
55let spinner = RunningProcess::new("Starting Fantoccini remote browser...", false);
56let mut caps_json = serde_json::json!({
57"goog:chromeOptions": {
58"args": [
59"headless",
60"disable-dev-shm-usage",
61"no-sandbox"
62]
63 }
64 });
6566if let Ok(chrome_path) = std::env::var("CHROME_PATH") {
67 caps_json["goog:chromeOptions"]["binary"] = chrome_path.into();
68 }
6970let serde_json::Value::Object(caps) = caps_json else {
71unreachable!("`serde_json::json!()` did not produce an object when provided an object. Something is broken.")
72 };
7374let 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?;
7980 spinner.success("Browser [OK]");
81Ok(browser)
82}