chromey 2.46.22

Concurrent chrome devtools protocol automation library for Rust
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
//! Integration tests for all `wait_for_*` methods on `Page`.
//!
//! These tests navigate to `https://example.com` (a stable, lightweight page)
//! and exercise every wait-for variant exposed by chromey:
//!
//!   1. `wait_for_navigation` / `wait_for_navigation_response`
//!   2. `wait_for_dom_content_loaded` / `wait_for_dom_content_loaded_response`
//!   3. `wait_for_network_idle`
//!   4. `wait_for_network_almost_idle`
//!   5. `wait_for_network_idle_with_timeout`
//!   6. `wait_for_network_almost_idle_with_timeout`
//!   7. `find_element` (selector-based wait)
//!
//! Run with:
//!   cargo test --test wait_for

use chromiumoxide::browser::{Browser, BrowserConfig, HeadlessMode};
use futures_util::StreamExt;
use std::path::PathBuf;
use tokio::time::{timeout, Duration};

const TARGET: &str = "https://example.com";

fn try_browser_config() -> Option<BrowserConfig> {
    BrowserConfig::builder().build().ok()
}

fn temp_profile_dir(test_name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "chromey-waitfor-{test_name}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock")
            .as_nanos()
    ));
    std::fs::create_dir_all(&dir).expect("create temp profile dir");
    dir
}

fn headless_config(test_name: &str) -> BrowserConfig {
    let profile_dir = temp_profile_dir(test_name);
    BrowserConfig::builder()
        .user_data_dir(&profile_dir)
        .arg("--no-first-run")
        .arg("--no-default-browser-check")
        .arg("--disable-extensions")
        .headless_mode(HeadlessMode::True)
        .launch_timeout(Duration::from_secs(30))
        .build()
        .expect("headless browser config")
}

async fn launch(config: BrowserConfig) -> Browser {
    let (browser, mut handler) = Browser::launch(config).await.expect("launch browser");
    let _handle = tokio::spawn(async move { while let Some(_event) = handler.next().await {} });
    browser
}

// ---------------------------------------------------------------------------
// 1. wait_for_navigation — waits for the `load` lifecycle event
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_navigation_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfn-goto")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(Duration::from_secs(30), page.wait_for_navigation())
        .await
        .expect("wait_for_navigation should not time out")
        .expect("wait_for_navigation should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain', got {} bytes",
        content.len()
    );
}

// ---------------------------------------------------------------------------
// 2. wait_for_navigation_response — returns the HTTP response
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_navigation_response_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfn-response")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let response = timeout(Duration::from_secs(30), page.wait_for_navigation_response())
        .await
        .expect("wait_for_navigation_response should not time out")
        .expect("wait_for_navigation_response should succeed");

    eprintln!("navigation response received: {:?}", response);
}

// ---------------------------------------------------------------------------
// 3. wait_for_dom_content_loaded — fires before `load`, no subresource wait
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_dom_content_loaded_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfdcl")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(Duration::from_secs(30), page.wait_for_dom_content_loaded())
        .await
        .expect("wait_for_dom_content_loaded should not time out")
        .expect("wait_for_dom_content_loaded should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain' after DOMContentLoaded"
    );
}

#[tokio::test]
async fn wait_for_dom_content_loaded_response_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfdcl-response")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let _response = timeout(
        Duration::from_secs(30),
        page.wait_for_dom_content_loaded_response(),
    )
    .await
    .expect("wait_for_dom_content_loaded_response should not time out")
    .expect("wait_for_dom_content_loaded_response should succeed");
}

/// DOMContentLoaded should resolve before or at the same time as load.
#[tokio::test]
async fn dom_content_loaded_resolves_before_load() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("dcl-before-load")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

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

    let dcl_time = {
        let _ = timeout(Duration::from_secs(30), page.wait_for_dom_content_loaded())
            .await
            .expect("dcl should not time out")
            .expect("dcl should succeed");
        start.elapsed()
    };

    let load_time = {
        let _ = timeout(Duration::from_secs(30), page.wait_for_navigation())
            .await
            .expect("nav should not time out")
            .expect("nav should succeed");
        start.elapsed()
    };

    eprintln!(
        "DOMContentLoaded: {dcl_time:?}, load: {load_time:?} (dcl <= load: {})",
        dcl_time <= load_time
    );

    assert!(
        dcl_time <= load_time,
        "DOMContentLoaded ({dcl_time:?}) should resolve before or at load ({load_time:?})"
    );
}

