playwright_core/protocol/
element_handle.rs1use crate::channel_owner::{ChannelOwner, ChannelOwnerImpl, ParentOrConnection};
7use crate::error::Result;
8use base64::Engine;
9use serde::Deserialize;
10use serde_json::Value;
11use std::any::Any;
12use std::sync::Arc;
13
14#[derive(Clone)]
21pub struct ElementHandle {
22 base: ChannelOwnerImpl,
23}
24
25impl ElementHandle {
26 pub fn new(
31 parent: Arc<dyn ChannelOwner>,
32 type_name: String,
33 guid: Arc<str>,
34 initializer: Value,
35 ) -> Result<Self> {
36 let base = ChannelOwnerImpl::new(
37 ParentOrConnection::Parent(parent),
38 type_name,
39 guid,
40 initializer,
41 );
42
43 Ok(Self { base })
44 }
45
46 pub async fn screenshot(
69 &self,
70 options: Option<crate::protocol::ScreenshotOptions>,
71 ) -> Result<Vec<u8>> {
72 let params = if let Some(opts) = options {
73 opts.to_json()
74 } else {
75 serde_json::json!({
77 "type": "png",
78 "timeout": crate::DEFAULT_TIMEOUT_MS
79 })
80 };
81
82 #[derive(Deserialize)]
83 struct ScreenshotResponse {
84 binary: String,
85 }
86
87 let response: ScreenshotResponse = self.base.channel().send("screenshot", params).await?;
88
89 let bytes = base64::prelude::BASE64_STANDARD
91 .decode(&response.binary)
92 .map_err(|e| {
93 crate::error::Error::ProtocolError(format!(
94 "Failed to decode element screenshot: {}",
95 e
96 ))
97 })?;
98
99 Ok(bytes)
100 }
101}
102
103impl ChannelOwner for ElementHandle {
104 fn guid(&self) -> &str {
105 self.base.guid()
106 }
107
108 fn type_name(&self) -> &str {
109 self.base.type_name()
110 }
111
112 fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
113 self.base.parent()
114 }
115
116 fn connection(&self) -> Arc<dyn crate::connection::ConnectionLike> {
117 self.base.connection()
118 }
119
120 fn initializer(&self) -> &Value {
121 self.base.initializer()
122 }
123
124 fn channel(&self) -> &crate::channel::Channel {
125 self.base.channel()
126 }
127
128 fn dispose(&self, reason: crate::channel_owner::DisposeReason) {
129 self.base.dispose(reason)
130 }
131
132 fn adopt(&self, child: Arc<dyn ChannelOwner>) {
133 self.base.adopt(child)
134 }
135
136 fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
137 self.base.add_child(guid, child)
138 }
139
140 fn remove_child(&self, guid: &str) {
141 self.base.remove_child(guid)
142 }
143
144 fn on_event(&self, _method: &str, _params: Value) {
145 }
147
148 fn was_collected(&self) -> bool {
149 self.base.was_collected()
150 }
151
152 fn as_any(&self) -> &dyn Any {
153 self
154 }
155}
156
157impl std::fmt::Debug for ElementHandle {
158 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159 f.debug_struct("ElementHandle")
160 .field("guid", &self.guid())
161 .finish()
162 }
163}