playwright_rs/protocol/root.rs
1// Copyright 2026 Paul Adamson
2// Licensed under the Apache License, Version 2.0
3//
4// Root - Internal object for sending initialize message
5//
6// Reference:
7// - Python: playwright-python/playwright/_impl/_connection.py (RootChannelOwner)
8// - Java: playwright-java/.../impl/Connection.java (Root inner class)
9// - .NET: playwright-dotnet/src/Playwright/Transport/Connection.cs (InitializePlaywrightAsync)
10
11use crate::error::Result;
12use crate::server::channel::Channel;
13use crate::server::channel_owner::{
14 ChannelOwner, ChannelOwnerImpl, DisposeReason, ParentOrConnection,
15};
16use crate::server::connection::ConnectionLike;
17use serde_json::Value;
18use std::any::Any;
19use std::sync::Arc;
20
21/// Root object for sending the initialize message to the Playwright server
22///
23/// This is an internal object not exposed to end users. It exists solely to
24/// send the `initialize` message to the server during connection setup.
25///
26/// # Protocol Flow
27///
28/// When `initialize()` is called:
29/// 1. Sends `initialize` message with `sdkLanguage: "rust"`
30/// 2. Server creates BrowserType objects (sends `__create__` messages)
31/// 3. Server creates Playwright object (sends `__create__` message)
32/// 4. Server responds with Playwright GUID: `{ "playwright": { "guid": "..." } }`
33/// 5. All objects are now in the connection's object registry
34///
35/// The Root object has an empty GUID (`""`) and is not registered in the
36/// object registry. It's discarded after initialization completes.
37///
38/// # Example
39///
40/// ```ignore
41/// # use playwright_rs::protocol::Root;
42/// # use playwright_rs::server::connection::ConnectionLike;
43/// # use std::sync::Arc;
44/// # async fn example(connection: Arc<dyn ConnectionLike>) -> Result<(), Box<dyn std::error::Error>> {
45/// // Create root object with connection
46/// let root = Root::new(connection.clone());
47///
48/// // Send initialize message to server
49/// let response = root.initialize().await?;
50///
51/// // Verify Playwright GUID is returned
52/// let playwright_guid = response["playwright"]["guid"]
53/// .as_str()
54/// .expect("Missing playwright.guid");
55/// assert!(!playwright_guid.is_empty());
56/// assert!(playwright_guid.contains("playwright"));
57///
58/// // Verify response contains BrowserType objects
59/// assert!(response["playwright"].is_object());
60/// # Ok(())
61/// # }
62/// ```
63///
64/// See:
65/// - Python: <https://github.com/microsoft/playwright-python/blob/main/playwright/_impl/_connection.py>
66/// - Java: <https://github.com/microsoft/playwright-java>
67pub struct Root {
68 /// Base ChannelOwner implementation
69 base: ChannelOwnerImpl,
70}
71
72impl Root {
73 /// Creates a new Root object
74 ///
75 /// # Arguments
76 ///
77 /// * `connection` - The connection to the Playwright server
78 pub fn new(connection: Arc<dyn ConnectionLike>) -> Self {
79 Self {
80 base: ChannelOwnerImpl::new(
81 ParentOrConnection::Connection(connection),
82 "Root".to_string(),
83 Arc::from(""), // Empty GUID - Root is not registered in object map
84 Value::Null,
85 ),
86 }
87 }
88
89 /// Send the initialize message to the Playwright server
90 ///
91 /// This is a synchronous request that blocks until the server responds.
92 /// By the time the response arrives, all protocol objects (Playwright,
93 /// BrowserType, etc.) will have been created and registered.
94 ///
95 /// # Returns
96 ///
97 /// The server response containing the Playwright object GUID:
98 /// ```json
99 /// {
100 /// "playwright": {
101 /// "guid": "playwright"
102 /// }
103 /// }
104 /// ```
105 ///
106 /// # Errors
107 ///
108 /// Returns error if:
109 /// - Message send fails
110 /// - Server returns protocol error
111 /// - Connection is closed
112 pub async fn initialize(&self) -> Result<Value> {
113 self.channel()
114 .send(
115 "initialize",
116 serde_json::json!({
117 // TODO: Use "rust" once upstream Playwright accepts it
118 // Current issue: Playwright v1.49.0 protocol validator only accepts:
119 // (javascript|python|java|csharp)
120 //
121 // Using "python" because:
122 // - Closest async/await patterns to Rust
123 // - sdkLanguage only affects CLI error messages and codegen
124 // - Does NOT affect core protocol functionality
125 // - Python error messages are appropriate ("playwright install")
126 //
127 // Plan: Contribute to microsoft/playwright to add 'rust' to Language enum
128 // See: packages/playwright-core/src/utils/isomorphic/locatorGenerators.ts
129 "sdkLanguage": "python"
130 }),
131 )
132 .await
133 }
134}
135
136impl ChannelOwner for Root {
137 fn guid(&self) -> &str {
138 self.base.guid()
139 }
140
141 fn type_name(&self) -> &str {
142 self.base.type_name()
143 }
144
145 fn parent(&self) -> Option<Arc<dyn ChannelOwner>> {
146 self.base.parent()
147 }
148
149 fn connection(&self) -> Arc<dyn ConnectionLike> {
150 self.base.connection()
151 }
152
153 fn initializer(&self) -> &Value {
154 self.base.initializer()
155 }
156
157 fn channel(&self) -> &Channel {
158 self.base.channel()
159 }
160
161 fn dispose(&self, reason: DisposeReason) {
162 self.base.dispose(reason)
163 }
164
165 fn adopt(&self, child: Arc<dyn ChannelOwner>) {
166 self.base.adopt(child)
167 }
168
169 fn add_child(&self, guid: Arc<str>, child: Arc<dyn ChannelOwner>) {
170 self.base.add_child(guid, child)
171 }
172
173 fn remove_child(&self, guid: &str) {
174 self.base.remove_child(guid)
175 }
176
177 fn on_event(&self, method: &str, params: Value) {
178 self.base.on_event(method, params)
179 }
180
181 fn was_collected(&self) -> bool {
182 self.base.was_collected()
183 }
184
185 fn as_any(&self) -> &dyn Any {
186 self
187 }
188}
189
190impl std::fmt::Debug for Root {
191 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192 f.debug_struct("Root")
193 .field("guid", &self.guid())
194 .field("type_name", &self.type_name())
195 .finish()
196 }
197}
198
199// Note: Root object testing is done via integration tests since it requires:
200// - A real Connection to send messages
201// - A real Playwright server to respond
202// See: crates/playwright-core/tests/initialization_integration.rs