viewpoint-core 0.4.3

High-level browser automation API for Viewpoint
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! WebSocket monitoring and testing.
//!
//! This module provides functionality for monitoring WebSocket connections,
//! including frame events for sent and received messages. Use this to test
//! real-time features without polling.
//!
//! # Strategy for Testing Dynamic WebSocket Content
//!
//! Testing WebSocket-driven dynamic content requires:
//! 1. Set up WebSocket listeners **before** navigation
//! 2. Navigate to the page that establishes WebSocket connections
//! 3. Use Viewpoint's auto-waiting locator assertions to verify DOM updates
//! 4. Optionally capture WebSocket messages for detailed verification
//!
//! ## Verifying WebSocket Data Updates in the DOM (Without Polling)
//!
//! The key insight is to use Viewpoint's built-in auto-waiting assertions
//! (`expect(locator).to_have_text()`, `to_be_visible()`, etc.) which automatically
//! wait for conditions without manual polling:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//! use viewpoint_test::expect;  // from viewpoint-test crate
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let browser = Browser::launch().headless(true).launch().await?;
//! let context = browser.new_context().await?;
//! let page = context.new_page().await?;
//!
//! // Navigate to page with WebSocket-driven live data
//! page.goto("https://example.com/live-dashboard").goto().await?;
//!
//! // Auto-waiting assertions verify DOM updates without polling!
//! // These wait up to 30 seconds for the condition to be true
//!
//! // Verify that live data container becomes visible
//! expect(page.locator(".live-data-container")).to_be_visible().await?;
//!
//! // Verify that WebSocket data rendered specific text content
//! expect(page.locator(".stock-price")).to_contain_text("$").await?;
//!
//! // Verify multiple data points updated via WebSocket
//! expect(page.locator(".connection-status")).to_have_text("Connected").await?;
//! expect(page.locator(".last-update")).not().to_be_empty().await?;
//!
//! // Verify a list populated by WebSocket messages
//! expect(page.locator(".message-list li")).to_have_count_greater_than(0).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Capturing and Verifying Specific WebSocket Messages
//!
//! For more detailed verification, capture WebSocket frames and correlate
//! with DOM state:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//! use viewpoint_test::expect;
//! use std::sync::Arc;
//! use tokio::sync::Mutex;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let browser = Browser::launch().headless(true).launch().await?;
//! let context = browser.new_context().await?;
//! let page = context.new_page().await?;
//!
//! // Capture WebSocket messages for verification
//! let received_messages: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
//! let messages_clone = received_messages.clone();
//!
//! // Set up WebSocket monitoring BEFORE navigation
//! page.on_websocket(move |ws| {
//!     let messages = messages_clone.clone();
//!     async move {
//!         println!("WebSocket connected: {}", ws.url());
//!         
//!         // Capture all received frames
//!         ws.on_framereceived(move |frame| {
//!             let messages = messages.clone();
//!             async move {
//!                 if frame.is_text() {
//!                     messages.lock().await.push(frame.payload().to_string());
//!                 }
//!             }
//!         }).await;
//!     }
//! }).await;
//!
//! // Navigate to the page
//! page.goto("https://example.com/realtime-chat").goto().await?;
//!
//! // Wait for WebSocket data to be reflected in the DOM
//! // The auto-waiting assertion handles timing without polling
//! expect(page.locator(".chat-messages")).to_be_visible().await?;
//! expect(page.locator(".chat-message")).to_have_count_greater_than(0).await?;
//!
//! // Verify the DOM content matches what was received via WebSocket
//! let messages = received_messages.lock().await;
//! if !messages.is_empty() {
//!     // Parse the WebSocket message (assuming JSON)
//!     let first_msg = &messages[0];
//!     if first_msg.contains("\"text\":") {
//!         // Verify the message text appears in the DOM
//!         let msg_text = page.locator(".chat-message").first().text_content().await?;
//!         assert!(msg_text.is_some(), "Message should be rendered in DOM");
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Waiting for Specific WebSocket Events Before DOM Verification
//!
//! Use synchronization primitives to coordinate between WebSocket events
//! and DOM assertions:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//! use viewpoint_test::expect;
//! use std::sync::Arc;
//! use std::sync::atomic::{AtomicBool, Ordering};
//! use tokio::sync::Notify;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let browser = Browser::launch().headless(true).launch().await?;
//! let context = browser.new_context().await?;
//! let page = context.new_page().await?;
//!
//! // Use Notify to signal when specific data arrives
//! let data_ready = Arc::new(Notify::new());
//! let data_ready_clone = data_ready.clone();
//!
//! page.on_websocket(move |ws| {
//!     let notify = data_ready_clone.clone();
//!     async move {
//!         ws.on_framereceived(move |frame| {
//!             let notify = notify.clone();
//!             async move {
//!                 // Signal when we receive the expected data
//!                 if frame.payload().contains("\"status\":\"ready\"") {
//!                     notify.notify_one();
//!                 }
//!             }
//!         }).await;
//!     }
//! }).await;
//!
//! page.goto("https://example.com/app").goto().await?;
//!
//! // Wait for the specific WebSocket message (with timeout)
//! tokio::select! {
//!     _ = data_ready.notified() => {
//!         // Data arrived, now verify DOM reflects it
//!         expect(page.locator(".status-indicator")).to_have_text("Ready").await?;
//!         expect(page.locator(".data-panel")).to_be_visible().await?;
//!     }
//!     _ = tokio::time::sleep(std::time::Duration::from_secs(10)) => {
//!         panic!("Timeout waiting for WebSocket data");
//!     }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Complete Example: Testing a Real-Time Stock Ticker
//!
//! ```ignore
//! use viewpoint_core::Browser;
//! use viewpoint_test::{TestHarness, expect};
//! use std::sync::Arc;
//! use tokio::sync::Mutex;
//!
//! #[tokio::test]
//! async fn test_stock_ticker_updates() -> Result<(), Box<dyn std::error::Error>> {
//!     let harness = TestHarness::new().await?;
//!     let page = harness.page();
//!
//!     // Track stock updates received via WebSocket
//!     let stock_updates: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
//!     let updates_clone = stock_updates.clone();
//!
//!     // Monitor WebSocket for stock price updates
//!     page.on_websocket(move |ws| {
//!         let updates = updates_clone.clone();
//!         async move {
//!             if ws.url().contains("stock-feed") {
//!                 ws.on_framereceived(move |frame| {
//!                     let updates = updates.clone();
//!                     async move {
//!                         updates.lock().await.push(frame.payload().to_string());
//!                     }
//!                 }).await;
//!             }
//!         }
//!     }).await;
//!
//!     // Navigate to the stock ticker page
//!     page.goto("https://example.com/stocks").goto().await?;
//!
//!     // Verify the ticker display updates (auto-waits for DOM changes)
//!     expect(page.locator(".stock-ticker")).to_be_visible().await?;
//!     expect(page.locator(".stock-price")).to_contain_text("$").await?;
//!     
//!     // Verify connection indicator shows live status
//!     expect(page.locator(".connection-status"))
//!         .to_have_text("Live")
//!         .await?;
//!
//!     // Verify at least one price update was rendered
//!     expect(page.locator(".price-change")).not().to_be_empty().await?;
//!
//!     // Confirm WebSocket messages were received
//!     let updates = stock_updates.lock().await;
//!     assert!(!updates.is_empty(), "Should have received stock updates via WebSocket");
//!
//!     Ok(())
//! }
//! ```

