playwright-rs 0.12.3

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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
use futures_util::{SinkExt, StreamExt};
use playwright_rs::protocol::Playwright;
use playwright_rs::server::channel_owner::ChannelOwner;
use playwright_rs::server::connection::Connection;
use playwright_rs::server::playwright_server::PlaywrightServer;
use playwright_rs::server::transport::PipeTransport;
use serde_json::json;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpListener;
use tokio::process::Command;
use tokio_tungstenite::accept_async;
use tokio_tungstenite::tungstenite::protocol::Message;

/// Test that we can establish a connection with real server and spawn message loops
///
/// This test verifies:
/// - Server launches successfully
/// - Connection can be created with server stdio
/// - Message loops can be spawned without errors
/// - Everything runs together and shuts down cleanly
#[tokio::test]
async fn test_connection_lifecycle_with_real_server() {
    crate::common::init_tracing();
    // Launch Playwright server
    let mut server = match PlaywrightServer::launch().await {
        Ok(s) => s,
        Err(e) => {
            tracing::warn!("Skipping test: Could not launch Playwright server: {}", e);
            tracing::warn!("This is expected if Node.js or Playwright driver is not available");
            return;
        }
    };

    // Take stdio handles from the process
    let stdin = server.process.stdin.take().expect("Failed to get stdin");
    let stdout = server.process.stdout.take().expect("Failed to get stdout");

    // Create transport and split into sender/receiver
    let (transport, message_rx) =
        playwright_rs::server::transport::PipeTransport::new(stdin, stdout);
    let (sender, receiver) = transport.into_parts();

    // Create connection
    let connection = Arc::new(Connection::new(sender, receiver, message_rx));

    // Spawn connection message loop
    let conn = Arc::clone(&connection);
    let connection_handle = tokio::spawn(async move {
        conn.run().await;
    });

    // Give the server time to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    // This test verifies the connection infrastructure works:
    // - Server launches successfully
    // - Connection and transport loops start without errors
    // - Everything compiles and runs together
    // - No panics or immediate crashes
    //
    // Full protocol initialization testing is done in:
    // - tests/initialization_integration.rs (complete initialization flow)
    // - tests/playwright_launch.rs (high-level Playwright::launch() API)

    // Clean up
    // Abort the connection loop (which will also stop reading from transport)
    connection_handle.abort();

    // Shutdown server
    server.shutdown().await.ok();
}

/// Test that connection detects server crash when sending
///
/// This test verifies that when the server crashes/exits:
/// - Attempting to send a message fails with appropriate error (broken pipe)
/// - The error is propagated correctly through the Connection layer
///
/// Note: The connection read loop will remain blocked on `read_exact()` until
/// the stdout pipe is fully closed by the OS, which can take time. This is
/// expected behavior - the important thing is that send operations fail fast.
#[tokio::test]
async fn test_connection_detects_server_crash_on_send() {
    crate::common::init_tracing();
    let mut server = match PlaywrightServer::launch().await {
        Ok(s) => s,
        Err(e) => {
            tracing::warn!("Skipping test: Could not launch Playwright server: {}", e);
            return;
        }
    };

    let stdin = server.process.stdin.take().expect("Failed to get stdin");
    let stdout = server.process.stdout.take().expect("Failed to get stdout");

    let (transport, message_rx) =
        playwright_rs::server::transport::PipeTransport::new(stdin, stdout);
    let (sender, receiver) = transport.into_parts();

    let connection = Arc::new(Connection::new(sender, receiver, message_rx));

    // Spawn connection loop
    let conn = Arc::clone(&connection);
    let _connection_handle = tokio::spawn(async move {
        conn.run().await;
    });

    // Give connection time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Kill the server
    server.kill().await.expect("Failed to kill server");

    // Give it a moment for the pipe to close
    tokio::time::sleep(Duration::from_millis(30)).await;

    // Try to send a message - this will detect the broken pipe immediately
    let send_result = connection
        .send_message(
            "test@guid".to_string(),
            "testMethod".to_string(),
            serde_json::json!({}),
        )
        .await;

    // Should fail with broken pipe error
    assert!(
        send_result.is_err(),
        "Expected error when sending to dead server"
    );

    // Verify it's a transport error (broken pipe)
    match send_result.unwrap_err() {
        playwright_rs::Error::TransportError(msg) => {
            assert!(
                msg.contains("Broken pipe") || msg.contains("Failed to write"),
                "Expected broken pipe error, got: {}",
                msg
            );
        }
        e => panic!("Expected TransportError, got: {:?}", e),
    }

    // Note: We don't wait for the connection loop to exit because the transport
    // read loop is blocked on read_exact() and won't exit until the OS fully
    // closes the stdout pipe. This is tested separately in the transport layer.
}

