#![cfg(feature = "manager-tests")]
use std::future::Future;
use std::time::Duration;
use thirtyfour::manager::WebDriverManager;
use thirtyfour::prelude::*;
use thirtyfour::{ChromeCapabilities, EdgeCapabilities, FirefoxCapabilities};
const TEST_TIMEOUT: Duration = Duration::from_secs(180);
async fn with_timeout<F, T>(f: F) -> WebDriverResult<T>
where
F: Future<Output = WebDriverResult<T>>,
{
tokio::time::timeout(TEST_TIMEOUT, f).await.unwrap_or_else(|_| {
Err(WebDriverError::FatalError(format!("test exceeded {}s budget", TEST_TIMEOUT.as_secs())))
})
}
fn skip_navigation() -> bool {
cfg!(target_os = "windows")
}
fn chrome_caps() -> ChromeCapabilities {
let mut caps = DesiredCapabilities::chrome();
caps.set_headless().unwrap();
caps.set_no_sandbox().unwrap();
caps.set_disable_gpu().unwrap();
caps.set_disable_dev_shm_usage().unwrap();
caps.add_arg("--no-sandbox").unwrap();
caps
}
fn firefox_caps() -> FirefoxCapabilities {
let mut caps = DesiredCapabilities::firefox();
caps.set_headless().unwrap();
caps
}
fn edge_caps() -> EdgeCapabilities {
let mut caps = DesiredCapabilities::edge();
caps.add_arg("--headless=new").unwrap();
caps.add_arg("--no-sandbox").unwrap();
caps.add_arg("--disable-gpu").unwrap();
caps.add_arg("--disable-dev-shm-usage").unwrap();
caps
}
#[tokio::test(flavor = "multi_thread")]
async fn managed_chrome_smoke() -> WebDriverResult<()> {
with_timeout(async {
let driver = WebDriver::managed(chrome_caps()).await?;
if !skip_navigation() {
driver.goto("about:blank").await?;
}
driver.quit().await?;
Ok(())
})
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn managed_firefox_smoke() -> WebDriverResult<()> {
with_timeout(async {
let driver = WebDriver::managed(firefox_caps()).await?;
driver.goto("about:blank").await?;
driver.quit().await?;
Ok(())
})
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn managed_edge_smoke() -> WebDriverResult<()> {
with_timeout(async {
let driver = WebDriver::managed(edge_caps()).await?;
driver.quit().await?;
Ok(())
})
.await
}
#[cfg(target_os = "macos")]
#[tokio::test(flavor = "multi_thread")]
async fn managed_safari_smoke() -> WebDriverResult<()> {
with_timeout(async {
let mut caps = DesiredCapabilities::safari();
let _ = &mut caps; let driver = WebDriver::managed(caps).await?;
driver.goto("about:blank").await?;
driver.quit().await?;
Ok(())
})
.await
}
#[tokio::test(flavor = "multi_thread")]
async fn managed_chrome_options_and_dedup() -> WebDriverResult<()> {
with_timeout(async {
let cache = tempfile::tempdir().expect("tempdir");
let mgr = WebDriverManager::builder()
.match_local() .cache_dir(cache.path().to_path_buf())
.ready_timeout(Duration::from_secs(60))
.build();
let d1 = mgr.launch(chrome_caps()).await?;
let d2 = mgr.launch(chrome_caps()).await?;
assert_eq!(
d1.handle.server_url(),
d2.handle.server_url(),
"two managed sessions for the same browser must share a chromedriver process"
);
if !skip_navigation() {
d1.goto("about:blank").await?;
d2.goto("about:blank").await?;
}
d1.quit().await?;
d2.quit().await?;
Ok(())
})
.await
}