// Allow dead code for websocket monitoring scaffolding (spec: network-events)

mod event_listener;
mod frame;

pub use frame::WebSocketFrame;

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use tokio::sync::{RwLock, broadcast};
use viewpoint_cdp::CdpConnection;

/// A WebSocket connection being monitored.
///
/// This struct represents an active WebSocket connection and provides
/// methods to register handlers for frame events.
#[derive(Clone)]
pub struct WebSocket {
    /// The request ID identifying this WebSocket.
    request_id: String,
    /// The WebSocket URL.
    url: String,
    /// Whether the WebSocket is closed.
    is_closed: Arc<AtomicBool>,
    /// Frame sent event broadcaster.
    frame_sent_tx: broadcast::Sender<WebSocketFrame>,
    /// Frame received event broadcaster.
    frame_received_tx: broadcast::Sender<WebSocketFrame>,
    /// Close event broadcaster.
    close_tx: broadcast::Sender<()>,
}

impl std::fmt::Debug for WebSocket {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WebSocket")
            .field("request_id", &self.request_id)
            .field("url", &self.url)
            .field("is_closed", &self.is_closed.load(Ordering::SeqCst))
            .finish()
    }
}

impl WebSocket {
    /// Create a new WebSocket instance.
    pub(crate) fn new(request_id: String, url: String) -> Self {
        let (frame_sent_tx, _) = broadcast::channel(256);
        let (frame_received_tx, _) = broadcast::channel(256);
        let (close_tx, _) = broadcast::channel(16);

        Self {
            request_id,
            url,
            is_closed: Arc::new(AtomicBool::new(false)),
            frame_sent_tx,
            frame_received_tx,
            close_tx,
        }
    }

    /// Get the WebSocket URL.
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Check if the WebSocket is closed.
    pub fn is_closed(&self) -> bool {
        self.is_closed.load(Ordering::SeqCst)
    }

    /// Get the request ID for this WebSocket.
    pub fn request_id(&self) -> &str {
        &self.request_id
    }