/// Test concurrent requests
///
/// This test will verify that multiple concurrent requests can be sent
/// and responses are correctly correlated, even when they arrive out of order.
///
/// Test concurrent requests to different protocol objects
///
/// This test verifies that concurrent requests to different objects
/// (Browser, Context, Page) are properly correlated when responses arrive.
#[tokio::test]
async fn test_concurrent_requests_with_server() {
    crate::common::init_tracing();
    let playwright = Playwright::launch()
        .await
        .expect("Failed to launch Playwright");

    let chromium = playwright.chromium();
    let browser = chromium.launch().await.expect("Failed to launch browser");

    // Create two contexts concurrently
    let context1_fut = browser.new_context();
    let context2_fut = browser.new_context();
    let (context1, context2) = tokio::join!(context1_fut, context2_fut);

    let context1 = context1.expect("Failed to create context 1");
    let context2 = context2.expect("Failed to create context 2");

    // Create pages concurrently across different contexts
    let page1_fut = context1.new_page();
    let page2_fut = context1.new_page();
    let page3_fut = context2.new_page();
    let (page1, page2, page3) = tokio::join!(page1_fut, page2_fut, page3_fut);

    let page1 = page1.expect("Failed to create page 1");
    let page2 = page2.expect("Failed to create page 2");
    let page3 = page3.expect("Failed to create page 3");

    // Verify all pages are at about:blank
    assert_eq!(page1.url(), "about:blank");
    assert_eq!(page2.url(), "about:blank");
    assert_eq!(page3.url(), "about:blank");

    // Close everything concurrently
    let page1_close = page1.close();
    let page2_close = page2.close();
    let page3_close = page3.close();
    let context1_close = context1.close();
    let context2_close = context2.close();

    let (r1, r2, r3, r4, r5) = tokio::join!(
        page1_close,
        page2_close,
        page3_close,
        context1_close,
        context2_close
    );

    // Verify all closes succeeded
    r1.expect("Failed to close page 1");
    r2.expect("Failed to close page 2");
    r3.expect("Failed to close page 3");
    r4.expect("Failed to close context 1");
    r5.expect("Failed to close context 2");

    browser.close().await.expect("Failed to close browser");
}

/// Test error handling with invalid requests
///
/// This test verifies that protocol errors from the server are properly
/// converted to Rust errors and propagated correctly.
#[tokio::test]
async fn test_error_response_from_server() {
    crate::common::init_tracing();
    let playwright = Playwright::launch()
        .await
        .expect("Failed to launch Playwright");

    let chromium = playwright.chromium();
    let browser = chromium.launch().await.expect("Failed to launch browser");

    // Close the browser
    browser.close().await.expect("Failed to close browser");

    // Try to close again - should result in an error from the server
    let result = browser.close().await;

    // The server should return an error for trying to close an already-closed browser
    assert!(
        result.is_err(),
        "Expected error when closing already-closed browser"
    );

    // Verify the error message contains relevant information
    // Test scenarios to implement:
    // - Call browser.close() twice (should error on second call)
    // - Invalid GUIDs
    // - Invalid method parameters
    //
    // Note: This is deferred for time, not technical reasons
}

