playwright-rs 0.12.0

Rust bindings for Microsoft Playwright
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// JSHandle protocol object
//
// Represents a handle to an in-browser JavaScript value.
// JSHandles are created via frame.evaluate_handle_js() and jshandle.get_property().
//
// Architecture:
// - ChannelOwner with GUID like "JSHandle@abc123"
// - Methods communicate via JSON-RPC over the channel
// - ElementHandle is conceptually a subtype but kept separate in this Rust implementation
//
// See: <https://playwright.dev/docs/api/class-jshandle>

use crate::error::Result;
use crate::protocol::evaluate_conversion::parse_result;
use crate::server::channel_owner::{ChannelOwner, ChannelOwnerImpl, ParentOrConnection};
use crate::server::connection::ConnectionExt;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;

/// JSHandle represents an in-browser JavaScript object.
///
/// JSHandles are created via [`Frame::evaluate_handle_js`](crate::protocol::Frame::evaluate_handle_js)
/// and [`JSHandle::get_property`]. Unlike `evaluate`, which serializes the return value,
/// `evaluate_handle_js` returns a live handle to the in-browser object.
///
/// # Example
///
/// ```ignore
/// # use playwright_rs::protocol::Playwright;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let playwright = Playwright::launch().await?;
/// let browser = playwright.chromium().launch().await?;
/// let page = browser.new_page().await?;
/// page.goto("https://example.com", None).await?;
///
/// let frame = page.main_frame().await?;
/// let handle = frame.evaluate_handle_js("() => ({ name: 'test' })").await?;
///
/// // Get the JSON-serializable value
/// let value = handle.json_value().await?;
/// assert_eq!(value["name"], "test");
///
/// // Get a specific property
/// let name_handle = handle.get_property("name").await?;
/// let name = name_handle.json_value().await?;
/// assert_eq!(name, "test");
///
/// // Release the handle
/// handle.dispose().await?;
///
/// browser.close().await?;
/// # Ok(())
/// # }
/// ```
///
/// See: <https://playwright.dev/docs/api/class-jshandle>
#[derive(Clone)]
pub struct JSHandle {
    base: ChannelOwnerImpl,
}

impl JSHandle {
    /// Creates a new JSHandle from protocol initialization.
    ///
    /// This is called by the object factory when the server sends a `__create__` message
    /// for a JSHandle object.
    pub fn new(
        parent: Arc<dyn ChannelOwner>,
        type_name: String,
        guid: Arc<str>,
        initializer: Value,
    ) -> Result<Self> {
        let base = ChannelOwnerImpl::new(
            ParentOrConnection::Parent(parent),
            type_name,
            guid,
            initializer,
        );

        Ok(Self { base })
    }

    /// Returns the JSON-serializable value of this handle.
    ///
    /// Serializes the JavaScript object to JSON. If the object has circular references
    /// or is not JSON-serializable (e.g., a DOM element), this will return an error.
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-json-value>
    pub async fn json_value(&self) -> Result<Value> {
        #[derive(Deserialize)]
        struct JsonValueResponse {
            value: Value,
        }

        let response: JsonValueResponse = self
            .base
            .channel()
            .send("jsonValue", serde_json::json!({}))
            .await?;

        Ok(parse_result(&response.value))
    }

    /// Returns a JSHandle for a named property of this object.
    ///
    /// # Arguments
    ///
    /// * `name` - The property name to retrieve
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-get-property>
    pub async fn get_property(&self, name: &str) -> Result<JSHandle> {
        #[derive(Deserialize)]
        struct HandleRef {
            guid: String,
        }
        #[derive(Deserialize)]
        struct GetPropertyResponse {
            handle: HandleRef,
        }

        let response: GetPropertyResponse = self
            .base
            .channel()
            .send(
                "getProperty",
                serde_json::json!({
                    "name": name
                }),
            )
            .await?;

        let guid = &response.handle.guid;
        let connection = self.base.connection();
        let mut attempts = 0;
        let max_attempts = 20;

        let handle = loop {
            match connection.get_typed::<JSHandle>(guid).await {
                Ok(h) => break h,
                Err(_) if attempts < max_attempts => {
                    attempts += 1;
                    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                }
                Err(e) => return Err(e),
            }
        };

        Ok(handle)
    }

    /// Returns a map of all enumerable own properties of this object.
    ///
    /// Each value in the map is a [`JSHandle`] for the property value.
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-get-properties>
    pub async fn get_properties(&self) -> Result<HashMap<String, JSHandle>> {
        #[derive(Deserialize)]
        struct PropertyEntry {
            name: String,
            value: HandleRef,
        }
        #[derive(Deserialize)]
        struct HandleRef {
            guid: String,
        }
        #[derive(Deserialize)]
        struct GetPropertiesResponse {
            properties: Vec<PropertyEntry>,
        }

        let response: GetPropertiesResponse = self
            .base
            .channel()
            .send("getPropertyList", serde_json::json!({}))
            .await?;

        let connection = self.base.connection();
        let mut map = HashMap::new();

        for entry in response.properties {
            let guid = &entry.name.clone();
            let handle_guid = &entry.value.guid;

            let mut attempts = 0;
            let max_attempts = 20;

            let handle = loop {
                match connection.get_typed::<JSHandle>(handle_guid).await {
                    Ok(h) => break h,
                    Err(_) if attempts < max_attempts => {
                        attempts += 1;
                        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                    }
                    Err(e) => return Err(e),
                }
            };

            map.insert(guid.clone(), handle);
        }

        Ok(map)
    }

