Skip to main content

vertigo_testing/
env.rs

1//! What a suite runs against. [`Core`] is the part every environment has: the settings, the
2//! address of the app, the work directory, an HTTP client and a chromedriver shared by the
3//! tests. A test crate wraps it in its own environment, which adds whatever its app needs (a
4//! backend, a database) and implements [`Env`].
5
6use std::{
7    net::TcpListener,
8    ops::Deref,
9    path::{Path, PathBuf},
10    sync::Arc,
11    time::Duration,
12};
13
14use anyhow::{Context, Result};
15use thirtyfour::{
16    CapabilitiesHelper, ChromiumLikeCapabilities, DesiredCapabilities, LoggingPrefsLogLevel,
17    WebDriver,
18    extensions::query::ElementPollerWithTimeout,
19    manager::{BrowserKind, WebDriverManager},
20};
21
22/// Time limit of element queries and of the `Ctx::wait_*` helpers.
23pub const WAIT: Duration = Duration::from_secs(10);
24pub const POLL: Duration = Duration::from_millis(100);
25
26/// The environment of one suite: started once, before the first test that needs it, shared by
27/// all the tests of the suite and shut down after the last one.
28///
29/// Implementations can write `async fn` for both methods.
30pub trait Env: Deref<Target = Core> + Send + Sync + Sized + 'static {
31    fn start(suite: &'static str) -> impl Future<Output = Result<Self>>;
32
33    /// Stops what `start` started. The process exits right after it, without running
34    /// destructors, so a container left here stays running.
35    fn shutdown(&self) -> impl Future<Output = ()>;
36}
37
38/// Settings from the `E2E_*` environment variables.
39#[derive(Debug, Clone)]
40pub struct Settings {
41    pub workspace_root: PathBuf,
42    pub target_dir: PathBuf,
43    /// `E2E_RELEASE`: builds in release mode.
44    pub release: bool,
45    /// `E2E_WASM_OPT`: runs `wasm-opt` on the apps, as a shipped build has it. Off by default:
46    /// it takes long and changes nothing a functional test looks at.
47    pub wasm_opt: bool,
48    /// `E2E_SKIP_BUILD`: uses the last build.
49    pub skip_build: bool,
50    /// `E2E_HEADLESS=0` shows the browser window.
51    pub headless: bool,
52    /// `E2E_CHROMEDRIVER`: chromedriver to use instead of downloading it.
53    pub chromedriver: Option<PathBuf>,
54    /// `E2E_WEBDRIVER`: URL of a running WebDriver (`http://localhost:9515`) to open the
55    /// sessions with, instead of a chromedriver started by the harness.
56    pub webdriver: Option<String>,
57}
58
59impl Settings {
60    /// `manifest_dir` is the `CARGO_MANIFEST_DIR` of the e2e crate, which lives directly in
61    /// the root of its workspace.
62    pub fn from_env(manifest_dir: &str) -> Result<Self> {
63        let workspace_root = Path::new(manifest_dir)
64            .parent()
65            .with_context(|| format!("{manifest_dir} is not inside a workspace"))?
66            .to_path_buf();
67
68        let target_dir = match std::env::var_os("CARGO_TARGET_DIR") {
69            Some(dir) => workspace_root.join(dir),
70            None => workspace_root.join("target"),
71        };
72
73        Ok(Self {
74            workspace_root,
75            target_dir,
76            release: flag("E2E_RELEASE", false),
77            wasm_opt: flag("E2E_WASM_OPT", false),
78            skip_build: flag("E2E_SKIP_BUILD", false),
79            headless: flag("E2E_HEADLESS", true),
80            chromedriver: std::env::var_os("E2E_CHROMEDRIVER").map(PathBuf::from),
81            webdriver: std::env::var("E2E_WEBDRIVER").ok(),
82        })
83    }
84
85    pub fn profile(&self) -> &'static str {
86        if self.release { "release" } else { "debug" }
87    }
88
89    /// `target/e2e/<profile>`, where the apps are built to.
90    pub fn build_dir(&self) -> PathBuf {
91        self.target_dir.join("e2e").join(self.profile())
92    }
93
94    /// `target/e2e/<suite>`: logs and artifacts of failed tests.
95    pub fn work_dir(&self, suite: &str) -> PathBuf {
96        self.target_dir.join("e2e").join(suite)
97    }
98}
99
100fn flag(name: &str, default: bool) -> bool {
101    match std::env::var(name) {
102        Ok(value) => parse_flag(&value),
103        Err(_) => default,
104    }
105}
106
107fn parse_flag(value: &str) -> bool {
108    !matches!(value, "" | "0" | "false" | "no")
109}
110
111/// How the Chrome sessions are set up, apart from what every app gets.
112#[derive(Debug, Clone)]
113pub struct ChromeConfig {
114    pub window_size: (u32, u32),
115    /// The language of the browser: `--lang`, and `--accept-lang`, which decides
116    /// `Accept-Language` and `navigator.languages`. Chrome on Linux takes the language from the
117    /// environment (`LANGUAGE`, `LANG`) and not from `--lang`, so without `--accept-lang` they
118    /// would depend on the machine. `Intl` and `toLocaleString` still do.
119    pub lang: &'static str,
120    /// Additional command line arguments.
121    pub args: &'static [&'static str],
122}
123
124impl ChromeConfig {
125    /// The command line arguments that come from the config.
126    fn arguments(&self) -> Vec<String> {
127        let (width, height) = self.window_size;
128        // `pl-PL,pl`: Chrome adds `pl` to `Accept-Language` by itself, but not to
129        // `navigator.languages`, and the app is to get the same list on the server and in
130        // the browser
131        let accept_lang = match self.lang.split_once('-') {
132            Some((language, _)) => format!("{},{language}", self.lang),
133            None => self.lang.to_string(),
134        };
135        let mut arguments = vec![
136            format!("--window-size={width},{height}"),
137            format!("--lang={}", self.lang),
138            format!("--accept-lang={accept_lang}"),
139        ];
140        let defaults = [
141            "--no-first-run",
142            "--no-default-browser-check",
143            "--disable-search-engine-choice-screen",
144        ];
145        arguments.extend(defaults.iter().chain(self.args).map(|arg| arg.to_string()));
146        arguments
147    }
148}
149
150impl Default for ChromeConfig {
151    fn default() -> Self {
152        Self {
153            window_size: (1400, 1000),
154            lang: "en-US",
155            args: &[],
156        }
157    }
158}
159
160/// The part of the environment every suite has. An [`Env`] dereferences to it.
161pub struct Core {
162    pub suite: &'static str,
163    pub settings: Settings,
164    /// `http://127.0.0.1:<port>`, without a trailing slash.
165    pub base_url: String,
166    /// `target/e2e/<suite>`: logs and artifacts of failed tests.
167    pub work_dir: PathBuf,
168    pub http: reqwest::Client,
169    chrome: ChromeConfig,
170    webdriver: Arc<WebDriverManager>,
171}
172
173impl Core {
174    /// Prepares the work directory, removing what the previous run left in `artifacts`.
175    pub fn new(
176        suite: &'static str,
177        settings: Settings,
178        base_url: String,
179        chrome: ChromeConfig,
180    ) -> Result<Self> {
181        let work_dir = settings.work_dir(suite);
182        std::fs::create_dir_all(&work_dir)
183            .with_context(|| format!("can't create {}", work_dir.display()))?;
184        // Only the failures of this run belong there
185        let _ = std::fs::remove_dir_all(work_dir.join("artifacts"));
186
187        let http = reqwest::Client::builder()
188            .timeout(Duration::from_secs(30))
189            .build()?;
190
191        Ok(Self {
192            suite,
193            webdriver: webdriver_manager(&settings),
194            settings,
195            base_url,
196            work_dir,
197            http,
198            chrome,
199        })
200    }
201
202    pub fn url(&self, path: &str) -> String {
203        format!("{}{path}", self.base_url)
204    }
205
206    /// A new Chrome session. Each browser test gets its own.
207    pub async fn new_browser(&self) -> Result<WebDriver> {
208        let mut caps = DesiredCapabilities::chrome();
209        if self.settings.headless {
210            caps.set_headless()?;
211        }
212        for arg in self.chrome.arguments() {
213            caps.add_arg(&arg)?;
214        }
215        // After a login form is used, the password manager offers to save the password (and
216        // warns about it having leaked). Its bubble takes the clicks meant for the page.
217        caps.add_experimental_option(
218            "prefs",
219            serde_json::json!({
220                "credentials_enable_service": false,
221                "profile.password_manager_enabled": false,
222                "profile.password_manager_leak_detection": false,
223            }),
224        )?;
225        caps.add_arg("--disable-features=PasswordLeakDetection")?;
226        // The console is checked after each test
227        caps.set_browser_log_level(LoggingPrefsLogLevel::All)?;
228        // Leaves `window.alert`/`confirm` open for the test to answer
229        caps.set("unhandledPromptBehavior", "ignore")?;
230
231        let driver = match &self.settings.webdriver {
232            Some(url) => WebDriver::builder(url, caps)
233                .poller(poller())
234                .await
235                .with_context(|| format!("can't start Chrome through the WebDriver at {url}"))?,
236            None => self
237                .webdriver
238                .launch(caps)
239                .await
240                .context("can't start Chrome through chromedriver")?,
241        };
242
243        // With E2E_HEADLESS=0 the windows of parallel tests take the system focus from each
244        // other. A page losing it blurs its focused element, which closes drop-downs in the
245        // middle of a test. Emulated, the page keeps the focus, as it does headless.
246        let emulated = driver
247            .cdp()
248            .send_raw(
249                "Emulation.setFocusEmulationEnabled",
250                serde_json::json!({ "enabled": true }),
251            )
252            .await;
253        if let Err(err) = emulated {
254            let _ = driver.quit().await;
255            return Err(err).context("can't emulate the focus of the page");
256        }
257        Ok(driver)
258    }
259}
260
261/// A port nothing listens on at the moment, for a server the environment starts.
262pub fn free_port() -> Result<u16> {
263    let listener = TcpListener::bind("127.0.0.1:0")?;
264    Ok(listener.local_addr()?.port())
265}
266
267/// Element queries wait up to [`WAIT`], checking every [`POLL`].
268fn poller() -> Arc<ElementPollerWithTimeout> {
269    Arc::new(ElementPollerWithTimeout::new(WAIT, POLL))
270}
271
272fn webdriver_manager(settings: &Settings) -> Arc<WebDriverManager> {
273    let mut builder = WebDriverManager::builder().match_local().poller(poller());
274    if let Some(chromedriver) = &settings.chromedriver {
275        builder = builder.driver_binary(BrowserKind::Chrome, chromedriver);
276    }
277    builder.build()
278}
279
280#[cfg(test)]
281mod tests {
282    use super::{ChromeConfig, parse_flag};
283
284    #[test]
285    fn the_language_decides_accept_language_too() {
286        let config = ChromeConfig {
287            lang: "pl-PL",
288            args: &["--mute-audio"],
289            ..Default::default()
290        };
291        let arguments = config.arguments();
292        for expected in ["--lang=pl-PL", "--accept-lang=pl-PL,pl", "--mute-audio"] {
293            assert!(arguments.iter().any(|arg| arg == expected), "{arguments:?}");
294        }
295    }
296
297    #[test]
298    fn a_language_without_a_region_is_accepted_alone() {
299        let config = ChromeConfig {
300            lang: "pl",
301            ..Default::default()
302        };
303        let arguments = config.arguments();
304        assert!(
305            arguments.iter().any(|arg| arg == "--accept-lang=pl"),
306            "{arguments:?}"
307        );
308    }
309
310    #[test]
311    fn flags() {
312        for off in ["", "0", "false", "no"] {
313            assert!(!parse_flag(off), "{off:?}");
314        }
315        for on in ["1", "true", "yes"] {
316            assert!(parse_flag(on), "{on:?}");
317        }
318    }
319}