/// Test that connect_over_cdp fails for Firefox (Chromium-only)
#[tokio::test]
async fn test_connect_over_cdp_chromium_only() {
    crate::common::init_tracing();

    let playwright = match Playwright::launch().await {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!("Skipping test: Failed to launch Playwright: {}", e);
            return;
        }
    };

    // Firefox should fail
    let result = playwright
        .firefox()
        .connect_over_cdp("http://localhost:9222", None)
        .await;
    assert!(
        result.is_err(),
        "Firefox should not support connect_over_cdp"
    );
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("Chromium"),
        "Error should mention Chromium: {}",
        err
    );

    // WebKit should fail
    let result = playwright
        .webkit()
        .connect_over_cdp("http://localhost:9222", None)
        .await;
    assert!(
        result.is_err(),
        "WebKit should not support connect_over_cdp"
    );

    playwright.shutdown().await.ok();
}

/// Launch Chrome with --remote-debugging-port and return the CDP endpoint URL.
///
/// Uses Playwright Node.js to find the Chrome binary and launch it with
/// remote debugging enabled, then discovers the CDP endpoint via /json/version.
async fn start_chrome_with_cdp(
    package_path: &std::path::Path,
) -> Option<(tokio::process::Child, String)> {
    // Node.js script that:
    // 1. Gets Chrome executable path from Playwright
    // 2. Spawns Chrome with --remote-debugging-port=0
    // 3. Reads the DevTools URL from stderr
    // 4. Outputs the HTTP endpoint to stdout
    let script = format!(
        r#"
const {{ chromium }} = require('{}');
const {{ spawn }} = require('child_process');
const http = require('http');

const execPath = chromium.executablePath();

const child = spawn(execPath, [
    '--headless',
    '--remote-debugging-port=0',
    '--no-sandbox',
    '--disable-gpu',
    '--use-mock-keychain',
    '--no-first-run'
], {{ stdio: ['pipe', 'pipe', 'pipe'] }});

// Chrome outputs the DevTools URL to stderr
let stderr = '';
child.stderr.on('data', (data) => {{
    stderr += data.toString();
    // Look for the DevTools listening message
    const match = stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/);
    if (match) {{
        // Extract port from ws://127.0.0.1:PORT/devtools/browser/...
        const portMatch = match[1].match(/:(\d+)\//);
        if (portMatch) {{
            console.log('http://127.0.0.1:' + portMatch[1]);
        }}
    }}
}});

// Keep running until stdin closes
process.stdin.resume();
process.stdin.on('close', () => {{
    child.kill();
    process.exit(0);
}});

// Also kill on timeout (safety)
setTimeout(() => {{
    child.kill();
    process.exit(1);
}}, 25000);
"#,
        package_path.display()
    );

    let mut child = Command::new("node")
        .arg("-e")
        .arg(&script)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .ok()?;

    let stdout = child.stdout.take()?;
    let mut reader = tokio::io::BufReader::new(stdout).lines();

    // Wait for the CDP endpoint with timeout
    let endpoint = tokio::time::timeout(Duration::from_secs(15), async {
        while let Ok(Some(line)) = reader.next_line().await {
            if line.starts_with("http://") || line.starts_with("ws://") {
                return Some(line);
            }
        }
        None
    })
    .await
    .ok()??;

    Some((child, endpoint))
}