// ---------------------------------------------------------------------------
// 4. wait_for_network_idle — 500ms of zero open connections
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_network_idle_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfni")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(Duration::from_secs(30), page.wait_for_network_idle())
        .await
        .expect("wait_for_network_idle should not time out")
        .expect("wait_for_network_idle should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain' after network idle"
    );
}

// ---------------------------------------------------------------------------
// 5. wait_for_network_almost_idle
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_network_almost_idle_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfnai")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(Duration::from_secs(30), page.wait_for_network_almost_idle())
        .await
        .expect("wait_for_network_almost_idle should not time out")
        .expect("wait_for_network_almost_idle should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain' after network almost idle"
    );
}

// ---------------------------------------------------------------------------
// 6. wait_for_network_idle_with_timeout — bounded idle wait
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_network_idle_with_timeout_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfni-timeout")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(
        Duration::from_secs(30),
        page.wait_for_network_idle_with_timeout(Duration::from_secs(15)),
    )
    .await
    .expect("outer timeout should not fire")
    .expect("wait_for_network_idle_with_timeout should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain' after idle-with-timeout"
    );
}

/// A very short timeout should gracefully return Ok (timeout elapsed).
#[tokio::test]
async fn wait_for_network_idle_with_tiny_timeout_does_not_error() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfni-tiny-timeout")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = page
        .wait_for_network_idle_with_timeout(Duration::from_millis(1))
        .await;

    assert!(
        result.is_ok(),
        "wait_for_network_idle_with_timeout should return Ok even when timeout elapses"
    );
}

// ---------------------------------------------------------------------------
// 7. wait_for_network_almost_idle_with_timeout
// ---------------------------------------------------------------------------

#[tokio::test]
async fn wait_for_network_almost_idle_with_timeout_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfnai-timeout")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = timeout(
        Duration::from_secs(30),
        page.wait_for_network_almost_idle_with_timeout(Duration::from_secs(15)),
    )
    .await
    .expect("outer timeout should not fire")
    .expect("wait_for_network_almost_idle_with_timeout should succeed");

    let content = timeout(Duration::from_secs(15), result.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "page should contain 'Example Domain' after almost-idle-with-timeout"
    );
}

/// Tiny timeout is graceful for almost-idle variant too.
#[tokio::test]
async fn wait_for_network_almost_idle_with_tiny_timeout_does_not_error() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("wfnai-tiny-timeout")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    let result = page
        .wait_for_network_almost_idle_with_timeout(Duration::from_millis(1))
        .await;

    assert!(
        result.is_ok(),
        "wait_for_network_almost_idle_with_timeout should return Ok even when timeout elapses"
    );
}

// ---------------------------------------------------------------------------
// 8. find_element — selector-based wait
// ---------------------------------------------------------------------------

#[tokio::test]
async fn find_element_after_goto() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("find-el")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    timeout(Duration::from_secs(30), page.wait_for_navigation())
        .await
        .expect("wait_for_navigation should not time out")
        .expect("wait_for_navigation should succeed");

    let element = timeout(Duration::from_secs(15), page.find_element("h1"))
        .await
        .expect("find_element should not time out")
        .expect("find_element should resolve for <h1>");

    let text = timeout(Duration::from_secs(10), element.inner_text())
        .await
        .expect("inner_text should not time out")
        .expect("inner_text should succeed");

    assert!(
        text.as_deref()
            .is_some_and(|t| t.contains("Example Domain")),
        "h1 inner text should contain 'Example Domain', got: {text:?}"
    );
}

// ---------------------------------------------------------------------------
// 9. Two-phase concurrent page_wait
// ---------------------------------------------------------------------------

/// Phase 1 (concurrent): network waits run together.
/// Phase 2 (concurrent): selector + delay run together.
/// Wall time = max(network waits) + max(selector, delay).
#[tokio::test]
async fn two_phase_concurrent_page_wait() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("two-phase-page-wait")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

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

    // Phase 1: all network waits run concurrently.
    tokio::join!(
        async {
            let _ = page
                .wait_for_network_idle_with_timeout(Duration::from_secs(15))
                .await;
        },
        async {
            let _ = page
                .wait_for_network_almost_idle_with_timeout(Duration::from_secs(15))
                .await;
        },
    );

    let phase1 = start.elapsed();

    // Phase 2: selector + delay run concurrently (after network settles).
    tokio::join!(
        async {
            let _ = timeout(Duration::from_secs(15), page.find_element("body")).await;
        },
        async {
            tokio::time::sleep(Duration::from_millis(100)).await;
        },
    );

    let phase2 = start.elapsed() - phase1;

    let content = timeout(Duration::from_secs(15), page.content())
        .await
        .expect("content() should not time out")
        .expect("content() should succeed");

    assert!(
        content.contains("Example Domain"),
        "two-phase wait should yield page with 'Example Domain'"
    );
    assert!(
        content.contains("<h1>"),
        "two-phase wait should yield page with <h1> tag"
    );
    eprintln!(
        "two-phase page_wait: phase1={phase1:?} phase2={phase2:?} total={:?} ({} bytes)",
        start.elapsed(),
        content.len()
    );
}

