Skip to main content

yew_e2e/test_driver/
input.rs

1//! Giving a field or an element what a hand could not.
2
3use anyhow::Result;
4
5use crate::{TestDriver, TestElement};
6
7impl TestDriver {
8    /// **Give a field a value the way a native control would**, then fire
9    /// `event` on it, for fields such as `<input type="color">` whose own
10    /// picker the driver cannot reach.
11    pub async fn set_value(&self, element: &TestElement, value: &str, event: &str) -> Result<()> {
12        tracing::trace!(value, event, "Setting a field's value");
13
14        self.inner
15            .execute(
16                "arguments[0].value = arguments[1]; \
17                 arguments[0].dispatchEvent(new Event(arguments[2], {bubbles: true}));",
18                vec![
19                    element.inner.to_json()?,
20                    serde_json::json!(value),
21                    serde_json::json!(event),
22                ],
23            )
24            .await?;
25
26        Ok(())
27    }
28
29    /// **Replace the text an element already shows**, for content the test
30    /// machine cannot be relied on to produce, such as a very long name. Only the existing text node's data changes, so the
31    /// page keeps owning the node.
32    pub async fn set_text(&self, element: &TestElement, text: &str) -> Result<()> {
33        tracing::trace!(text, "Replacing an element's text");
34
35        let script = "const node = arguments[0].firstChild; \
36             if (!node || node.nodeType !== Node.TEXT_NODE) throw new Error('no text node'); \
37             node.data = arguments[1];";
38
39        self.inner
40            .execute(
41                script,
42                vec![element.inner.to_json()?, serde_json::json!(text)],
43            )
44            .await?;
45
46        Ok(())
47    }
48}