/// Test connecting to a real Chrome via CDP
#[tokio::test]
async fn test_connect_over_cdp_real_chrome() {
    crate::common::init_tracing();

    // Find the Playwright package path
    let drivers_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("drivers");

    let package_path = std::fs::read_dir(&drivers_dir)
        .ok()
        .and_then(|mut entries| entries.next())
        .and_then(|e| e.ok())
        .map(|e| e.path().join("package"));

    let package_path = match package_path {
        Some(p) if p.exists() => p,
        _ => {
            tracing::warn!("Skipping test: Playwright driver not found");
            return;
        }
    };

    // Start Chrome with CDP
    let (mut chrome_process, cdp_endpoint) = match start_chrome_with_cdp(&package_path).await {
        Some(result) => result,
        None => {
            tracing::warn!("Skipping test: Failed to start Chrome with CDP");
            return;
        }
    };

    tracing::info!("Chrome CDP endpoint: {}", cdp_endpoint);

    // Launch local Playwright
    let playwright = match Playwright::launch().await {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!("Skipping test: Failed to launch Playwright: {}", e);
            let _ = chrome_process.kill().await;
            return;
        }
    };

    // Connect over CDP
    let browser = match playwright
        .chromium()
        .connect_over_cdp(&cdp_endpoint, None)
        .await
    {
        Ok(b) => b,
        Err(e) => {
            tracing::error!("Failed to connect over CDP: {}", e);
            let _ = playwright.shutdown().await;
            let _ = chrome_process.kill().await;
            panic!("connect_over_cdp failed: {:?}", e);
        }
    };

    tracing::info!("Connected via CDP! Browser version: {}", browser.version());

    // Verify browser works
    assert!(browser.is_connected());
    assert!(!browser.version().is_empty());

    // Create a page and navigate
    let page = browser.new_page().await.expect("Failed to create page");
    page.goto("data:text/html,<h1>CDP Connection Works!</h1>", None)
        .await
        .expect("Failed to navigate");

    let heading = page.locator("h1").await;
    let text = heading.text_content().await.expect("Failed to get text");
    assert_eq!(text, Some("CDP Connection Works!".to_string()));

    tracing::info!("CDP connection test passed!");

    // Cleanup
    browser.close().await.ok();
    playwright.shutdown().await.ok();
    let _ = chrome_process.kill().await;
}

