1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! WebStorage: per-origin `localStorage` / `sessionStorage` access.
//!
//! Obtained via [`Page::local_storage`](crate::protocol::Page::local_storage) /
//! [`Page::session_storage`](crate::protocol::Page::session_storage). Reads and
//! writes the current origin's storage directly through the Page channel (not via
//! `page.evaluate`), matching playwright-python's `WebStorage`.
//!
//! ```no_run
//! # use playwright_rs::Playwright;
//! # async fn ex() -> playwright_rs::Result<()> {
//! # let pw = Playwright::launch().await?;
//! # let browser = pw.chromium().launch().await?;
//! # let page = browser.new_page().await?;
//! page.goto("https://example.com", None).await?;
//! let storage = page.local_storage();
//! storage.set_item("token", "abc123").await?;
//! assert_eq!(storage.get_item("token").await?, Some("abc123".to_string()));
//! # Ok(())
//! # }
//! ```
//!
//! See: <https://playwright.dev/docs/api/class-webstorage>
use crate::error::Result;
use crate::server::channel::Channel;
use serde_json::json;
/// Which storage area a [`WebStorage`] handle targets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebStorageKind {
/// `window.localStorage` — persists across sessions for the origin.
Local,
/// `window.sessionStorage` — cleared when the tab/context closes.
Session,
}
impl WebStorageKind {
pub(crate) fn as_str(self) -> &'static str {
match self {
WebStorageKind::Local => "local",
WebStorageKind::Session => "session",
}
}
}
/// Read/write access to a page's `localStorage` or `sessionStorage` for the
/// current origin.
///
/// Obtained from [`Page::local_storage`](crate::protocol::Page::local_storage)
/// and [`Page::session_storage`](crate::protocol::Page::session_storage).
///
/// See: <https://playwright.dev/docs/api/class-webstorage>
#[derive(Clone)]
pub struct WebStorage {
channel: Channel,
kind: WebStorageKind,
}
impl std::fmt::Debug for WebStorage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WebStorage")
.field("kind", &self.kind)
.finish_non_exhaustive()
}
}
impl WebStorage {
pub(crate) fn new(channel: Channel, kind: WebStorageKind) -> Self {
Self { channel, kind }
}
/// Returns the value for `name`, or `None` if the key is not set.
///
/// # Errors
///
/// Returns error if:
/// - The page has been closed
/// - Communication with the browser process fails
///
/// See: <https://playwright.dev/docs/api/class-webstorage#web-storage-get-item>
pub async fn get_item(&self, name: &str) -> Result<Option<String>> {
#[derive(serde::Deserialize)]
struct R {
#[serde(default)]
value: Option<String>,
}
let r: R = self
.channel
.send(
"webStorageGetItem",
json!({ "kind": self.kind.as_str(), "name": name }),
)
.await?;
Ok(r.value)
}
/// Sets `name` to `value`.
///
/// # Errors
///
/// Returns error if:
/// - The page has been closed
/// - Communication with the browser process fails
///
/// See: <https://playwright.dev/docs/api/class-webstorage#web-storage-set-item>
pub async fn set_item(&self, name: &str, value: &str) -> Result<()> {
self.channel
.send_no_result(
"webStorageSetItem",
json!({ "kind": self.kind.as_str(), "name": name, "value": value }),
)
.await
}
/// Removes `name` from storage (no-op if absent).
///
/// # Errors
///
/// Returns error if:
/// - The page has been closed
/// - Communication with the browser process fails
///
/// See: <https://playwright.dev/docs/api/class-webstorage#web-storage-remove-item>
pub async fn remove_item(&self, name: &str) -> Result<()> {
self.channel
.send_no_result(
"webStorageRemoveItem",
json!({ "kind": self.kind.as_str(), "name": name }),
)
.await
}
/// Removes all entries from this storage area.
///
/// # Errors
///
/// Returns error if:
/// - The page has been closed
/// - Communication with the browser process fails
///
/// See: <https://playwright.dev/docs/api/class-webstorage#web-storage-clear>
pub async fn clear(&self) -> Result<()> {
self.channel
.send_no_result("webStorageClear", json!({ "kind": self.kind.as_str() }))
.await
}
/// Returns all `(name, value)` entries currently in this storage area.
///
/// # Errors
///
/// Returns error if:
/// - The page has been closed
/// - Communication with the browser process fails
///
/// See: <https://playwright.dev/docs/api/class-webstorage#web-storage-items>
pub async fn items(&self) -> Result<Vec<(String, String)>> {
#[derive(serde::Deserialize)]
struct Item {
name: String,
value: String,
}
#[derive(serde::Deserialize)]
struct R {
items: Vec<Item>,
}
let r: R = self
.channel
.send("webStorageItems", json!({ "kind": self.kind.as_str() }))
.await?;
Ok(r.items.into_iter().map(|i| (i.name, i.value)).collect())
}
}
#[cfg(test)]
mod tests {
use super::WebStorageKind;
#[test]
fn kind_as_str_maps_each_variant() {
assert_eq!(WebStorageKind::Local.as_str(), "local");
assert_eq!(WebStorageKind::Session.as_str(), "session");
}
}