Skip to main content

yew_e2e/test_driver/
storage.rs

1//! `localStorage`, read, seeded and blocked.
2
3use anyhow::Result;
4
5use crate::TestDriver;
6
7impl TestDriver {
8    /// Read a browser-persisted setting without changing it.
9    pub async fn local_storage(&self, key: &str) -> Result<Option<String>> {
10        let value = self
11            .inner
12            .execute(
13                "return localStorage.getItem(arguments[0]);",
14                vec![serde_json::json!(key)],
15            )
16            .await?;
17        Ok(value.json().as_str().map(str::to_owned))
18    }
19
20    /// Seed a persisted setting before mounting the component that reads it.
21    pub async fn set_local_storage(&self, key: &str, value: &str) -> Result<()> {
22        self.inner
23            .execute(
24                "localStorage.setItem(arguments[0], arguments[1]);",
25                vec![serde_json::json!(key), serde_json::json!(value)],
26            )
27            .await?;
28        Ok(())
29    }
30
31    /// Deny access for the current document, as when browser policy blocks storage.
32    pub async fn block_local_storage(&self) -> Result<()> {
33        let function = "() => { \
34             Object.defineProperty(window, 'localStorage', { \
35                 configurable: true, \
36                 get() { throw new DOMException('Storage blocked by test', 'SecurityError'); }, \
37             }); \
38         }";
39
40        self.inject(function, Vec::new()).await
41    }
42}