#[tokio::test]
async fn test_browser_type_connect() {
    eprintln!("Test starting");

    // 1. Setup Remote Mock Server (TCP)
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    eprintln!("Remote Mock server bound");
    let addr = listener.local_addr().unwrap();
    let url = format!("ws://127.0.0.1:{}", addr.port());

    // Spawn Remote Mock Server logic
    tokio::spawn(async move {
        // Accept incoming connection
        let (stream, _) = listener.accept().await.unwrap();
        let mut ws_stream = accept_async(stream).await.unwrap();

        let browser_guid = "browser@remote"; // Remote browser GUID

        // Send objects for Remote Connection
        // Order: BrowserTypes (Root) -> Browser (Child) -> Playwright (Root, referencing others)

        let types = vec!["chromium", "firefox", "webkit"];
        for t in types {
            let create_type = json!({
                "guid": "", // Root
                "method": "__create__",
                "params": {
                    "type": "BrowserType",
                    "guid": format!("browserType@{}", t),
                    "initializer": {
                        "name": t,
                        "executablePath": "/bin/browser"
                    }
                }
            });
            ws_stream
                .send(Message::Text(create_type.to_string().into()))
                .await
                .unwrap();
        }

        let create_browser = json!({
            "guid": "browserType@chromium",
            "method": "__create__",
            "params": {
                "type": "Browser",
                "guid": browser_guid,
                "initializer": {
                    "name": "chromium",
                    "executablePath": "/bin/chromium",
                    "version": "1.0"
                }
            }
        });
        ws_stream
            .send(Message::Text(create_browser.to_string().into()))
            .await
            .unwrap();

        let create_playwright = json!({
            "guid": "", // Root
            "method": "__create__",
            "params": {
                "type": "Playwright",
                "guid": "playwright",
                "initializer": {
                    "chromium": { "guid": "browserType@chromium" },
                    "firefox": { "guid": "browserType@firefox" },
                    "webkit": { "guid": "browserType@webkit" },
                    "preLaunchedBrowser": { "guid": browser_guid }
                }
            }
        });
        ws_stream
            .send(Message::Text(create_playwright.to_string().into()))
            .await
            .unwrap();

        // Handle incoming messages (especially the initialize request)
        while let Some(msg) = ws_stream.next().await {
            match msg {
                Ok(Message::Text(text)) => {
                    if let Ok(request) = serde_json::from_str::<serde_json::Value>(&text) {
                        // Check if this is an initialize request
                        if request.get("method").and_then(|m| m.as_str()) == Some("initialize") {
                            let id = request.get("id").and_then(|i| i.as_u64()).unwrap_or(0);
                            let response = json!({
                                "id": id,
                                "result": {
                                    "playwright": {
                                        "guid": "playwright"
                                    }
                                }
                            });
                            ws_stream
                                .send(Message::Text(response.to_string().into()))
                                .await
                                .unwrap();
                        }
                    }
                }
                Ok(Message::Close(_)) => break,
                Ok(_) => {}
                Err(_) => break,
            }
        }
    });

    // 2. Setup Local Mock Connection (Duplex) to simulate local driver
    let (client_conn, mut server_conn) = tokio::io::duplex(65536);

    // Spawn Local Mock Server
    tokio::spawn(async move {
        // Helper to send framed message
        async fn send_framed(stream: &mut tokio::io::DuplexStream, msg: serde_json::Value) {
            let bytes = serde_json::to_vec(&msg).unwrap();
            let len = bytes.len() as u32;
            stream.write_all(&len.to_le_bytes()).await.unwrap();
            stream.write_all(&bytes).await.unwrap();
        }

        // Send BrowserTypes (Roots)
        let types = vec!["chromium", "firefox", "webkit"];
        for t in types {
            let create_type = json!({
                "guid": "",
                "method": "__create__",
                "params": {
                    "type": "BrowserType",
                    "guid": format!("browserType@{}", t),
                    "initializer": {
                        "name": t,
                        "executablePath": "/bin/browser"
                    }
                }
            });
            send_framed(&mut server_conn, create_type).await;
        }

        // Send Playwright (Root). No preLaunchedBrowser locally usually.
        let create_playwright = json!({
            "guid": "",
            "method": "__create__",
            "params": {
                "type": "Playwright",
                "guid": "playwright",
                "initializer": {
                    "chromium": { "guid": "browserType@chromium" },
                    "firefox": { "guid": "browserType@firefox" },
                    "webkit": { "guid": "browserType@webkit" }
                }
            }
        });
        send_framed(&mut server_conn, create_playwright).await;

        // Read "initialize" request
        let mut len_buf = [0u8; 4];
        server_conn.read_exact(&mut len_buf).await.unwrap();
        let len = u32::from_le_bytes(len_buf) as usize;
        let mut msg_buf = vec![0u8; len];
        server_conn.read_exact(&mut msg_buf).await.unwrap();

        let msg: serde_json::Value = serde_json::from_slice(&msg_buf).unwrap();
        if let Some(id) = msg["id"].as_i64() {
            let response = json!({
                "id": id,
                "result": {
                    "playwright": {
                        "guid": "playwright" // Must match the GUID sent in __create__
                    }
                }
            });
            send_framed(&mut server_conn, response).await;
        }

        // Consume further input (keep connection open)
        let mut buf = vec![0u8; 1024];
        loop {
            if server_conn.read(&mut buf).await.unwrap() == 0 {
                break;
            }
        }
    });

    // 3. Initialize Client (Local)
    let (client_r, client_w) = tokio::io::split(client_conn);
    let (transport, message_rx) = PipeTransport::new(client_w, client_r);
    let (sender, receiver) = transport.into_parts();

    let connection = Arc::new(Connection::new(sender, receiver, message_rx));
    let conn_clone = connection.clone();
    tokio::spawn(async move {
        conn_clone.run().await;
    });

    // Give the connection a moment to start
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;

    eprintln!("Initializing local playwright");
    let playwright_obj = connection
        .initialize_playwright()
        .await
        .expect("Local init failed");
    let playwright = playwright_obj
        .as_any()
        .downcast_ref::<Playwright>()
        .unwrap();
    eprintln!("Local playwright initialized");

    // 4. Connect to Remote
    eprintln!("Connecting to remote: {}", url);
    let browser = playwright
        .chromium()
        .connect(&url, None)
        .await
        .expect("Connect failed");
    eprintln!("Connected!");

    assert_eq!(browser.guid(), "browser@remote");
}

// ============================================================================
// Remote browser connection via WebSocket
//
// Verifies `BrowserType::connect()` against a real Playwright browser server
// (`chromium.launchServer()`) — critical for CI/CD container environments.
// ============================================================================

