yew_e2e/test_driver.rs
1//! [`TestDriver`], the browser a test is handed, and its methods group by
2//! group.
3
4use std::sync::atomic::AtomicBool;
5
6use anyhow::{Result, ensure};
7use thirtyfour::prelude::*;
8use tokio::time::{self, Duration};
9
10use crate::finding::common;
11use crate::{Holding, IntoBy, TestElement, Waiting};
12
13mod attributes;
14mod canvas;
15mod capture;
16mod drag;
17mod input;
18mod keyboard;
19mod navigation;
20mod pointer;
21mod scripts;
22mod scroll;
23mod storage;
24mod style;
25mod wheel;
26
27pub use self::canvas::CanvasSize;
28
29/// A number handed to a page script as an argument, refused unless it is
30/// finite: JSON has no infinity or NaN, so the page would read `null` instead.
31fn finite(what: &str, value: f64) -> Result<serde_json::Value> {
32 ensure!(value.is_finite(), "{what} must be finite, not {value}");
33 Ok(serde_json::json!(value))
34}
35
36/// Wrapper for `WebDriver` which adds convenience methods for testing.
37pub struct TestDriver {
38 pub(crate) inner: WebDriver,
39 pub(crate) downloads: tempfile::TempDir,
40 pub(crate) wait_timeout: Duration,
41 pub(crate) load_timeout: Duration,
42 pub(crate) waiting: Waiting,
43 /// Whether this test asked for [`TestDriver::watch_for_errors`], so the
44 /// helpers that navigate know to watch the page they arrive at too.
45 pub(crate) watching: AtomicBool,
46 pub(crate) holding: Holding,
47}
48
49impl TestDriver {
50 /// Files saved by this browser, cleared before each test and removed when it closes.
51 pub fn downloads(&self) -> &std::path::Path {
52 self.downloads.path()
53 }
54
55 /// What this browser's test was last waiting for, for the runner to hang
56 /// off a report of giving up on it.
57 pub(crate) fn waiting(&self) -> Waiting {
58 self.waiting.clone()
59 }
60
61 /// The WebDriver session underneath, for what this wrapper has no method
62 /// for.
63 pub fn webdriver(&self) -> &WebDriver {
64 &self.inner
65 }
66
67 /// How long one observation of a page that is already up may take to
68 /// agree: the deadline [`Self::wait_until`] and the finds wait against.
69 pub fn wait_timeout(&self) -> Duration {
70 self.wait_timeout
71 }
72
73 /// How long the first look at a page may take: the browser starting, the
74 /// bundle being fetched, the wasm compiled, none of which has happened
75 /// when a page is first asked for.
76 pub fn load_timeout(&self) -> Duration {
77 self.load_timeout
78 }
79
80 /// **Run `function`, a JavaScript function's source, as the page's own
81 /// script**, called with `args`.
82 ///
83 /// A WebDriver script runs apart from the page in some browsers (Firefox
84 /// runs it in a sandbox), so what it puts on `window` or a prototype is
85 /// not what the page sees. A `<script>` element added to the document
86 /// runs as the page, and runs at once; it cannot read `arguments`, so the
87 /// call it makes is built in the page, with `args` as JSON.
88 async fn inject(&self, function: &str, args: Vec<serde_json::Value>) -> Result<()> {
89 let script = "const [source, args] = arguments; \
90 const inject = document.createElement('script'); \
91 inject.textContent = `(${source})(...${JSON.stringify(args)});`; \
92 document.documentElement.appendChild(inject); \
93 inject.remove();";
94
95 self.inner
96 .execute(
97 script,
98 vec![serde_json::json!(function), serde_json::json!(args)],
99 )
100 .await?;
101 Ok(())
102 }
103
104 common!();
105}