browser_commander/playwright/protocol/
js_handle.rs1#![allow(clippy::all, missing_docs, unused_imports)]
5
6use serde::{Deserialize, Serialize};
7
8use super::*;
9use crate::playwright::connection::{
10 Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
11};
12
13#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct JSHandleInitializer {
16 #[serde(rename = "preview")]
17 pub preview: String,
18}
19
20#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
22pub struct JSHandleEvaluateExpressionParams {
23 #[serde(rename = "expression")]
24 pub expression: String,
25 #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
26 pub is_function: Option<bool>,
27 #[serde(rename = "arg")]
28 pub arg: SerializedArgument,
29}
30
31#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
33pub struct JSHandleEvaluateExpressionResult {
34 #[serde(rename = "value")]
35 pub value: SerializedValue,
36}
37
38#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
40pub struct JSHandleEvaluateExpressionHandleParams {
41 #[serde(rename = "expression")]
42 pub expression: String,
43 #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
44 pub is_function: Option<bool>,
45 #[serde(rename = "arg")]
46 pub arg: SerializedArgument,
47}
48
49#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
51pub struct JSHandleEvaluateExpressionHandleResult {
52 #[serde(rename = "handle")]
53 pub handle: Ref<JSHandle>,
54}
55
56#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
57pub struct JSHandleGetPropertyListResultProperties {
58 #[serde(rename = "name")]
59 pub name: String,
60 #[serde(rename = "value")]
61 pub value: Ref<JSHandle>,
62}
63
64#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
66pub struct JSHandleGetPropertyListResult {
67 #[serde(rename = "properties")]
68 pub properties: Vec<JSHandleGetPropertyListResultProperties>,
69}
70
71#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
73pub struct JSHandleGetPropertyParams {
74 #[serde(rename = "name")]
75 pub name: String,
76}
77
78#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
80pub struct JSHandleGetPropertyResult {
81 #[serde(rename = "handle")]
82 pub handle: Ref<JSHandle>,
83}
84
85#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
87pub struct JSHandleJsonValueResult {
88 #[serde(rename = "value")]
89 pub value: SerializedValue,
90}
91
92#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
94pub struct JSHandlePreviewUpdatedEventParams {
95 #[serde(rename = "preview")]
96 pub preview: String,
97}
98
99#[derive(Debug, Clone, PartialEq)]
101pub enum JSHandleEvent {
102 PreviewUpdated(JSHandlePreviewUpdatedEventParams),
104 Unknown {
106 method: String,
108 params: serde_json::Value,
110 },
111}
112
113impl ProtocolEvent for JSHandleEvent {
114 fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
115 match method {
116 "previewUpdated" => Ok(Self::PreviewUpdated(serde_json::from_value(params)?)),
117 _ => Ok(Self::Unknown { method: method.to_string(), params }),
118 }
119 }
120}
121
122#[derive(Debug, Clone)]
124pub struct JSHandle {
125 channel: Channel,
126}
127
128impl ChannelType for JSHandle {
129 const INTERFACE: &'static str = "JSHandle";
130 type Initializer = JSHandleInitializer;
131 type Event = JSHandleEvent;
132
133 fn accepts(interface: &str) -> bool {
134 matches!(interface, "JSHandle" | "ElementHandle")
135 }
136
137 fn from_channel(channel: Channel) -> Self {
138 Self { channel }
139 }
140
141 fn channel(&self) -> &Channel {
142 &self.channel
143 }
144}
145
146impl JSHandle {
147 pub async fn dispose(&self) -> Result<(), ProtocolError> {
149 self.channel.send_no_result("dispose", &serde_json::json!({})).await
150 }
151
152 pub async fn evaluate_expression(&self, params: JSHandleEvaluateExpressionParams) -> Result<JSHandleEvaluateExpressionResult, ProtocolError> {
154 self.channel.send("evaluateExpression", ¶ms).await
155 }
156
157 pub async fn evaluate_expression_handle(&self, params: JSHandleEvaluateExpressionHandleParams) -> Result<JSHandleEvaluateExpressionHandleResult, ProtocolError> {
159 self.channel.send("evaluateExpressionHandle", ¶ms).await
160 }
161
162 pub async fn get_property_list(&self) -> Result<JSHandleGetPropertyListResult, ProtocolError> {
164 self.channel.send("getPropertyList", &serde_json::json!({})).await
165 }
166
167 pub async fn get_property(&self, params: JSHandleGetPropertyParams) -> Result<JSHandleGetPropertyResult, ProtocolError> {
169 self.channel.send("getProperty", ¶ms).await
170 }
171
172 pub async fn json_value(&self) -> Result<JSHandleJsonValueResult, ProtocolError> {
174 self.channel.send("jsonValue", &serde_json::json!({})).await
175 }
176}