/// Start a Playwright browser server using launchServer() and return the ws endpoint
///
/// This creates a Node.js script that:
/// 1. Requires Playwright
/// 2. Calls chromium.launchServer()
/// 3. Outputs the WebSocket endpoint to stdout
/// 4. Waits for stdin to close before shutting down
async fn start_browser_server(
    package_path: &std::path::Path,
) -> Option<(tokio::process::Child, String)> {
    // Node.js script to launch browser server
    let script = format!(
        r#"
const {{ chromium }} = require('{}');

(async () => {{
    const server = await chromium.launchServer({{ headless: true }});
    console.log(server.wsEndpoint());

    // Keep running until stdin closes (parent process exits)
    process.stdin.resume();
    process.stdin.on('close', async () => {{
        await server.close();
        process.exit(0);
    }});
}})().catch(err => {{
    console.error(err);
    process.exit(1);
}});
"#,
        package_path.display()
    );

    let mut child = Command::new("node")
        .arg("-e")
        .arg(&script)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .ok()?;

    let stdout = child.stdout.take()?;
    let mut reader = BufReader::new(stdout).lines();

    // Wait for the WebSocket endpoint with timeout
    let ws_endpoint = tokio::time::timeout(Duration::from_secs(30), async {
        if let Ok(Some(line)) = reader.next_line().await
            && line.starts_with("ws://")
        {
            return Some(line);
        }
        None
    })
    .await
    .ok()??;

    Some((child, ws_endpoint))
}

/// Test connecting to a real Playwright browser server
///
/// This test:
/// 1. Launches a Playwright browser server via launchServer()
/// 2. Launches a local Playwright instance
/// 3. Connects to the browser server via WebSocket
/// 4. Performs browser operations on the remote browser
/// 5. Verifies everything works end-to-end
#[tokio::test]
async fn test_connect_to_real_server() {
    crate::common::init_tracing();

    // Find the Playwright package path
    let drivers_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("drivers");

    let package_path = std::fs::read_dir(&drivers_dir)
        .ok()
        .and_then(|mut entries| entries.next())
        .and_then(|e| e.ok())
        .map(|e| e.path().join("package"));

    let package_path = match package_path {
        Some(p) if p.exists() => p,
        _ => {
            tracing::warn!(
                "Skipping test: Playwright driver not found in {:?}",
                drivers_dir
            );
            return;
        }
    };

    tracing::info!("Starting browser server");

    // Start the browser server
    let (mut server_process, ws_endpoint) = match start_browser_server(&package_path).await {
        Some(result) => result,
        None => {
            tracing::warn!("Skipping test: Failed to start browser server");
            return;
        }
    };

    tracing::info!("Browser server ready at {}", ws_endpoint);

    // Launch local Playwright to get access to BrowserType
    let playwright = match Playwright::launch().await {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!("Skipping test: Failed to launch local Playwright: {}", e);
            let _ = server_process.kill().await;
            return;
        }
    };

    tracing::info!("Connecting to remote server at {}", ws_endpoint);

    // Connect to the remote server
    let browser = match playwright.chromium().connect(&ws_endpoint, None).await {
        Ok(b) => b,
        Err(e) => {
            tracing::error!("Failed to connect to remote server: {}", e);
            let _ = playwright.shutdown().await;
            let _ = server_process.kill().await;
            panic!("Connect failed: {:?}", e);
        }
    };

    tracing::info!("Connected! Browser GUID: {}", browser.guid());

    // Verify browser is connected
    assert!(browser.is_connected());
    assert!(!browser.version().is_empty());

    tracing::info!("Browser version: {}", browser.version());

    // Create a context and page on the remote browser
    let context = browser
        .new_context()
        .await
        .expect("Failed to create context");
    let page = context.new_page().await.expect("Failed to create page");

    // Navigate to a simple page
    page.goto("data:text/html,<h1>Hello from Remote!</h1>", None)
        .await
        .expect("Failed to navigate");

    // Verify the page loaded by checking content
    let locator = page.locator("h1").await;
    let content = locator.text_content().await.expect("Failed to get text");
    assert_eq!(
        content,
        Some("Hello from Remote!".to_string()),
        "Page content should match"
    );

    tracing::info!("✓ Remote connection test passed!");

    // Cleanup
    page.close().await.ok();
    context.close().await.ok();
    browser.close().await.ok();
    playwright.shutdown().await.ok();

    // Kill the server process (closing stdin triggers shutdown)
    let _ = server_process.kill().await;
}

