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
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
//! Per-page handle to a single CDP target session.
//!
//! [`Tab`] is the primary interaction surface in zendriver — most workflows
//! are some sequence of `goto`, `find().css(...).one()`, `evaluate`,
//! `screenshot`, and `wait_for_idle`. Each [`Tab`] owns its own
//! [`InputController`] (cursor + held-modifier state), its own per-tab
//! frame registry, and its own in-flight network tracker, so multiple tabs
//! in the same [`crate::Browser`] don't interfere with one another.
//!
//! ```no_run
//! # async fn ex() -> zendriver::Result<()> {
//! let browser = zendriver::Browser::builder().launch().await?;
//! let tab = browser.main_tab();
//! tab.goto("https://example.com").await?;
//! tab.wait_for_load().await?;
//! let title: String = tab.evaluate_main("document.title").await?;
//! assert_eq!(title, "Example Domain");
//! # Ok(()) }
//! ```
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use futures::StreamExt;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use tokio::time::timeout;
use tracing::trace;
use zendriver_transport::SessionHandle;
use crate::error::{Result, ZendriverError};
use crate::frame::Frame;
use crate::input::InputController;
use crate::isolated_world::IsolatedWorldCache;
use crate::screenshot::ScreenshotBuilder;
const DEFAULT_LOAD_TIMEOUT: Duration = Duration::from_secs(30);
/// Handle to a single CDP target session — one open page in Chrome.
///
/// `Tab` is `Clone` (cheap — wraps an `Arc`) and `Send + Sync`, so the same
/// handle can be passed across `tokio::spawn` boundaries freely. Dropping
/// the last clone tears down the per-Tab background tasks (network tracker,
/// frame lifecycle subscriber) but does NOT close the page in Chrome — call
/// [`Tab::close`] for an explicit teardown.
///
/// Obtain a `Tab` from [`crate::Browser::main_tab`], [`crate::Browser::new_tab`],
/// or [`crate::Browser::tabs`].
#[derive(Clone, Debug)]
pub struct Tab {
pub(crate) inner: Arc<TabInner>,
}
#[derive(Debug)]
pub(crate) struct TabInner {
pub(crate) session: SessionHandle,
pub(crate) isolated_world: tokio::sync::Mutex<IsolatedWorldCache>,
/// Weak ref to the owning `BrowserInner`. Used by [`Tab::cookies`] to
/// hand back a [`crate::CookieJar`] bound to the browser's root
/// connection (Chrome's cookie store is browser-scoped, so per-tab jars
/// would all dispatch the same way). Reserved for future P4 tasks
/// (tabs registry walks, storage). `Weak` breaks the Browser→Tab→Browser
/// cycle.
pub(crate) browser: std::sync::Weak<crate::browser::BrowserInner>,
/// Per-Tab input controller. Each tab owns its own cursor + held-modifier
/// state — distinct tabs in the same Browser have independent pointers.
/// `Element` actions clone this `Arc` to drive `mouse::*` / `keyboard::*`
/// dispatch helpers; the shared mutex inside `InputController` serializes
/// per-tab writes without crossing tab boundaries.
pub(crate) input: Arc<InputController>,
/// CDP `targetId` for the page target this tab wraps. Cached at Tab
/// construction time (from `Target.attachedToTarget`'s `target_info`)
/// so multi-tab orchestration (`Browser::new_tab` correlation,
/// `Tab::activate`, `Tab::close`'s `Target.closeTarget` upgrade) can
/// dispatch by `targetId` without re-querying `Target.getTargetInfo`
/// per call.
pub(crate) target_id: String,
/// Per-Tab in-flight network request tracker. Constructed in
/// [`Tab::new`] alongside a background task (spawned via
/// [`crate::network_idle::InFlightTracker::run`]) that subscribes to
/// `Network.*` events and maintains the set. Consulted by
/// [`Tab::wait_for_idle`] / [`Tab::wait_for_idle_with`] for Playwright
/// `networkidle` semantics.
pub(crate) network_tracker: Arc<crate::network_idle::InFlightTracker>,
/// Cancellation token for the background tracker task. Fires on
/// [`Drop`] so the spawned task exits cleanly when the last clone of
/// this Tab goes away. Cloned by the spawned task at construction
/// time; cancelling here propagates to the task's `tokio::select!`
/// loop within one event tick.
pub(crate) network_cancel: tokio_util::sync::CancellationToken,
/// Lazily-discovered main [`Frame`] for this tab. First call to
/// [`Tab::main_frame`] sends `Page.getFrameTree`, extracts the top-level
/// frame id/url/name, constructs a `Frame` (sharing this tab's
/// session — the main frame is always same-process), and stores it
/// here. Subsequent calls return the cached `Frame` clone without
/// another round-trip.
pub(crate) main_frame: tokio::sync::OnceCell<Frame>,
/// Per-Tab download coordinator. Lazily initialized on the first
/// [`Tab::expect_download`] call (gated `expect`) — the constructor
/// allocates a tempdir, dispatches `Browser.setDownloadBehavior` once,
/// and spawns a long-running `Page.downloadProgress` subscriber. Held
/// behind a [`tokio::sync::OnceCell`] so the wiring happens exactly
/// once per Tab; subsequent `expect_download` calls reuse the same
/// coordinator (and therefore the same tempdir + subscriber).
///
/// `Arc` because both the [`Tab`] (via this cell) and the spawned
/// progress subscriber task hold references to the same coordinator
/// state for the Tab's entire lifetime.
#[cfg(feature = "expect")]
pub(crate) download_setup:
tokio::sync::OnceCell<Arc<crate::expect::download::DownloadCoordinator>>,
/// Per-Tab frames registry keyed by CDP `frameId`. Populated by the
/// background subscriber spawned in [`Tab::new`] via
/// [`crate::frame::lifecycle::run`] which mutates the map in response
/// to `Page.frameAttached` / `Page.frameDetached` /
/// `Page.frameNavigated` events on this tab's session. Read by
/// [`Tab::frames`] / [`Tab::frame_by_url`] / [`Tab::frame_by_name`].
///
/// Same-origin sub-frames go in this map directly; out-of-process
/// iframes (OOPIFs) take the `Target.attachedToTarget` path wired in
/// T16 and land here only after that observer registers them.
pub(crate) frames: Arc<tokio::sync::RwLock<HashMap<String, Frame>>>,
/// Cancellation token for the frame lifecycle subscriber task. Mirror
/// of [`TabInner::network_cancel`]: fires on [`Drop`] so the spawned
/// task exits cleanly when the last clone of this Tab goes away. The
/// task selects on this token alongside the three `Page.frame*`
/// subscriber streams so cancellation unblocks the select even if no
/// events are arriving.
pub(crate) frame_lifecycle_cancel: tokio_util::sync::CancellationToken,
/// Oneshot receiver published by [`Tab::goto`] (subscribes to
/// `Page.frameStoppedLoading` BEFORE issuing `Page.navigate` so the
/// event can't race past) and consumed by [`Tab::wait_for_load`].
/// `None` when there is no pending navigation — `wait_for_load` falls
/// back to a fresh subscribe + `document.readyState` short-circuit.
pub(crate) pending_load: tokio::sync::Mutex<Option<tokio::sync::oneshot::Receiver<()>>>,
}
impl Drop for TabInner {
fn drop(&mut self) {
// Signal the spawned `InFlightTracker::run` task to exit. The task
// selects on this token alongside the four `Network.*` subscriber
// streams; cancellation unblocks the select even if no events are
// arriving. Without this the task would leak per Tab on shutdown.
self.network_cancel.cancel();
// Signal the spawned `frame::lifecycle::run` task to exit. Same
// posture as `network_cancel` above — the task selects on this
// token alongside the three `Page.frame*` subscriber streams.
self.frame_lifecycle_cancel.cancel();
}
}
impl Tab {
pub(crate) fn new(
session: SessionHandle,
browser: std::sync::Weak<crate::browser::BrowserInner>,
input: Arc<InputController>,
target_id: String,
) -> Self {
// Build the per-Tab network tracker + spawn its background subscriber
// task. The task calls `Network.enable` once, then maintains the
// in-flight set in response to `Network.requestWillBeSent` /
// `responseReceived` / `loadingFailed` / `loadingFinished` events
// arriving on this tab's session. `wait_for_idle` reads from the
// same `network_tracker` Arc.
let network_tracker = crate::network_idle::InFlightTracker::new();
let network_cancel = tokio_util::sync::CancellationToken::new();
tokio::spawn({
let tracker = network_tracker.clone();
let session_for_task = session.clone();
let cancel_for_task = network_cancel.clone();
async move {
tracker.run(session_for_task, cancel_for_task).await;
}
});
// Build the per-Tab frames registry + spawn the lifecycle
// subscriber. The task calls `Page.enable` once, then mutates the
// registry in response to `Page.frameAttached` (insert),
// `Page.frameNavigated` (update url / insert if unseen) and
// `Page.frameDetached` (remove). The `Arc<RwLock<_>>` lives on
// `TabInner::frames` so `Tab::frames` / `frame_by_url` /
// `frame_by_name` can take snapshots without going through the
// tracker task. The `Weak<TabInner>` is wired in via
// `Arc::new_cyclic` below so every `Frame` constructed by the
// subscriber can upgrade back to the owning Tab.
let frames: Arc<tokio::sync::RwLock<HashMap<String, Frame>>> =
Arc::new(tokio::sync::RwLock::new(HashMap::new()));
let frame_lifecycle_cancel = tokio_util::sync::CancellationToken::new();
let inner = Arc::new_cyclic(|weak: &std::sync::Weak<TabInner>| {
tokio::spawn({
let session_for_task = session.clone();
let frames_for_task = frames.clone();
let weak_for_task = weak.clone();
let cancel_for_task = frame_lifecycle_cancel.clone();
async move {
crate::frame::lifecycle::run(
session_for_task,
frames_for_task,
weak_for_task,
cancel_for_task,
)
.await;
}
});
TabInner {
session,
isolated_world: tokio::sync::Mutex::new(IsolatedWorldCache::default()),
browser,
input,
target_id,
network_tracker,
network_cancel,
main_frame: tokio::sync::OnceCell::new(),
#[cfg(feature = "expect")]
download_setup: tokio::sync::OnceCell::new(),
frames,
frame_lifecycle_cancel,
pending_load: tokio::sync::Mutex::new(None),
}
});
Self { inner }
}
/// Test-only constructor: builds a `Tab` with a deterministic seeded
/// [`InputController`] (native input profile, seed `42`) and an empty
/// `Weak` browser ref. Replaces the P3 `Tab::new(sess, Weak::new())`
/// pattern that paired with `Tab::input() -> Option<_>`; now that
/// `Tab::input()` returns `&Arc<InputController>` unconditionally, tests
/// must seed a controller at construction time.
///
/// The synthetic `target_id` is derived from the session_id — tests that
/// need a specific `targetId` should use [`Tab::new_for_test_with_target`].
#[cfg(test)]
pub(crate) fn new_for_test(session: SessionHandle) -> Self {
let target_id = format!("test-target-{}", session.session_id());
Self::new(
session,
std::sync::Weak::new(),
crate::input::InputController::new_with_seed(
zendriver_stealth::InputProfile::native(),
42,
),
target_id,
)
}
/// CDP `targetId` for the page target this tab wraps.
///
/// Stable for the lifetime of the underlying target — used by
/// [`crate::Browser::new_tab`] to correlate a `Target.createTarget`
/// response with the [`Tab`] that the internal `TabRegistrar`
/// subsequently registers.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// let browser = zendriver::Browser::builder().launch().await?;
/// let tab = browser.main_tab();
/// let id = tab.target_id();
/// assert!(!id.is_empty());
/// # Ok(()) }
/// ```
#[must_use]
pub fn target_id(&self) -> &str {
&self.inner.target_id
}
/// Per-Tab [`InputController`].
///
/// Each tab carries its own cursor + modifier state; [`crate::Element`]
/// actions ([`crate::Element::click`], [`crate::Element::hover`],
/// [`crate::Element::type_text`], [`crate::Element::press`]) call this
/// to drive internal mouse / keyboard dispatch helpers. Always returns a
/// valid handle.
#[must_use]
pub fn input(&self) -> &Arc<InputController> {
&self.inner.input
}
/// Raw [`SessionHandle`] escape hatch.
///
/// For advanced users who need to send CDP commands the high-level API
/// doesn't expose. Returns the underlying transport session bound to
/// this tab's `sessionId`.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// let session = tab.session();
/// // Send a CDP command the high-level API doesn't wrap.
/// session.call("Page.bringToFront", serde_json::json!({})).await?;
/// # Ok(()) }
/// ```
pub fn session(&self) -> &SessionHandle {
&self.inner.session
}
/// The top-level [`Frame`] for this tab.
///
/// First call dispatches `Page.getFrameTree` on the tab's session,
/// extracts the top-level frame's `id` / `url` / `name`, and constructs
/// a [`Frame`] whose session is this tab's session (the main frame is
/// always same-process). The result is cached internally so subsequent
/// calls return the same `Frame` clone without a round-trip.
///
/// # Errors
///
/// Returns [`ZendriverError::Navigation`] if Chrome's response is
/// missing the top-level frame id.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let main = tab.main_frame().await?;
/// assert!(main.url().await.contains("example.com"));
/// # Ok(()) }
/// ```
pub async fn main_frame(&self) -> Result<Frame> {
let frame = self
.inner
.main_frame
.get_or_try_init(|| async {
let tree = self.call("Page.getFrameTree", json!({})).await?;
let frame_node = &tree["frameTree"]["frame"];
let frame_id = frame_node["id"]
.as_str()
.ok_or_else(|| {
ZendriverError::Navigation(
"Page.getFrameTree missing frameTree.frame.id".into(),
)
})?
.to_string();
let url = frame_node["url"].as_str().unwrap_or("").to_string();
let name = frame_node["name"].as_str().map(str::to_string);
Ok::<_, ZendriverError>(Frame::new(
frame_id,
None,
url,
name,
self.inner.session.clone(),
Arc::downgrade(&self.inner),
))
})
.await?;
Ok(frame.clone())
}
/// Snapshot of all currently-registered frames for this tab.
///
/// The registry is maintained by an internal lifecycle subscriber spawned
/// when the Tab is constructed (see the [`crate::frame::lifecycle`]
/// module). Includes the top-level frame (once Chrome has emitted at
/// least one `Page.frameAttached` or `Page.frameNavigated` for it) plus
/// every same-origin sub-frame. Out-of-process iframes (OOPIFs) land in
/// this map via the [`crate::frame::oopif`] observer path.
///
/// Order is unspecified ([`HashMap`] iteration); callers that need a
/// stable order should sort by [`Frame::id`] or [`Frame::url`].
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// for f in tab.frames().await? {
/// println!("frame {}: {}", f.id(), f.url().await);
/// }
/// # Ok(()) }
/// ```
pub async fn frames(&self) -> Result<Vec<Frame>> {
Ok(self.inner.frames.read().await.values().cloned().collect())
}
/// First frame in [`Tab::frames`] whose URL contains `url_substr`.
///
/// Linear scan over the registry. Useful for picking a frame by its
/// origin (e.g. `tab.frame_by_url("docs.google.com")`) without knowing
/// the exact path. Returns `Ok(None)` if no frame matches; the registry
/// lock is released before returning so concurrent updates can land.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// if let Some(iframe) = tab.frame_by_url("youtube.com").await? {
/// println!("found iframe: {}", iframe.url().await);
/// }
/// # Ok(()) }
/// ```
pub async fn frame_by_url(&self, url_substr: &str) -> Result<Option<Frame>> {
let map = self.inner.frames.read().await;
for frame in map.values() {
if frame.url().await.contains(url_substr) {
return Ok(Some(frame.clone()));
}
}
Ok(None)
}
/// First frame in [`Tab::frames`] whose `name` attribute equals `name`.
///
/// Linear scan. Frames without a name attribute (the common case for
/// the top-level frame and unnamed iframes) are skipped. Returns
/// `Ok(None)` if no frame matches.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// if let Some(content) = tab.frame_by_name("content").await? {
/// content.evaluate::<()>("document.body.scrollTop = 0").await?;
/// }
/// # Ok(()) }
/// ```
pub async fn frame_by_name(&self, name: &str) -> Result<Option<Frame>> {
let map = self.inner.frames.read().await;
Ok(map.values().find(|f| f.name() == Some(name)).cloned())
}
/// Browser-wide cookie store handle.
///
/// Convenience accessor that delegates to the owning [`crate::Browser`]'s
/// root [`zendriver_transport::Connection`] — Chrome's cookie store is
/// browser-scoped, so this jar is functionally identical to
/// [`crate::Browser::cookies`] for the same browser.
///
/// If the owning Browser has already been dropped (which shouldn't happen
/// in practice because Drop ordering keeps it alive while any Tab clone
/// exists, but is handled defensively here), the jar falls back to the
/// Tab's session-level connection.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let jar = tab.cookies();
/// let all = jar.all().await?;
/// println!("{} cookies set", all.len());
/// # Ok(()) }
/// ```
#[must_use]
pub fn cookies(&self) -> crate::CookieJar {
let conn = self.inner.browser.upgrade().map_or_else(
|| self.inner.session.connection().clone(),
|b| b.conn.clone(),
);
crate::CookieJar::new(conn)
}
/// Per-tab `localStorage` accessor.
///
/// The returned [`crate::Storage`] is configured with `is_local: true`
/// and dispatches against this tab's session; each operation re-resolves
/// the tab's current origin via a [`Tab::url`] round-trip (since
/// DOMStorage is origin-keyed and a navigation between calls would shift
/// the target storage area).
///
/// `DOMStorage.enable` fires lazily on the first op per handle so
/// re-using the same handle across many calls pays the enable cost
/// exactly once.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let ls = tab.local_storage();
/// ls.set("theme", "dark").await?;
/// let v = ls.get("theme").await?;
/// assert_eq!(v.as_deref(), Some("dark"));
/// # Ok(()) }
/// ```
#[must_use]
pub fn local_storage(&self) -> crate::Storage {
crate::Storage::new(
self.inner.session.clone(),
true,
Arc::downgrade(&self.inner),
)
}
/// Per-tab `sessionStorage` accessor.
///
/// Mirror of [`Tab::local_storage`] with `is_local: false` — backs the
/// per-tab, per-origin `sessionStorage` area instead of the persistent
/// localStorage.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.session_storage().set("draft", "hello").await?;
/// # Ok(()) }
/// ```
#[must_use]
pub fn session_storage(&self) -> crate::Storage {
crate::Storage::new(
self.inner.session.clone(),
false,
Arc::downgrade(&self.inner),
)
}
/// Helper: call a CDP method on this tab's session, parsing transport
/// errors into `ZendriverError`.
pub(crate) async fn call(&self, method: &str, params: Value) -> Result<Value> {
trace!(%method, "tab.call");
let res = self.inner.session.call(method, params).await?;
Ok(res)
}
/// Navigate the tab to `url`.
///
/// Does NOT wait for the load to complete — call [`Tab::wait_for_load`]
/// (or [`Tab::wait_for_idle`]) afterward to block on the navigation.
///
/// # Errors
///
/// Returns [`ZendriverError::Navigation`] when Chrome reports
/// `errorText` on the `Page.navigate` response (e.g. DNS failure,
/// connection refused, invalid URL).
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.wait_for_load().await?;
/// # Ok(()) }
/// ```
pub async fn goto(&self, url: impl AsRef<str>) -> Result<()> {
// Enable Page domain so we get FrameStoppedLoading events.
self.call("Page.enable", json!({})).await?;
// Subscribe to Page.frameStoppedLoading BEFORE issuing Page.navigate.
// The transport's event bus is a tokio broadcast channel, so a
// subscriber created after the event has already been published can
// never observe it. By subscribing first, then handing a oneshot to
// `wait_for_load`, we guarantee the next load event lands in our
// receiver regardless of how fast the page loads (e.g. localhost
// wiremock fixtures in CI).
let mut stream = self
.inner
.session
.subscribe::<Value>("Page.frameStoppedLoading");
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
if stream.next().await.is_some() {
let _ = tx.send(());
}
});
*self.inner.pending_load.lock().await = Some(rx);
let url_s = url.as_ref().to_string();
let res = self.call("Page.navigate", json!({ "url": url_s })).await?;
if let Some(err) = res.get("errorText").and_then(|v| v.as_str()) {
if !err.is_empty() {
return Err(ZendriverError::Navigation(err.to_string()));
}
}
Ok(())
}
/// Wait until the main frame's load event fires.
///
/// Subscribes to `Page.frameStoppedLoading` and waits for the first
/// event. Bounded by a 30s timeout.
///
/// # Errors
///
/// Returns [`ZendriverError::Timeout`] when no load event arrives
/// within 30s; [`ZendriverError::Navigation`] if the event stream
/// closes (transport teardown).
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.wait_for_load().await?;
/// # Ok(()) }
/// ```
pub async fn wait_for_load(&self) -> Result<()> {
// Preferred path: consume the oneshot stashed by `goto`, which
// subscribed to `Page.frameStoppedLoading` BEFORE the navigation
// request — guaranteed delivery.
if let Some(rx) = self.inner.pending_load.lock().await.take() {
timeout(DEFAULT_LOAD_TIMEOUT, rx)
.await
.map_err(|_| ZendriverError::Timeout(DEFAULT_LOAD_TIMEOUT))?
.map_err(|_| ZendriverError::Navigation("page event stream closed".into()))?;
return Ok(());
}
// Fallback: no pending navigation (e.g. caller invoked
// `wait_for_load` without a preceding `goto`, or after the page
// navigated itself). Subscribe + short-circuit on the current
// `document.readyState` so an already-loaded page returns
// immediately rather than blocking on a missed event.
let mut stream = self
.inner
.session
.subscribe::<Value>("Page.frameStoppedLoading");
let ready: Option<String> = self
.call(
"Runtime.evaluate",
json!({
"expression": "document.readyState",
"returnByValue": true,
}),
)
.await
.ok()
.and_then(|v| v.get("result")?.get("value")?.as_str().map(str::to_owned));
if ready.as_deref() == Some("complete") {
return Ok(());
}
timeout(DEFAULT_LOAD_TIMEOUT, stream.next())
.await
.map_err(|_| ZendriverError::Timeout(DEFAULT_LOAD_TIMEOUT))?
.ok_or_else(|| ZendriverError::Navigation("page event stream closed".into()))?;
Ok(())
}
/// Evaluate a JavaScript expression in an isolated world.
///
/// Runs in a sandbox where page globals are NOT visible — the default for
/// stealth-safe execution. The result is deserialized into `T`.
///
/// If the cached isolated-world execution context was destroyed (e.g. by
/// a page navigation), the cache is invalidated and the evaluation is
/// retried once. One retry is enough: the failure mode this guards
/// against is "navigation happened between cache-fetch and `Runtime.evaluate`",
/// which is a one-shot race — recreating the world for the new context
/// and re-issuing the call clears it. If the same call fails the
/// second attempt the page has a real problem (target gone, isolated
/// world refused to recreate) and further retries would only mask it.
///
/// # Errors
///
/// Returns [`ZendriverError::JsException`] when the expression raises;
/// [`ZendriverError::Serde`] when the result cannot be decoded into `T`;
/// [`ZendriverError::Navigation`] when the execution context is missing.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let n: i32 = tab.evaluate("1 + 2").await?;
/// assert_eq!(n, 3);
/// # Ok(()) }
/// ```
pub async fn evaluate<T: DeserializeOwned>(&self, js: impl AsRef<str>) -> Result<T> {
let js = js.as_ref();
for attempt in 0..2 {
let ctx_id = self.ensure_isolated_world().await?;
let res = self
.call(
"Runtime.evaluate",
json!({
"expression": js,
"contextId": ctx_id,
"returnByValue": true,
"awaitPromise": true,
}),
)
.await;
match res {
Ok(v) => {
if let Some(details) = v.get("exceptionDetails") {
let msg = details
.get("exception")
.and_then(|e| e.get("description"))
.and_then(|d| d.as_str())
.unwrap_or("unknown")
.to_string();
return Err(ZendriverError::JsException(msg));
}
let value = v
.get("result")
.and_then(|r| r.get("value"))
.cloned()
.unwrap_or(Value::Null);
return serde_json::from_value(value).map_err(ZendriverError::Serde);
}
// Chrome returns -32000 "Cannot find context with specified
// id" when the execution context we cached was destroyed
// (typically by a navigation). `From<CallError>` maps that
// to `Navigation` (see `error.rs`), so we match on that
// variant here — not on `Cdp` as the original P2 plan
// suggested.
Err(ZendriverError::Navigation(ref m))
if attempt == 0 && m.contains("Cannot find context") =>
{
self.inner.isolated_world.lock().await.context_id = None;
continue;
}
Err(e) => return Err(e),
}
}
unreachable!()
}
/// Evaluate a JavaScript expression in the page main world.
///
/// Page globals (e.g. `window.foo` set by page scripts) ARE visible.
/// Escape hatch for cases where isolated-world semantics don't fit; for
/// stealth-sensitive contexts prefer [`Tab::evaluate`].
///
/// # Errors
///
/// Returns [`ZendriverError::JsException`] when the expression raises;
/// [`ZendriverError::Serde`] when the result cannot be decoded into `T`.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let title: String = tab.evaluate_main("document.title").await?;
/// println!("{title}");
/// # Ok(()) }
/// ```
pub async fn evaluate_main<T: DeserializeOwned>(&self, js: impl AsRef<str>) -> Result<T> {
let res = self
.call(
"Runtime.evaluate",
json!({
"expression": js.as_ref(),
"returnByValue": true,
"awaitPromise": true,
}),
)
.await?;
if let Some(details) = res.get("exceptionDetails") {
let msg = details
.get("exception")
.and_then(|e| e.get("description"))
.and_then(|d| d.as_str())
.unwrap_or("unknown")
.to_string();
return Err(ZendriverError::JsException(msg));
}
let value = res
.get("result")
.and_then(|r| r.get("value"))
.cloned()
.unwrap_or(Value::Null);
serde_json::from_value(value).map_err(ZendriverError::Serde)
}
/// Ensure an isolated-world execution context exists for this tab's main
/// frame, returning its `executionContextId`. Cached after first call.
pub(crate) async fn ensure_isolated_world(&self) -> Result<i64> {
let mut cache = self.inner.isolated_world.lock().await;
if let Some(ctx) = cache.context_id {
return Ok(ctx);
}
// Discover the main frame id.
let tree = self.call("Page.getFrameTree", json!({})).await?;
let frame_id = tree["frameTree"]["frame"]["id"]
.as_str()
.ok_or_else(|| ZendriverError::Navigation("no main frame in Page.getFrameTree".into()))?
.to_string();
let res = self
.call(
"Page.createIsolatedWorld",
json!({
"frameId": frame_id,
"worldName": "zendriver-eval",
"grantUniversalAccess": false,
}),
)
.await?;
let ctx_id = res["executionContextId"].as_i64().ok_or_else(|| {
ZendriverError::Navigation(
"Page.createIsolatedWorld did not return executionContextId".into(),
)
})?;
cache.main_frame_id = Some(frame_id);
cache.context_id = Some(ctx_id);
Ok(ctx_id)
}
/// Get the tab's current URL.
///
/// Returns a parsed [`url::Url`]. Reads from `Target.getTargetInfo`'s
/// `targetInfo.url`.
///
/// # Errors
///
/// Returns [`ZendriverError::Navigation`] when Chrome returns no URL or
/// the URL is unparseable.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com/foo").await?;
/// let u = tab.url().await?;
/// assert_eq!(u.path(), "/foo");
/// # Ok(()) }
/// ```
pub async fn url(&self) -> Result<url::Url> {
let res = self.call("Target.getTargetInfo", json!({})).await?;
let s = res["targetInfo"]["url"]
.as_str()
.ok_or_else(|| ZendriverError::Navigation("target has no url".into()))?;
url::Url::parse(s).map_err(|e| ZendriverError::Navigation(e.to_string()))
}
/// Get the tab's `<title>`.
///
/// Reads from `Target.getTargetInfo`'s `targetInfo.title`. Returns an
/// empty string when the page has no title.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// assert_eq!(tab.title().await?, "Example Domain");
/// # Ok(()) }
/// ```
pub async fn title(&self) -> Result<String> {
let res = self.call("Target.getTargetInfo", json!({})).await?;
Ok(res["targetInfo"]["title"]
.as_str()
.unwrap_or("")
.to_string())
}
/// Construct a [`ScreenshotBuilder`] bound to this tab.
///
/// Chain format / clip / quality / full-page options, then call
/// [`ScreenshotBuilder::bytes`] or [`ScreenshotBuilder::save`] to
/// execute the capture.
///
/// For element-scoped screenshots, see [`crate::Element::screenshot`].
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.screenshot_builder()
/// .full_page(true)
/// .jpeg()
/// .quality(85)
/// .save("page.jpg").await?;
/// # Ok(()) }
/// ```
#[must_use]
pub fn screenshot_builder(&self) -> ScreenshotBuilder<'_> {
ScreenshotBuilder::new(self)
}
/// Capture a full-viewport PNG screenshot of this tab.
///
/// Convenience wrapper over `self.screenshot_builder().png().bytes().await`.
/// For JPEG / WebP / full-page / clipped captures, drive
/// [`Tab::screenshot_builder`] directly. For element-scoped screenshots,
/// see [`crate::Element::screenshot`].
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let png_bytes = tab.screenshot().await?;
/// tokio::fs::write("page.png", png_bytes).await?;
/// # Ok(()) }
/// ```
pub async fn screenshot(&self) -> Result<Vec<u8>> {
self.screenshot_builder().png().bytes().await
}
/// Close this tab in Chrome.
///
/// Sends `Target.closeTarget { targetId }` at browser scope (no
/// `session_id`) using the cached `targetId`. Chrome destroys the page
/// target, which in turn produces a `Target.detachedFromTarget` event
/// whose internal handler removes this tab from the browser's tab
/// registry.
///
/// Consumes `self` — the [`Tab`] handle is gone after this returns.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// let tab = browser.new_tab().await?;
/// tab.goto("https://example.com").await?;
/// tab.close().await?;
/// # Ok(()) }
/// ```
pub async fn close(self) -> Result<()> {
let target_id = self.target_id().to_string();
self.inner
.session
.connection()
.call_raw("Target.closeTarget", json!({ "targetId": target_id }), None)
.await?;
Ok(())
}
/// Bring this tab to the foreground in Chrome.
///
/// Sends `Target.activateTarget { targetId }` at browser scope (no
/// `session_id`) using the cached `targetId`. Chrome focuses the page
/// target so it becomes the visible/active tab.
///
/// Unlike [`Tab::close`], this borrows `&self` — the tab remains usable
/// after activation. Useful in multi-tab workflows where you want to
/// surface a specific tab without tearing it down.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// let tab1 = browser.main_tab();
/// let tab2 = browser.new_tab().await?;
/// // Bring the first tab back to focus.
/// tab1.activate().await?;
/// # let _ = tab2;
/// # Ok(()) }
/// ```
pub async fn activate(&self) -> Result<()> {
let target_id = self.target_id().to_string();
self.inner
.session
.connection()
.call_raw(
"Target.activateTarget",
json!({ "targetId": target_id }),
None,
)
.await?;
Ok(())
}
/// Navigate one step backward in the tab's session history.
///
/// Fetches the history list via `Page.getNavigationHistory`, then
/// dispatches `Page.navigateToHistoryEntry { entryId }` for the entry at
/// `currentIndex - 1`.
///
/// # Errors
///
/// Returns [`ZendriverError::HistoryNavigation`] with `"no back history"`
/// when `currentIndex <= 0`.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.goto("https://example.org").await?;
/// tab.back().await?;
/// # Ok(()) }
/// ```
pub async fn back(&self) -> Result<()> {
let history = self.call("Page.getNavigationHistory", json!({})).await?;
let current_idx = history["currentIndex"].as_i64().ok_or_else(|| {
ZendriverError::HistoryNavigation(
"Page.getNavigationHistory missing currentIndex".into(),
)
})?;
if current_idx <= 0 {
return Err(ZendriverError::HistoryNavigation("no back history".into()));
}
let entry_id = history["entries"][(current_idx - 1) as usize]["id"].clone();
self.call(
"Page.navigateToHistoryEntry",
json!({ "entryId": entry_id }),
)
.await?;
Ok(())
}
/// Navigate one step forward in the tab's session history.
///
/// Fetches the history list via `Page.getNavigationHistory`, then
/// dispatches `Page.navigateToHistoryEntry { entryId }` for the entry at
/// `currentIndex + 1`.
///
/// # Errors
///
/// Returns [`ZendriverError::HistoryNavigation`] with `"no forward history"`
/// when `currentIndex` is already at the last entry.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.goto("https://example.org").await?;
/// tab.back().await?;
/// tab.forward().await?;
/// # Ok(()) }
/// ```
pub async fn forward(&self) -> Result<()> {
let history = self.call("Page.getNavigationHistory", json!({})).await?;
let current_idx = history["currentIndex"].as_i64().ok_or_else(|| {
ZendriverError::HistoryNavigation(
"Page.getNavigationHistory missing currentIndex".into(),
)
})?;
let entries = history["entries"].as_array().ok_or_else(|| {
ZendriverError::HistoryNavigation("Page.getNavigationHistory missing entries".into())
})?;
if (current_idx + 1) as usize >= entries.len() {
return Err(ZendriverError::HistoryNavigation(
"no forward history".into(),
));
}
let entry_id = entries[(current_idx + 1) as usize]["id"].clone();
self.call(
"Page.navigateToHistoryEntry",
json!({ "entryId": entry_id }),
)
.await?;
Ok(())
}
/// Reload the tab's current page.
///
/// Dispatches `Page.reload` with `ignoreCache: false` — equivalent to a
/// soft refresh.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.reload().await?;
/// # Ok(()) }
/// ```
pub async fn reload(&self) -> Result<()> {
self.call("Page.reload", json!({ "ignoreCache": false }))
.await?;
Ok(())
}
/// Wait until the tab's network has been idle (0 in-flight requests)
/// for 500ms, with a 30s outer timeout. Playwright `networkidle`
/// semantics.
///
/// Backed by a per-Tab in-flight network tracker that subscribes to
/// `Network.requestWillBeSent` (insert) and the three terminal events
/// (`responseReceived` / `loadingFailed` / `loadingFinished`, all
/// remove).
///
/// # Errors
///
/// Returns [`ZendriverError::Timeout`] with the configured timeout
/// duration when the network does not stay idle within the deadline.
///
/// See [`Tab::wait_for_idle_with`] for tunable timeout + quiet window.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.wait_for_idle().await?;
/// # Ok(()) }
/// ```
pub async fn wait_for_idle(&self) -> Result<()> {
self.wait_for_idle_with(Duration::from_secs(30), Duration::from_millis(500))
.await
}
/// Wait until the tab's network has been idle for `quiet_window`,
/// bounded by `timeout`.
///
/// Algorithm: poll the in-flight set with a `Notify`-driven wake (or a
/// 50ms fallback tick). Track `quiet_start = Some(now)` on the first
/// observation of an empty set; reset to `None` on any observation
/// where the set is non-empty. Return once `now - quiet_start
/// >= quiet_window`.
///
/// The 50ms tick is a safety net for the case where the tracker is
/// already at 0 in-flight requests and no further events fire to wake
/// the notifier. Worst-case latency to detect "stayed idle long enough"
/// is `quiet_window + 50ms`.
///
/// # Errors
///
/// Returns [`ZendriverError::Timeout`] (carrying the supplied `timeout`)
/// once the outer deadline elapses.
///
/// # Examples
///
/// ```no_run
/// # use std::time::Duration;
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// tab.wait_for_idle_with(
/// Duration::from_secs(60),
/// Duration::from_secs(1),
/// ).await?;
/// # Ok(()) }
/// ```
pub async fn wait_for_idle_with(
&self,
timeout: Duration,
quiet_window: Duration,
) -> Result<()> {
let tracker = self.inner.network_tracker.clone();
let deadline = tokio::time::Instant::now() + timeout;
let mut quiet_start: Option<tokio::time::Instant> = None;
loop {
// Arm the notification interest BEFORE reading the in-flight set
// so a notification fired between the read and the `select!`
// below is still delivered. `Notify::notified()` only catches
// notifications fired after the future has been `enable()`d, so
// doing it the other way around would let a request that started
// *and finished* inside the quiet window slip past us with a
// sustained count of 0 — `wait_for_idle` would return early.
let notif = tracker.notifier.notified();
tokio::pin!(notif);
notif.as_mut().enable();
let in_flight_count = tracker.in_flight.lock().await.len();
if in_flight_count == 0 {
let now = tokio::time::Instant::now();
match quiet_start {
None => quiet_start = Some(now),
Some(start) if now.duration_since(start) >= quiet_window => {
return Ok(());
}
_ => {}
}
} else {
quiet_start = None;
}
if tokio::time::Instant::now() >= deadline {
return Err(ZendriverError::Timeout(timeout));
}
tokio::select! {
() = tokio::time::sleep(Duration::from_millis(50)) => {}
() = notif => {
// A membership change fired since we armed `notif`. Reset
// the quiet window — even if the set is back to zero by
// the next iteration, real activity occurred during this
// window so it doesn't count as "idle".
quiet_start = None;
}
}
}
}
}
impl Tab {
/// Begin a chainable element query against this tab.
///
/// Pick a selector kind (`.css`, `.xpath`, `.text`, `.text_exact`,
/// `.text_regex`, `.text_regex_with_flags`, `.role`, `.role_named`),
/// optionally apply modifiers (`.nth`, `.visible_only`, `.in_frame`,
/// `.timeout`), then terminate with `.one()` or `.one_or_none()`.
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let h1 = tab.find().css("h1").one().await?;
/// h1.click().await?;
/// # Ok(()) }
/// ```
pub fn find(&self) -> crate::query::FindBuilder<'_> {
crate::query::FindBuilder::new_for_tab(self)
}
/// Begin a chainable element query against this tab that returns
/// ALL matches.
///
/// Mirrors [`Tab::find`] selectors + modifiers (no `nth`); terminate
/// with `.many()` (errors on empty) or `.many_or_empty()` (returns
/// empty `Vec` instead).
///
/// # Examples
///
/// ```no_run
/// # async fn ex() -> zendriver::Result<()> {
/// # let browser = zendriver::Browser::builder().launch().await?;
/// # let tab = browser.main_tab();
/// tab.goto("https://example.com").await?;
/// let links = tab.find_all().css("a").many_or_empty().await?;
/// println!("{} links", links.len());
/// # Ok(()) }
/// ```
pub fn find_all(&self) -> crate::query::FindAllBuilder<'_> {
crate::query::FindAllBuilder::new_for_tab(self)
}
}
#[cfg(feature = "expect")]
impl Tab {
/// Register a one-shot expectation for the first
/// `Network.requestWillBeSent` whose URL matches `pattern`.
///
/// `pattern` is anything convertible to a [`crate::expect::UrlMatcher`]:
/// `&str` / `String` build a substring matcher; [`regex::Regex`] builds
/// a regex matcher. The returned
/// [`RequestExpectation`](crate::expect::request::RequestExpectation)
/// is awaitable directly (`expectation.await`) or via the
/// Playwright-style `expectation.matched().await`; configure the
/// timeout via
/// [`timeout`](crate::expect::request::RequestExpectation::timeout)
/// before awaiting.
///
/// The subscriber task is spawned synchronously inside this call —
/// the subscription is live by the time you receive the
/// `RequestExpectation`, so a trigger action issued immediately
/// after cannot race past us. `Network.enable` is already on per-Tab
/// via the P4 in-flight tracker; this call does not re-enable.
///
/// Gated by the `expect` cargo feature.
#[must_use]
pub fn expect_request(
&self,
pattern: impl Into<crate::expect::UrlMatcher>,
) -> crate::expect::request::RequestExpectation {
crate::expect::request::register(self.session(), pattern.into())
}
/// Register a one-shot expectation for the first
/// `Network.responseReceived` whose URL matches `pattern`.
///
/// `pattern` is anything convertible to a [`crate::expect::UrlMatcher`]:
/// `&str` / `String` build a substring matcher; [`regex::Regex`] builds
/// a regex matcher. The returned
/// [`ResponseExpectation`](crate::expect::response::ResponseExpectation)
/// is awaitable directly (`expectation.await`) or via the
/// Playwright-style `expectation.matched().await`; configure the
/// timeout via
/// [`timeout`](crate::expect::response::ResponseExpectation::timeout)
/// before awaiting.
///
/// Resolves with a
/// [`MatchedResponse`](crate::expect::response::MatchedResponse) whose
/// [`body`](crate::expect::response::MatchedResponse::body) method
/// fetches the response payload via `Network.getResponseBody`. Bodies
/// are only retained for a short window after the response completes —
/// call `body()` promptly.
///
/// The subscriber task is spawned synchronously inside this call —
/// the subscription is live by the time you receive the
/// `ResponseExpectation`, so a trigger action issued immediately after
/// cannot race past us. `Network.enable` is already on per-Tab via the
/// P4 in-flight tracker; this call does not re-enable.
///
/// Gated by the `expect` cargo feature.
#[must_use]
pub fn expect_response(
&self,
pattern: impl Into<crate::expect::UrlMatcher>,
) -> crate::expect::response::ResponseExpectation {
crate::expect::response::register(self.session(), pattern.into())
}
/// Register a one-shot expectation for the first
/// `Page.javascriptDialogOpened` event on this tab.
///
/// There is no URL pattern: dialogs don't carry a request URL the way
/// requests/responses do — any dialog opened during the expectation
/// window matches. The page URL is captured on the resolved
/// [`MatchedDialog`](crate::expect::dialog::MatchedDialog) for context.
///
/// The returned
/// [`DialogExpectation`](crate::expect::dialog::DialogExpectation) is
/// awaitable directly (`expectation.await`) or via the
/// Playwright-style `expectation.matched().await`; configure the
/// timeout via
/// [`timeout`](crate::expect::dialog::DialogExpectation::timeout) before
/// awaiting.
///
/// Resolves with a
/// [`MatchedDialog`](crate::expect::dialog::MatchedDialog) whose
/// [`accept`](crate::expect::dialog::MatchedDialog::accept) /
/// [`dismiss`](crate::expect::dialog::MatchedDialog::dismiss) methods
/// dispatch `Page.handleJavaScriptDialog`.
///
/// The subscriber task is spawned synchronously inside this call — the
/// subscription is live by the time you receive the
/// `DialogExpectation`, so a trigger action issued immediately after
/// cannot race past us. `Page.enable` is already on per-Tab via P1's
/// `Tab::goto`; this call does not re-enable.
///
/// Gated by the `expect` cargo feature.
#[must_use]
pub fn expect_dialog(&self) -> crate::expect::dialog::DialogExpectation {
crate::expect::dialog::register(self.session())
}
/// Register a one-shot expectation for the first `Page.downloadWillBegin`
/// on this tab.
///
/// First call on a Tab also allocates a per-Tab tempdir, dispatches
/// `Browser.setDownloadBehavior { behavior: "allowAndName", downloadPath
/// }` at browser scope, and spawns a long-running `Page.downloadProgress`
/// subscriber. The coordinator is reused across every subsequent
/// `expect_download` call on the same tab. `Page.enable` is already on
/// per-Tab via P1's `Tab::goto` / the frame lifecycle subscriber, so
/// this call does not re-enable.
///
/// Returned [`MatchedDownload`](crate::expect::download::MatchedDownload)
/// exposes [`path`](crate::expect::download::MatchedDownload::path) /
/// [`save_to`](crate::expect::download::MatchedDownload::save_to) for
/// reaching the downloaded bytes once Chrome reports completion.
///
/// Gated by the `expect` cargo feature.
pub async fn expect_download(&self) -> Result<crate::expect::download::DownloadExpectation> {
let coord = crate::expect::download::ensure_download_setup(
&self.inner.download_setup,
self.session(),
)
.await?;
Ok(crate::expect::download::register(self.session(), coord))
}
}
#[cfg(feature = "cloudflare")]
impl Tab {
/// Construct a
/// [`CloudflareBypass`](zendriver_cloudflare::CloudflareBypass) bound to
/// this tab's session.
///
/// Chain
/// [`poll_interval`](zendriver_cloudflare::CloudflareBypass::poll_interval)
/// to tune the polling cadence, then call
/// [`wait_for_clearance`](zendriver_cloudflare::CloudflareBypass::wait_for_clearance)
/// to detect the Turnstile checkbox, click it at the canonical 15%
/// offset, and poll until either the `cf-turnstile-response` token
/// appears, the challenge container disappears, or the supplied timeout
/// elapses. Use
/// [`is_challenge_present`](zendriver_cloudflare::CloudflareBypass::is_challenge_present)
/// for a one-shot probe without driving a click.
///
/// **Stealth recommended.** Cloudflare Turnstile is somewhat forgiving
/// of non-stealth Chrome, but `BrowserBuilder::stealth` significantly
/// raises the clearance success rate.
///
/// Gated by the `cloudflare` cargo feature.
#[must_use]
pub fn cloudflare(&self) -> zendriver_cloudflare::CloudflareBypass<'_> {
zendriver_cloudflare::CloudflareBypass::new(self.session())
}
}
#[cfg(feature = "imperva")]
impl Tab {
/// Construct an
/// [`ImpervaBypass`](zendriver_imperva::ImpervaBypass) bound to this
/// tab's session.
///
/// Chain
/// [`timeout`](zendriver_imperva::ImpervaBypass::timeout) /
/// [`poll_interval`](zendriver_imperva::ImpervaBypass::poll_interval) /
/// [`with_interception`](zendriver_imperva::ImpervaBypass::with_interception) /
/// [`on_captcha`](zendriver_imperva::ImpervaBypass::on_captcha)
/// builder methods, then call
/// [`wait_for_clearance`](zendriver_imperva::ImpervaBypass::wait_for_clearance)
/// to detect the active Imperva surface (modern reese84, legacy
/// Incapsula, or CAPTCHA escalation) and poll until clearance.
///
/// **Stealth required.** Without `BrowserBuilder::stealth`, the
/// bypass will fail on nearly all real Imperva-protected sites.
///
/// Gated by the `imperva` cargo feature.
#[must_use]
pub fn imperva(&self) -> zendriver_imperva::ImpervaBypass<'_> {
zendriver_imperva::ImpervaBypass::new(self.session())
}
}
#[cfg(feature = "interception")]
impl Tab {
/// Construct a fluent
/// [`InterceptBuilder`](zendriver_interception::InterceptBuilder) for
/// this tab's session.
///
/// Chain rule registration (`.block(...)` / `.redirect(...)` /
/// `.respond(...)` / `.modify_request(...)`) and optional CDP
/// `RequestPattern` filters (`.pattern(...)` / `.at_request()` /
/// `.at_response()` / `.resource(...)`), then call
/// [`start`](zendriver_interception::InterceptBuilder::start) to spawn
/// the rule-driven actor (returns an
/// [`InterceptHandle`](zendriver_interception::InterceptHandle) whose
/// `Drop` tears it down), or
/// [`subscribe`](zendriver_interception::InterceptBuilder::subscribe)
/// to receive raw
/// [`PausedRequest`](zendriver_interception::PausedRequest)s on a
/// stream you drive manually.
///
/// Gated by the `interception` cargo feature.
#[must_use]
pub fn intercept(&self) -> zendriver_interception::InterceptBuilder<'_> {
zendriver_interception::InterceptBuilder::new(self.session())
}
}
impl crate::traits::Queryable for Tab {
fn find(&self) -> crate::query::FindBuilder<'_> {
Tab::find(self)
}
fn find_all(&self) -> crate::query::FindAllBuilder<'_> {
Tab::find_all(self)
}
}
#[async_trait::async_trait]
impl crate::traits::Evaluable for Tab {
async fn evaluate<T>(&self, js: &str) -> crate::error::Result<T>
where
T: serde::de::DeserializeOwned + Send + 'static,
{
Tab::evaluate(self, js).await
}
async fn evaluate_main<T>(&self, js: &str) -> crate::error::Result<T>
where
T: serde::de::DeserializeOwned + Send + 'static,
{
Tab::evaluate_main(self, js).await
}
}
#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod tests {
use super::*;
use zendriver_transport::testing::MockConnection;
#[tokio::test]
async fn goto_sends_page_enable_then_page_navigate_with_url() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.goto("https://example.com").await }
});
let id_enable = mock.expect_cmd("Page.enable").await;
mock.reply(id_enable, json!({})).await;
let id_nav = mock.expect_cmd("Page.navigate").await;
assert_eq!(mock.last_sent()["params"]["url"], "https://example.com");
mock.reply(id_nav, json!({ "frameId": "F1" })).await;
fut.await.unwrap().unwrap();
conn.shutdown();
}
#[tokio::test]
async fn goto_returns_navigation_error_when_chrome_reports_errortext() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.goto("https://bad.test").await }
});
let id_enable = mock.expect_cmd("Page.enable").await;
mock.reply(id_enable, json!({})).await;
let id_nav = mock.expect_cmd("Page.navigate").await;
mock.reply(id_nav, json!({ "errorText": "net::ERR_NAME_NOT_RESOLVED" }))
.await;
let res = fut.await.unwrap();
match res {
Err(ZendriverError::Navigation(m)) => assert!(m.contains("ERR_NAME_NOT_RESOLVED")),
other => panic!("unexpected: {other:?}"),
}
conn.shutdown();
}
// --- main-world evaluate (escape hatch) ----------------------------
#[tokio::test]
async fn evaluate_main_returns_typed_value() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.evaluate_main::<i32>("1+1").await }
});
let id = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["expression"], "1+1");
// Main-world evaluate must NOT pass a contextId.
assert!(mock.last_sent()["params"].get("contextId").is_none());
mock.reply(id, json!({ "result": { "value": 2, "type": "number" } }))
.await;
let n = fut.await.unwrap().unwrap();
assert_eq!(n, 2);
conn.shutdown();
}
#[tokio::test]
async fn evaluate_main_returns_js_exception_when_chrome_reports_one() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.evaluate_main::<i32>("throw new Error('boom')").await }
});
let id = mock.expect_cmd("Runtime.evaluate").await;
mock.reply(
id,
json!({
"result": { "type": "object", "subtype": "error" },
"exceptionDetails": {
"exception": { "description": "Error: boom\n at <anonymous>:1:7" }
}
}),
)
.await;
let res = fut.await.unwrap();
match res {
Err(ZendriverError::JsException(m)) => assert!(m.contains("Error: boom")),
other => panic!("unexpected: {other:?}"),
}
conn.shutdown();
}
// --- isolated-world evaluate ---------------------------------------
#[tokio::test]
async fn evaluate_isolated_creates_world_then_evaluates() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("1+1").await }
});
// 1. Page.getFrameTree → main frame id.
let id_tree = mock.expect_cmd("Page.getFrameTree").await;
mock.reply(
id_tree,
json!({ "frameTree": { "frame": { "id": "MAIN_FRAME" } } }),
)
.await;
// 2. Page.createIsolatedWorld → executionContextId.
let id_world = mock.expect_cmd("Page.createIsolatedWorld").await;
assert_eq!(mock.last_sent()["params"]["frameId"], "MAIN_FRAME");
assert_eq!(mock.last_sent()["params"]["worldName"], "zendriver-eval");
mock.reply(id_world, json!({ "executionContextId": 42 }))
.await;
// 3. Runtime.evaluate with that contextId.
let id_eval = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["expression"], "1+1");
assert_eq!(mock.last_sent()["params"]["contextId"], 42);
mock.reply(
id_eval,
json!({ "result": { "value": 2, "type": "number" } }),
)
.await;
let n = fut.await.unwrap().unwrap();
assert_eq!(n, 2);
conn.shutdown();
}
#[tokio::test]
async fn evaluate_caches_context_id_across_calls() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
// First call: full handshake + eval.
let fut1 = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("1").await }
});
let id_tree = mock.expect_cmd("Page.getFrameTree").await;
mock.reply(
id_tree,
json!({ "frameTree": { "frame": { "id": "MAIN_FRAME" } } }),
)
.await;
let id_world = mock.expect_cmd("Page.createIsolatedWorld").await;
mock.reply(id_world, json!({ "executionContextId": 7 }))
.await;
let id_eval1 = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["contextId"], 7);
mock.reply(
id_eval1,
json!({ "result": { "value": 1, "type": "number" } }),
)
.await;
assert_eq!(fut1.await.unwrap().unwrap(), 1);
// Second call: must reuse the cached contextId → next outbound
// frame should be Runtime.evaluate, with NO Page.getFrameTree or
// Page.createIsolatedWorld in between.
let fut2 = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("2").await }
});
let id_eval2 = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["contextId"], 7);
assert_eq!(mock.last_sent()["params"]["expression"], "2");
mock.reply(
id_eval2,
json!({ "result": { "value": 2, "type": "number" } }),
)
.await;
assert_eq!(fut2.await.unwrap().unwrap(), 2);
conn.shutdown();
}
#[tokio::test]
async fn evaluate_recreates_world_after_context_destroyed_error() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
// --- Call 1: establishes cache, succeeds. ---
let fut1 = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("1").await }
});
let id_tree = mock.expect_cmd("Page.getFrameTree").await;
mock.reply(
id_tree,
json!({ "frameTree": { "frame": { "id": "MAIN_FRAME" } } }),
)
.await;
let id_world = mock.expect_cmd("Page.createIsolatedWorld").await;
mock.reply(id_world, json!({ "executionContextId": 7 }))
.await;
let id_eval1 = mock.expect_cmd("Runtime.evaluate").await;
mock.reply(
id_eval1,
json!({ "result": { "value": 1, "type": "number" } }),
)
.await;
assert_eq!(fut1.await.unwrap().unwrap(), 1);
// --- Call 2: cached contextId is now stale. Runtime.evaluate
// returns -32000 "Cannot find context with specified id";
// evaluate must invalidate the cache, re-run the discovery
// handshake with a NEW contextId, then re-issue Runtime.evaluate.
let fut2 = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("2").await }
});
// First Runtime.evaluate uses cached id 7 → CDP returns error.
let id_eval_fail = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["contextId"], 7);
mock.reply_err(
id_eval_fail,
-32000,
"Cannot find context with specified id",
)
.await;
// Cache invalidated → discovery handshake re-runs.
let id_tree2 = mock.expect_cmd("Page.getFrameTree").await;
mock.reply(
id_tree2,
json!({ "frameTree": { "frame": { "id": "MAIN_FRAME_2" } } }),
)
.await;
let id_world2 = mock.expect_cmd("Page.createIsolatedWorld").await;
assert_eq!(mock.last_sent()["params"]["frameId"], "MAIN_FRAME_2");
mock.reply(id_world2, json!({ "executionContextId": 99 }))
.await;
// Retried Runtime.evaluate uses the fresh contextId.
let id_eval_retry = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["contextId"], 99);
mock.reply(
id_eval_retry,
json!({ "result": { "value": 2, "type": "number" } }),
)
.await;
assert_eq!(fut2.await.unwrap().unwrap(), 2);
// --- Call 3: cache is fresh again → straight to Runtime.evaluate.
let fut3 = tokio::spawn({
let t = tab.clone();
async move { t.evaluate::<i32>("3").await }
});
let id_eval3 = mock.expect_cmd("Runtime.evaluate").await;
assert_eq!(mock.last_sent()["params"]["contextId"], 99);
mock.reply(
id_eval3,
json!({ "result": { "value": 3, "type": "number" } }),
)
.await;
assert_eq!(fut3.await.unwrap().unwrap(), 3);
conn.shutdown();
}
// --- main_frame discovery (P4 T12) --------------------------------
/// First [`Tab::main_frame`] call dispatches `Page.getFrameTree`, parses
/// the top-level frame, and constructs a [`Frame`] with `is_main() ==
/// true`. Second call must NOT round-trip — the `OnceCell` caches the
/// `Frame` so further outbound traffic is empty for the same tab.
#[tokio::test]
async fn main_frame_discovers_top_level_frame_and_caches() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.main_frame().await }
});
let id = mock.expect_cmd("Page.getFrameTree").await;
mock.reply(
id,
json!({
"frameTree": {
"frame": {
"id": "F0",
"url": "https://x.test",
}
}
}),
)
.await;
let frame = fut.await.unwrap().unwrap();
assert_eq!(frame.id(), "F0");
assert!(frame.is_main());
assert!(frame.parent_id().is_none());
assert!(frame.name().is_none());
assert_eq!(frame.url().await, "https://x.test");
// Second call: must hit cache, no further outbound CDP traffic.
let frame2 = tab.main_frame().await.unwrap();
assert_eq!(frame2.id(), "F0");
// Verify the mock saw no additional commands — `expect_cmd` would
// time out internally on the next call. We check via the lighter
// `try_next` shape: a follow-up request would be queued already.
// Drop the connection to assert nothing else is in-flight.
conn.shutdown();
}
#[tokio::test]
async fn url_returns_parsed_url_from_target_info() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.url().await }
});
let id = mock.expect_cmd("Target.getTargetInfo").await;
mock.reply(
id,
json!({ "targetInfo": { "url": "https://example.com/x", "title": "ok" } }),
)
.await;
let u = fut.await.unwrap().unwrap();
assert_eq!(u.as_str(), "https://example.com/x");
conn.shutdown();
}
#[tokio::test]
async fn close_sends_target_close_target_with_target_id() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S42");
// `Tab::new_for_test` derives a deterministic target_id from the
// session_id: `test-target-S42` here.
let tab = Tab::new_for_test(sess);
assert_eq!(tab.target_id(), "test-target-S42");
let fut = tokio::spawn({
let t = tab.clone();
async move { t.close().await }
});
let id = mock.expect_cmd("Target.closeTarget").await;
assert_eq!(mock.last_sent()["params"]["targetId"], "test-target-S42");
// Browser-scope command — no session_id.
assert!(mock.last_sent().get("sessionId").is_none());
mock.reply(id, json!({ "success": true })).await;
fut.await.unwrap().unwrap();
conn.shutdown();
}
#[tokio::test]
async fn activate_sends_target_activate_target_with_target_id() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S99");
// `Tab::new_for_test` derives a deterministic target_id from the
// session_id: `test-target-S99` here.
let tab = Tab::new_for_test(sess);
assert_eq!(tab.target_id(), "test-target-S99");
let fut = tokio::spawn({
let t = tab.clone();
async move { t.activate().await }
});
let id = mock.expect_cmd("Target.activateTarget").await;
assert_eq!(mock.last_sent()["params"]["targetId"], "test-target-S99");
// Browser-scope command — no session_id.
assert!(mock.last_sent().get("sessionId").is_none());
mock.reply(id, json!({})).await;
fut.await.unwrap().unwrap();
conn.shutdown();
}
#[tokio::test]
async fn screenshot_sends_page_capturescreenshot_without_clip_and_decodes_base64() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.screenshot().await }
});
let id = mock.expect_cmd("Page.captureScreenshot").await;
let sent = mock.last_sent();
assert_eq!(sent["params"]["format"], "png");
// Tab::screenshot must NOT pass a clip — that's Element::screenshot.
assert!(sent["params"].get("clip").is_none());
// "PNG!" → b"PNG!" once base64-decoded.
mock.reply(id, json!({ "data": "UE5HIQ==" })).await;
let bytes = fut.await.unwrap().unwrap();
assert_eq!(bytes, b"PNG!");
conn.shutdown();
}
#[tokio::test]
async fn title_returns_string_from_target_info() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.title().await }
});
let id = mock.expect_cmd("Target.getTargetInfo").await;
mock.reply(
id,
json!({ "targetInfo": { "url": "https://x", "title": "Hello" } }),
)
.await;
let s = fut.await.unwrap().unwrap();
assert_eq!(s, "Hello");
conn.shutdown();
}
// --- nav history: back / forward / reload --------------------------
#[tokio::test]
async fn back_dispatches_navigate_to_history_entry_at_prev_index() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.back().await }
});
let id_hist = mock.expect_cmd("Page.getNavigationHistory").await;
mock.reply(
id_hist,
json!({
"currentIndex": 1,
"entries": [
{ "id": 10, "url": "https://a.test" },
{ "id": 11, "url": "https://b.test" },
],
}),
)
.await;
let id_nav = mock.expect_cmd("Page.navigateToHistoryEntry").await;
// Should target the entry at currentIndex - 1 (id=10).
assert_eq!(mock.last_sent()["params"]["entryId"], 10);
mock.reply(id_nav, json!({})).await;
fut.await.unwrap().unwrap();
conn.shutdown();
}
#[tokio::test]
async fn back_errors_when_current_index_is_zero() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.back().await }
});
let id_hist = mock.expect_cmd("Page.getNavigationHistory").await;
mock.reply(
id_hist,
json!({
"currentIndex": 0,
"entries": [{ "id": 10, "url": "https://a.test" }],
}),
)
.await;
let res = fut.await.unwrap();
match res {
Err(ZendriverError::HistoryNavigation(m)) => assert!(m.contains("no back history")),
other => panic!("unexpected: {other:?}"),
}
conn.shutdown();
}
#[tokio::test]
async fn reload_dispatches_page_reload_with_ignore_cache_false() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let fut = tokio::spawn({
let t = tab.clone();
async move { t.reload().await }
});
let id = mock.expect_cmd("Page.reload").await;
assert_eq!(mock.last_sent()["params"]["ignoreCache"], false);
mock.reply(id, json!({})).await;
fut.await.unwrap().unwrap();
conn.shutdown();
}
// --- Tab::cookies (P4 T10) ----------------------------------------
/// [`Tab::cookies`] returns a [`crate::CookieJar`] bound to the owning
/// browser's root connection — discovered via the cached `Weak<BrowserInner>`
/// upgrade. The test builds a synthetic `BrowserInner` with a known
/// connection, attaches a Tab whose Weak ref points at it, and asserts
/// that calling `.set(...)` dispatches `Storage.setCookies` on that
/// browser-level connection (not the Tab's session channel).
#[tokio::test]
async fn tab_cookies_dispatches_through_browser_connection_via_weak_upgrade() {
use crate::browser::BrowserInner;
use crate::cookies::Cookie;
use std::collections::HashMap;
use std::sync::{Arc, Weak};
let input_profile = zendriver_stealth::InputProfile::native();
let (mut mock, conn) = MockConnection::pair();
let inner = Arc::new_cyclic(|weak: &Weak<BrowserInner>| {
let main_session = SessionHandle::new(conn.clone(), "S1");
let main_input = crate::input::InputController::new(input_profile.clone());
let main_tab = Tab::new(main_session, weak.clone(), main_input, "T1".to_string());
let mut map = HashMap::new();
map.insert("S1".to_string(), main_tab.clone());
BrowserInner {
conn: conn.clone(),
main_tab,
child: tokio::sync::Mutex::new(None),
_user_data: None,
stealth_input_profile: input_profile.clone(),
tabs: tokio::sync::RwLock::new(map),
tabs_changed: tokio::sync::Notify::new(),
#[cfg(feature = "interception")]
proxy_auth_handle: std::sync::OnceLock::new(),
}
});
let tab = inner.main_tab.clone();
let jar = tab.cookies();
let fut = tokio::spawn(async move {
jar.set(Cookie {
name: "sid".into(),
value: "abc".into(),
domain: ".example.com".into(),
path: "/".into(),
expires: None,
http_only: false,
secure: false,
same_site: None,
url: None,
})
.await
});
let id = mock.expect_cmd("Storage.setCookies").await;
let params = &mock.last_sent()["params"];
let arr = params["cookies"]
.as_array()
.expect("setCookies payload must carry a cookies array");
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["name"], "sid");
// Browser-scope command — no session_id (jar dispatches against
// the browser's connection, not the tab's session).
assert!(mock.last_sent().get("sessionId").is_none());
mock.reply(id, json!({})).await;
fut.await.unwrap().unwrap();
// Keep `inner` alive until after the dispatch so the Weak upgrade
// succeeds — that's the path under test.
drop(inner);
conn.shutdown();
}
// --- frame lifecycle subscriber (P4 T15) ---------------------------
/// End-to-end: emit `Page.frameAttached` for a new same-origin
/// sub-frame; `tab.frames()` should expose it. Then emit
/// `Page.frameDetached` for the same `frameId` and assert the
/// registry shrinks back to empty.
///
/// Mirrors the [`InFlightTracker`] test pattern — synchronize on the
/// subscriber's outbound `Page.enable` call before driving events,
/// then poll the registry shape (the lifecycle task processes events
/// asynchronously).
#[tokio::test]
async fn frame_lifecycle_attach_then_detach_round_trip() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
// Synchronize: wait until the background lifecycle task has run
// far enough to issue `Page.enable`. Once that command lands in
// the mock's outbound queue, the three `Page.frame*` subscriptions
// are already registered, so any subsequent
// `emit_event_for_session` will be routed to them.
let id_enable =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Page.enable"))
.await
.expect("frame lifecycle did not send Page.enable within 2s");
mock.reply(id_enable, json!({})).await;
// Emit a Page.frameAttached event for a child frame.
mock.emit_event_for_session(
"Page.frameAttached",
json!({
"frameId": "FCHILD",
"parentFrameId": "FROOT",
}),
"S1",
)
.await;
// Poll until the subscriber processes the event (async).
for _ in 0..50 {
if !tab.inner.frames.read().await.is_empty() {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
let frames = tab.frames().await.unwrap();
assert_eq!(frames.len(), 1, "expected one frame after attach event");
let attached = &frames[0];
assert_eq!(attached.id(), "FCHILD");
assert_eq!(attached.parent_id(), Some("FROOT"));
assert!(!attached.is_main());
// Emit a Page.frameDetached for the same frame.
mock.emit_event_for_session("Page.frameDetached", json!({ "frameId": "FCHILD" }), "S1")
.await;
for _ in 0..50 {
if tab.inner.frames.read().await.is_empty() {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
let frames_after = tab.frames().await.unwrap();
assert!(
frames_after.is_empty(),
"expected registry to drain after detach event",
);
conn.shutdown();
}
// --- wait_for_idle quiet-window enforcement ------------------------
/// End-to-end: emit a `Network.requestWillBeSent` event, then 100ms
/// later emit `Network.responseReceived` for the same id. With a 500ms
/// quiet window + 2s outer timeout, `wait_for_idle_with` should
/// resolve `Ok(())` within ~600ms of the response (500ms quiet +
/// scheduling slack). Asserts the call returns within 1.5s of the
/// response event — a generous bound that still rejects "never
/// resolves" without flaking on a loaded CI machine.
#[tokio::test]
async fn wait_for_idle_resolves_after_quiet_window_post_response() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
// Synchronize: wait until the background tracker task has run far
// enough to issue `Network.enable`. Once that command lands in the
// mock's outbound queue, the subscriptions are already registered
// (created in `InFlightTracker::run` before the enable spawn) — so
// any subsequent `emit_event_for_session` will be routed to them.
let id_enable =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Network.enable"))
.await
.expect("tracker did not send Network.enable within 2s");
mock.reply(id_enable, json!({})).await;
// Insert via requestWillBeSent.
mock.emit_event_for_session(
"Network.requestWillBeSent",
json!({ "requestId": "R1" }),
"S1",
)
.await;
// Wait for the tracker to actually observe the insert before
// starting the wait — otherwise wait_for_idle could see an empty
// set on its first poll and resolve immediately.
for _ in 0..50 {
if tab.inner.network_tracker.in_flight.lock().await.len() == 1 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(
tab.inner.network_tracker.in_flight.lock().await.len(),
1,
"request did not register before wait_for_idle starts",
);
let fut = tokio::spawn({
let t = tab.clone();
async move {
t.wait_for_idle_with(Duration::from_secs(2), Duration::from_millis(500))
.await
}
});
// Hold the request in-flight briefly, then close it. After this
// emit, the tracker drains to empty and the 500ms quiet window
// starts ticking.
tokio::time::sleep(Duration::from_millis(100)).await;
let response_at = tokio::time::Instant::now();
mock.emit_event_for_session(
"Network.responseReceived",
json!({ "requestId": "R1" }),
"S1",
)
.await;
let res = tokio::time::timeout(Duration::from_millis(1500), fut)
.await
.expect("wait_for_idle did not resolve within 1500ms after response");
res.unwrap().unwrap();
let elapsed = response_at.elapsed();
// 500ms quiet window + slack; must be at least 500ms.
assert!(
elapsed >= Duration::from_millis(450),
"resolved too early ({elapsed:?}) — quiet window not enforced",
);
assert!(
elapsed < Duration::from_millis(1500),
"resolved too late ({elapsed:?})",
);
conn.shutdown();
}
/// Regression: the in-flight set going 1 → 0 → 1 within the quiet
/// window must NOT cause `wait_for_idle_with` to resolve early. The
/// quiet window measures sustained idleness, not a single
/// instantaneous touch-of-zero.
///
/// Sequence:
/// 1. R1 starts (in_flight = 1).
/// 2. R1 completes (in_flight = 0). Quiet window starts.
/// 3. ~100ms later, well inside the 200ms quiet window, R2 starts
/// (in_flight = 1). Quiet window MUST reset to `None`.
/// 4. R2 completes (in_flight = 0). New quiet window starts.
/// 5. `wait_for_idle_with` resolves only after R2's quiet window
/// closes.
///
/// Assertion: elapsed time from R1's response (step 2) to the future
/// resolving is at least `delay-between-completions (~100ms) +
/// quiet_window (200ms)`. A buggy implementation that ignored the
/// in-window R2 burst would resolve at ~200ms and fail the lower
/// bound.
#[tokio::test]
async fn wait_for_idle_does_not_return_early_if_new_request_arrives_in_quiet_window() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let id_enable =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Network.enable"))
.await
.expect("tracker did not send Network.enable within 2s");
mock.reply(id_enable, json!({})).await;
// Insert R1 and wait for the tracker to observe it before
// starting wait_for_idle (mirrors the sibling test).
mock.emit_event_for_session(
"Network.requestWillBeSent",
json!({ "requestId": "R1" }),
"S1",
)
.await;
for _ in 0..50 {
if tab.inner.network_tracker.in_flight.lock().await.len() == 1 {
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert_eq!(
tab.inner.network_tracker.in_flight.lock().await.len(),
1,
"R1 did not register before wait_for_idle starts",
);
let fut = tokio::spawn({
let t = tab.clone();
async move {
// 5s outer timeout: plenty of headroom for the worst-case
// scheduling on a loaded CI. 200ms quiet window: small
// enough that the test finishes fast, large enough that
// the 100ms gap fits comfortably inside it.
t.wait_for_idle_with(Duration::from_secs(5), Duration::from_millis(200))
.await
}
});
// Drain R1 — quiet window opens here.
tokio::time::sleep(Duration::from_millis(50)).await;
let r1_response_at = tokio::time::Instant::now();
mock.emit_event_for_session(
"Network.responseReceived",
json!({ "requestId": "R1" }),
"S1",
)
.await;
// ~100ms later (still inside the 200ms quiet window), insert R2.
// A correct implementation resets quiet_start; a buggy one would
// already be near the 200ms threshold and resolve any moment.
tokio::time::sleep(Duration::from_millis(100)).await;
mock.emit_event_for_session(
"Network.requestWillBeSent",
json!({ "requestId": "R2" }),
"S1",
)
.await;
// Wait for the tracker to actually observe the insert before
// closing it — otherwise R2 could complete before the tracker
// even noticed it started, defeating the test.
for _ in 0..50 {
if tab
.inner
.network_tracker
.in_flight
.lock()
.await
.contains("R2")
{
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert!(
tab.inner
.network_tracker
.in_flight
.lock()
.await
.contains("R2"),
"R2 did not register inside quiet window",
);
// Hold R2 in-flight briefly, then close it. A new quiet window
// starts from this point — wait_for_idle must wait it out.
tokio::time::sleep(Duration::from_millis(50)).await;
mock.emit_event_for_session(
"Network.responseReceived",
json!({ "requestId": "R2" }),
"S1",
)
.await;
let res = tokio::time::timeout(Duration::from_secs(2), fut)
.await
.expect("wait_for_idle did not resolve within 2s after R2 completed");
res.unwrap().unwrap();
let total_elapsed = r1_response_at.elapsed();
// Lower bound: R1-response (T0) → 100ms gap → R2 starts → 50ms
// hold → R2 response → 200ms quiet window → resolve. Total ≥
// 350ms. A bug that ignored R2's in-window arrival would resolve
// at T0 + 200ms = 200ms.
assert!(
total_elapsed >= Duration::from_millis(330),
"wait_for_idle resolved too early ({total_elapsed:?}); R2 inside quiet \
window must have reset quiet_start, requiring a fresh post-R2 quiet \
window before resolving",
);
conn.shutdown();
}
/// Regression for the "burst within tick" race: a request that fires
/// *and finishes* inside one 50ms poll tick takes the in-flight set
/// 0 → 1 → 0 without ever being observed at a >0 read. A naive
/// implementation that only checks the set len would see sustained
/// 0 and resolve early. The fix arms a `Notify::notified()` future
/// before each count read so the two membership events from the burst
/// both hit an armed waker; on the next iteration we wake via the
/// notifier arm and reset `quiet_start`.
#[tokio::test]
async fn wait_for_idle_burst_inside_tick_resets_quiet_window() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let id_enable =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Network.enable"))
.await
.expect("tracker did not send Network.enable within 2s");
mock.reply(id_enable, json!({})).await;
// Start with the set already at 0 — wait_for_idle should accumulate
// a quiet window from T0. The inner `wait_for_idle_with` timeout is
// deliberately generous (30s) so a slow / loaded CI runner doesn't
// flake the test; the correctness assertion below uses a strict
// lower bound on `elapsed` and an outer 10s tokio::time::timeout to
// catch a too-early resolve or a hang.
let fut = tokio::spawn({
let t = tab.clone();
async move {
t.wait_for_idle_with(Duration::from_secs(30), Duration::from_millis(300))
.await
}
});
let started_at = tokio::time::Instant::now();
// Let the wait_for_idle loop tick once on an empty set so
// `quiet_start` is firmly armed.
tokio::time::sleep(Duration::from_millis(75)).await;
// Burst: fire R-burst's requestWillBeSent + responseReceived
// back-to-back. The tracker should observe both transitions and
// notify on each; the wait_for_idle loop should wake on the notif
// arm and reset quiet_start even though the set's instantaneous
// value returns to 0.
mock.emit_event_for_session(
"Network.requestWillBeSent",
json!({ "requestId": "Rburst" }),
"S1",
)
.await;
mock.emit_event_for_session(
"Network.responseReceived",
json!({ "requestId": "Rburst" }),
"S1",
)
.await;
// Outer timeout deliberately generous (10s) so a slow / loaded
// CI runner doesn't flake the test. The correctness assertion
// below uses a strict lower bound on `elapsed` to catch a
// too-early resolve regardless of how long the slack window is.
let res = tokio::time::timeout(Duration::from_secs(10), fut)
.await
.expect("wait_for_idle did not resolve within 10s");
res.unwrap().unwrap();
// Lower bound: 75ms initial sleep + 300ms quiet window after the
// burst's last notification = 375ms. A bug that ignored the burst
// would resolve at started_at + 300ms = 300ms.
let elapsed = started_at.elapsed();
assert!(
elapsed >= Duration::from_millis(355),
"wait_for_idle resolved too early ({elapsed:?}); 0→1→0 burst inside \
quiet window must reset quiet_start",
);
conn.shutdown();
}
// --- Tab::intercept (P5 T7, feature = "interception") -------------
/// `tab.intercept().block("*").start()` should spawn the rule actor on
/// the tab's session: assert `Fetch.enable` lands and a matching
/// `Fetch.requestPaused` triggers `Fetch.failRequest`. Verifies the
/// `Tab::intercept` shim plumbs into `InterceptBuilder` end-to-end.
#[cfg(feature = "interception")]
#[tokio::test]
async fn intercept_block_all_dispatches_fail_request_via_tab_shim() {
let (mut mock, conn) = MockConnection::pair();
let sess = SessionHandle::new(conn.clone(), "S1");
let tab = Tab::new_for_test(sess);
let handle = tab.intercept().block("*").unwrap().start();
// Side-task `Fetch.enable` must land first; default match-all
// pattern is injected when none was registered explicitly.
let enable_id =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.enable"))
.await
.expect("intercept did not send Fetch.enable within 2s");
let enable_params = mock.last_sent()["params"].clone();
assert_eq!(enable_params["handleAuthRequests"], false);
assert_eq!(enable_params["patterns"][0]["urlPattern"], "*");
mock.reply(enable_id, json!({})).await;
// Any paused URL matches the `block("*")` rule.
mock.emit_event_for_session(
"Fetch.requestPaused",
json!({
"requestId": "REQ-1",
"request": {
"url": "https://any.test/whatever",
"method": "GET",
"headers": {},
},
"resourceType": "Document",
}),
"S1",
)
.await;
let fail_id =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.failRequest"))
.await
.expect("actor did not send Fetch.failRequest within 2s");
let fail_params = mock.last_sent()["params"].clone();
assert_eq!(fail_params["requestId"], "REQ-1");
assert_eq!(fail_params["errorReason"], "BlockedByClient");
mock.reply(fail_id, json!({})).await;
let stop_fut = tokio::spawn(handle.stop());
let disable_id =
tokio::time::timeout(Duration::from_secs(2), mock.expect_cmd("Fetch.disable"))
.await
.expect("actor did not send Fetch.disable on stop()");
mock.reply(disable_id, json!({})).await;
stop_fut
.await
.expect("stop() task panicked")
.expect("stop() returned Err");
conn.shutdown();
}
}