    /// Evaluates a JavaScript expression with this handle as the first argument.
    ///
    /// Returns the deserialized result of the expression.
    ///
    /// # Arguments
    ///
    /// * `expression` - JavaScript expression (function or expression string)
    /// * `arg` - Optional additional argument to pass after the handle
    ///
    /// # Errors
    ///
    /// Returns an error if the JavaScript expression throws.
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-evaluate>
    pub async fn evaluate<R, T>(&self, expression: &str, arg: Option<&T>) -> Result<R>
    where
        R: DeserializeOwned,
        T: Serialize,
    {
        // The handle is passed as the first argument using the handles array protocol.
        // The argument value uses {"h": 0} to reference the first handle.
        // Any additional arg is not supported in this signature (matches playwright-python).
        let _ = arg; // arg parameter reserved for future use / compatibility

        let params = serde_json::json!({
            "expression": expression,
            "isFunction": true,
            "arg": {
                "value": {"h": 0},
                "handles": [{"guid": self.base.guid()}]
            }
        });

        #[derive(Deserialize)]
        struct EvaluateResult {
            value: Value,
        }

        let result: EvaluateResult = self
            .base
            .channel()
            .send("evaluateExpression", params)
            .await?;

        let parsed = parse_result(&result.value);
        serde_json::from_value(parsed).map_err(|e| {
            crate::error::Error::ProtocolError(format!("Failed to deserialize result: {}", e))
        })
    }

    /// Evaluates a JavaScript expression returning a new JSHandle.
    ///
    /// Unlike [`evaluate`](JSHandle::evaluate) which deserializes the result,
    /// this returns a handle to the in-browser object.
    ///
    /// # Arguments
    ///
    /// * `expression` - JavaScript expression (function or expression string)
    /// * `arg` - Optional additional argument to pass after the handle
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-evaluate-handle>
    pub async fn evaluate_handle<T>(&self, expression: &str, arg: Option<&T>) -> Result<JSHandle>
    where
        T: Serialize,
    {
        let _ = arg; // arg parameter reserved for future use / compatibility

        let params = serde_json::json!({
            "expression": expression,
            "isFunction": true,
            "arg": {
                "value": {"h": 0},
                "handles": [{"guid": self.base.guid()}]
            }
        });

        #[derive(Deserialize)]
        struct HandleRef {
            guid: String,
        }
        #[derive(Deserialize)]
        struct EvaluateHandleResponse {
            handle: HandleRef,
        }

        let response: EvaluateHandleResponse = self
            .base
            .channel()
            .send("evaluateExpressionHandle", params)
            .await?;

        let guid = &response.handle.guid;
        let connection = self.base.connection();
        let mut attempts = 0;
        let max_attempts = 20;

        let handle = loop {
            match connection.get_typed::<JSHandle>(guid).await {
                Ok(h) => break h,
                Err(_) if attempts < max_attempts => {
                    attempts += 1;
                    tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                }
                Err(e) => return Err(e),
            }
        };

        Ok(handle)
    }

    /// Releases this handle and frees the associated browser resources.
    ///
    /// After calling `dispose()`, the handle is no longer valid.
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-dispose>
    pub async fn dispose(&self) -> Result<()> {
        self.base
            .channel()
            .send_no_result("dispose", serde_json::json!({}))
            .await
    }

    /// Returns this handle as an [`ElementHandle`](crate::protocol::ElementHandle) if it
    /// represents a DOM element, or `None` if it is a non-element JS value.
    ///
    /// This method checks whether the protocol type of this handle is `"ElementHandle"`.
    /// In Playwright's protocol, DOM element handles are typed as `ElementHandle` while
    /// plain JavaScript values are typed as `JSHandle`.
    ///
    /// See: <https://playwright.dev/docs/api/class-jshandle#js-handle-as-element>
    pub fn as_element_type_name(&self) -> Option<&str> {
        // In practice, the server will have created an ElementHandle object
        // (not a JSHandle) for DOM elements. JSHandle.as_element() returns None
        // for non-DOM values. We check the stored type_name for forward compatibility.
        if self.base.type_name() == "ElementHandle" {
            Some(self.base.guid())
        } else {
            None
        }
    }
}

impl ChannelOwner for JSHandle {
    fn guid(&self) -> &str {
        self.base.guid()
    }

    fn type_name(&self) -> &str {
        self.base.type_name()
    }

    fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
        self.base.parent()
    }

    fn connection(&self) -> Arc<dyn crate::server::connection::ConnectionLike> {
        self.base.connection()
    }

    fn initializer(&self) -> &Value {
        self.base.initializer()
    }

    fn channel(&self) -> &crate::server::channel::Channel {
        self.base.channel()
    }

    fn dispose(&self, reason: crate::server::channel_owner::DisposeReason) {
        self.base.dispose(reason)
    }

    fn adopt(&self, child: Arc<dyn ChannelOwner>) {
        self.base.adopt(child)
    }

    fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
        self.base.add_child(guid, child)
    }

    fn remove_child(&self, guid: &str) {
        self.base.remove_child(guid)
    }

    fn on_event(&self, _method: &str, _params: Value) {
        // JSHandle has no events
    }

    fn was_collected(&self) -> bool {
        self.base.was_collected()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl std::fmt::Debug for JSHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JSHandle")
            .field("guid", &self.guid())
            .finish()
    }
}