/// Test connection timeout when server is not available
#[tokio::test]
async fn test_connect_timeout_when_server_unavailable() {
    crate::common::init_tracing();

    // Launch local Playwright
    let playwright = match Playwright::launch().await {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!("Skipping test: Failed to launch Playwright: {}", e);
            return;
        }
    };

    // Try to connect to a port with no server (should fail quickly)
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    drop(listener); // Release the port
    let ws_endpoint = format!("ws://127.0.0.1:{}", port);

    tracing::info!(
        "Attempting to connect to unavailable server at {}",
        ws_endpoint
    );

    let start = std::time::Instant::now();

    // Use a short timeout for the test
    let options = playwright_rs::api::ConnectOptions::new().timeout(2000.0); // 2 seconds

    let result = playwright
        .chromium()
        .connect(&ws_endpoint, Some(options))
        .await;

    let elapsed = start.elapsed();

    // Should fail (connection refused or timeout)
    assert!(result.is_err(), "Expected connection to fail");

    // Should fail reasonably quickly (within timeout + some margin)
    assert!(
        elapsed < Duration::from_secs(5),
        "Connection took too long to fail: {:?}",
        elapsed
    );

    tracing::info!(
        "✓ Connection failed as expected in {:?}: {:?}",
        elapsed,
        result.unwrap_err()
    );

    playwright.shutdown().await.ok();
}

/// Test connecting with custom headers
#[tokio::test]
async fn test_connect_with_custom_headers() {
    crate::common::init_tracing();

    // This test verifies that custom headers are passed through.
    // In a real scenario, this would be used for authentication tokens.
    //
    // For now, we just verify that passing headers doesn't break the connection.
    // A full test would require a server that validates headers.

    let drivers_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("drivers");

    let package_path = std::fs::read_dir(&drivers_dir)
        .ok()
        .and_then(|mut entries| entries.next())
        .and_then(|e| e.ok())
        .map(|e| e.path().join("package"));

    let package_path = match package_path {
        Some(p) if p.exists() => p,
        _ => {
            tracing::warn!("Skipping test: Playwright driver not found");
            return;
        }
    };

    // Start browser server
    let (mut server_process, ws_endpoint) = match start_browser_server(&package_path).await {
        Some(result) => result,
        None => {
            tracing::warn!("Skipping test: Failed to start browser server");
            return;
        }
    };

    let playwright = match Playwright::launch().await {
        Ok(p) => p,
        Err(e) => {
            let _ = server_process.kill().await;
            tracing::warn!("Skipping test: {}", e);
            return;
        }
    };

    // Connect with custom headers
    let mut headers = std::collections::HashMap::new();
    headers.insert("X-Custom-Auth".to_string(), "test-token".to_string());
    headers.insert("X-Request-Id".to_string(), "12345".to_string());

    let options = playwright_rs::api::ConnectOptions::new()
        .headers(headers)
        .timeout(10000.0);

    let browser = match playwright
        .chromium()
        .connect(&ws_endpoint, Some(options))
        .await
    {
        Ok(b) => b,
        Err(e) => {
            tracing::error!("Failed to connect: {}", e);
            let _ = playwright.shutdown().await;
            let _ = server_process.kill().await;
            panic!("Connect with headers failed: {:?}", e);
        }
    };

    assert!(browser.is_connected());
    tracing::info!("✓ Connected with custom headers successfully");

    // Cleanup
    browser.close().await.ok();
    playwright.shutdown().await.ok();
    let _ = server_process.kill().await;
}