    /// Register a handler for frame sent events.
    ///
    /// The handler will be called whenever a frame is sent over this WebSocket.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::WebSocket;
    ///
    /// # async fn example(websocket: WebSocket) -> Result<(), viewpoint_core::CoreError> {
    /// websocket.on_framesent(|frame| async move {
    ///     println!("Sent: {:?}", frame.payload());
    /// }).await;
    /// # Ok(())
    /// # }
    pub async fn on_framesent<F, Fut>(&self, handler: F)
    where
        F: Fn(WebSocketFrame) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut rx = self.frame_sent_tx.subscribe();
        tokio::spawn(async move {
            while let Ok(frame) = rx.recv().await {
                handler(frame).await;
            }
        });
    }

    /// Register a handler for frame received events.
    ///
    /// The handler will be called whenever a frame is received on this WebSocket.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::WebSocket;
    ///
    /// # async fn example(websocket: WebSocket) -> Result<(), viewpoint_core::CoreError> {
    /// websocket.on_framereceived(|frame| async move {
    ///     println!("Received: {:?}", frame.payload());
    /// }).await;
    /// # Ok(())
    /// # }
    pub async fn on_framereceived<F, Fut>(&self, handler: F)
    where
        F: Fn(WebSocketFrame) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut rx = self.frame_received_tx.subscribe();
        tokio::spawn(async move {
            while let Ok(frame) = rx.recv().await {
                handler(frame).await;
            }
        });
    }

    /// Register a handler for WebSocket close events.
    ///
    /// The handler will be called when this WebSocket connection is closed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::WebSocket;
    ///
    /// # async fn example(websocket: WebSocket) -> Result<(), viewpoint_core::CoreError> {
    /// websocket.on_close(|| async {
    ///     println!("WebSocket closed");
    /// }).await;
    /// # Ok(())
    /// # }
    pub async fn on_close<F, Fut>(&self, handler: F)
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut rx = self.close_tx.subscribe();
        tokio::spawn(async move {
            if rx.recv().await.is_ok() {
                handler().await;
            }
        });
    }

    /// Emit a frame sent event (internal use).
    pub(crate) fn emit_frame_sent(&self, frame: WebSocketFrame) {
        let _ = self.frame_sent_tx.send(frame);
    }

    /// Emit a frame received event (internal use).
    pub(crate) fn emit_frame_received(&self, frame: WebSocketFrame) {
        let _ = self.frame_received_tx.send(frame);
    }

    /// Mark the WebSocket as closed and emit close event (internal use).
    pub(crate) fn mark_closed(&self) {
        self.is_closed.store(true, Ordering::SeqCst);
        let _ = self.close_tx.send(());
    }
}

/// Type alias for the WebSocket event handler function.
pub type WebSocketEventHandler =
    Box<dyn Fn(WebSocket) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

/// Manager for WebSocket events on a page.
pub struct WebSocketManager {
    /// CDP connection.
    connection: Arc<CdpConnection>,
    /// Session ID.
    session_id: String,
    /// Active WebSocket connections indexed by request ID.
    websockets: Arc<RwLock<HashMap<String, WebSocket>>>,
    /// WebSocket created event handler.
    handler: Arc<RwLock<Option<WebSocketEventHandler>>>,
    /// Whether the manager is listening for events.
    is_listening: AtomicBool,
}

impl WebSocketManager {
    /// Create a new WebSocket manager for a page.
    pub fn new(connection: Arc<CdpConnection>, session_id: String) -> Self {
        Self {
            connection,
            session_id,
            websockets: Arc::new(RwLock::new(HashMap::new())),
            handler: Arc::new(RwLock::new(None)),
            is_listening: AtomicBool::new(false),
        }
    }

    /// Set a handler for WebSocket created events.
    pub async fn set_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(WebSocket) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let boxed_handler: WebSocketEventHandler = Box::new(move |ws| Box::pin(handler(ws)));
        let mut h = self.handler.write().await;
        *h = Some(boxed_handler);

        // Start listening for events if not already
        self.start_listening().await;
    }

    /// Remove the WebSocket handler.
    pub async fn remove_handler(&self) {
        let mut h = self.handler.write().await;
        *h = None;
    }

    /// Start listening for WebSocket CDP events.
    async fn start_listening(&self) {
        if self.is_listening.swap(true, Ordering::SeqCst) {
            // Already listening
            return;
        }

        event_listener::spawn_event_listener(
            self.connection.clone(),
            self.session_id.clone(),
            self.websockets.clone(),
            self.handler.clone(),
        );
    }

    /// Get a WebSocket by request ID.
    pub async fn get(&self, request_id: &str) -> Option<WebSocket> {
        let sockets = self.websockets.read().await;
        sockets.get(request_id).cloned()
    }

    /// Get all active `WebSockets`.
    pub async fn all(&self) -> Vec<WebSocket> {
        let sockets = self.websockets.read().await;
        sockets.values().cloned().collect()
    }
}

#[cfg(test)]
mod tests;