// ---------------------------------------------------------------------------
// 10. Click + wait_for_navigation (interaction-triggered navigation)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn click_then_wait_for_navigation() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("click-nav")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    timeout(Duration::from_secs(30), page.wait_for_navigation())
        .await
        .expect("initial wait_for_navigation should not time out")
        .expect("initial wait_for_navigation should succeed");

    let link = timeout(Duration::from_secs(15), page.find_element("a"))
        .await
        .expect("find_element(a) should not time out")
        .expect("find_element(a) should resolve");

    timeout(Duration::from_secs(15), link.click())
        .await
        .expect("click should not time out")
        .expect("click should succeed");

    // The navigation may or may not succeed depending on the remote server,
    // but the wait_for_navigation call itself should not hang or panic.
    let nav_result = timeout(Duration::from_secs(30), page.wait_for_navigation()).await;

    match nav_result {
        Ok(Ok(_)) => {
            let url = page.url().await.expect("url()");
            eprintln!("navigated to: {url:?}");
        }
        Ok(Err(err)) => {
            eprintln!("navigation after click errored (acceptable): {err}");
        }
        Err(_) => {
            eprintln!("navigation after click timed out (acceptable for external site)");
        }
    }
}

// ---------------------------------------------------------------------------
// 11. Concurrent wait_for across multiple pages
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_wait_for_across_pages() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("concurrent-wait")).await;

    let mut pages = Vec::new();
    for _ in 0..3 {
        let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
            .await
            .expect("new_page should not time out")
            .expect("new_page should resolve");
        pages.push(page);
    }

    let futs: Vec<_> = pages
        .into_iter()
        .enumerate()
        .map(|(i, page)| {
            tokio::spawn(async move {
                timeout(Duration::from_secs(30), page.goto(TARGET))
                    .await
                    .unwrap_or_else(|_| panic!("page {i}: goto timed out"))
                    .unwrap_or_else(|e| panic!("page {i}: goto failed: {e}"));

                // Each page uses a different wait strategy
                match i % 3 {
                    0 => {
                        let _ = page
                            .wait_for_network_idle_with_timeout(Duration::from_secs(15))
                            .await;
                    }
                    1 => {
                        let _ = page
                            .wait_for_network_almost_idle_with_timeout(Duration::from_secs(15))
                            .await;
                    }
                    _ => {
                        let _ =
                            timeout(Duration::from_secs(15), page.wait_for_dom_content_loaded())
                                .await;
                    }
                }

                let content = timeout(Duration::from_secs(15), page.content())
                    .await
                    .unwrap_or_else(|_| panic!("page {i}: content() timed out"))
                    .unwrap_or_else(|e| panic!("page {i}: content() failed: {e}"));

                assert!(
                    content.contains("Example Domain"),
                    "page {i}: should contain 'Example Domain'"
                );
                eprintln!("page {i}: {} bytes", content.len());
            })
        })
        .collect();

    for fut in futs {
        fut.await.expect("task join");
    }
}

// ---------------------------------------------------------------------------
// 12. dom_content_loaded under concurrency — no deadlock
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_dom_content_loaded_does_not_deadlock() {
    if try_browser_config().is_none() {
        eprintln!("skipping: no Chrome/Chromium executable found");
        return;
    }

    let browser = launch(headless_config("concurrent-dcl")).await;

    let page = timeout(Duration::from_secs(30), browser.new_page("about:blank"))
        .await
        .expect("new_page should not time out")
        .expect("new_page should resolve");

    timeout(Duration::from_secs(30), page.goto(TARGET))
        .await
        .expect("goto should not time out")
        .expect("goto should succeed");

    // Fire many concurrent wait_for_dom_content_loaded calls.
    let futs: Vec<_> = (0..50)
        .map(|i| {
            let page = page.clone();
            tokio::spawn(async move {
                timeout(Duration::from_secs(30), page.wait_for_dom_content_loaded())
                    .await
                    .unwrap_or_else(|_| panic!("dcl({i}) timed out — possible deadlock"))
                    .unwrap_or_else(|err| panic!("dcl({i}) failed: {err}"));
            })
        })
        .collect();

    for (i, fut) in futs.into_iter().enumerate() {
        fut.await
            .unwrap_or_else(|err| panic!("task {i} panicked: {err}"));
    }
}