eoka 0.6.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use std::collections::{HashMap, HashSet};

use serde::Deserialize;
use serde_json::{json, Value};

use super::{FrameInfo, Page};
use crate::cdp::{Frame, FrameTree, RuntimeEvaluateResult, Session, TargetGetTargets};
use crate::error::{Error, Result};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Targets {
    target_infos: Vec<FrameTarget>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct FrameTarget {
    target_id: String,
    r#type: String,
    url: String,
    parent_frame_id: Option<String>,
}

fn frame_discovery_raced(error: &Error) -> bool {
    matches!(error, Error::Cdp { message, .. } if [
        "No target with given id found", "Session with given id not found",
        "Not attached to an active page", "No frame with given id found",
    ].iter().any(|text| message.contains(text)))
}

struct AttachedFrame(Session);

impl Drop for AttachedFrame {
    fn drop(&mut self) {
        let session = self.0.clone();
        if let Ok(runtime) = tokio::runtime::Handle::try_current() {
            runtime.spawn(async move {
                if let Err(error) = session.detach().await {
                    tracing::debug!("Frame session detach failed: {error}");
                }
            });
        }
    }
}

struct FrameObjects {
    session: Session,
    group: String,
    active: bool,
}

impl FrameObjects {
    fn new(session: Session) -> Self {
        static NEXT_GROUP: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
        Self {
            session,
            group: format!(
                "eoka-frame-objects-{}",
                NEXT_GROUP.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            ),
            active: true,
        }
    }

    async fn release(mut self) -> Result<()> {
        release_frame_objects(&self.session, &self.group).await?;
        self.active = false;
        Ok(())
    }
}

async fn release_frame_objects(session: &Session, group: &str) -> Result<()> {
    tokio::time::timeout(
        std::time::Duration::from_secs(2),
        session.send::<_, Value>("Runtime.releaseObjectGroup", &json!({"objectGroup":group})),
    )
    .await
    .map_err(|_| Error::cdp_msg("Frame object cleanup timed out"))??;
    Ok(())
}

impl Drop for FrameObjects {
    fn drop(&mut self) {
        if self.active {
            let session = self.session.clone();
            let group = self.group.clone();
            if let Ok(runtime) = tokio::runtime::Handle::try_current() {
                runtime.spawn(async move {
                    if release_frame_objects(&session, &group).await.is_err() {
                        tracing::debug!("Frame object cleanup unconfirmed");
                    }
                });
            }
        }
    }
}

struct Entry {
    info: FrameInfo,
    parent: Option<String>,
    session: Session,
}

struct Snapshot {
    entries: HashMap<String, Entry>,
    order: Vec<String>,
    attachments: Vec<AttachedFrame>,
}

impl Snapshot {
    fn get(&self, id: &str) -> Result<&Entry> {
        self.entries
            .get(id)
            .ok_or_else(|| Error::cdp_msg(format!("Frame not in this page: {id}")))
    }

    async fn capture(page: &Page) -> Result<Self> {
        for attempt in 0..3 {
            match Self::capture_once(page).await {
                Err(error) if frame_discovery_raced(&error) && attempt < 2 => {
                    tokio::time::sleep(std::time::Duration::from_millis(20)).await;
                }
                result => return result,
            }
        }
        unreachable!()
    }

    async fn capture_once(page: &Page) -> Result<Self> {
        let targets: Targets = page
            .session
            .transport()
            .send("Target.getTargets", &TargetGetTargets {})
            .await?;
        let targets: Vec<_> = targets
            .target_infos
            .into_iter()
            .filter(|t| t.r#type == "iframe")
            .collect();
        let iframe_targets: HashSet<_> = targets.iter().map(|t| t.target_id.as_str()).collect();
        let root = page.session.get_frame_tree().await?;
        let mut stack = vec![(root, None, page.session.clone())];
        let mut snapshot = Self {
            entries: HashMap::new(),
            order: Vec::new(),
            attachments: Vec::new(),
        };
        while let Some((mut tree, parent, mut session)) = stack.pop() {
            let id = tree.frame.id.clone();
            if id.is_empty() || snapshot.entries.contains_key(&id) {
                return Err(Error::cdp_msg("Invalid or duplicate frame in tree"));
            }
            if iframe_targets.contains(id.as_str()) && session.target_id() != id {
                session = page.session.attach_frame_target(&id).await?;
                snapshot.attachments.push(AttachedFrame(session.clone()));
                tree = session.get_frame_tree().await?;
                if tree.frame.id != id {
                    return Err(Error::cdp_msg("Frame target changed while attaching"));
                }
            }
            for target in targets
                .iter()
                .filter(|t| t.parent_frame_id.as_deref() == Some(id.as_str()))
            {
                if !tree
                    .child_frames
                    .iter()
                    .any(|child| child.frame.id == target.target_id)
                {
                    tree.child_frames.push(FrameTree {
                        frame: Frame {
                            id: target.target_id.clone(),
                            name: None,
                            url: target.url.clone(),
                        },
                        child_frames: Vec::new(),
                    });
                }
            }
            for child in tree.child_frames.into_iter().rev() {
                stack.push((child, Some(id.clone()), session.clone()));
            }
            snapshot.order.push(id.clone());
            snapshot.entries.insert(
                id.clone(),
                Entry {
                    info: FrameInfo {
                        id,
                        url: tree.frame.url,
                        name: tree.frame.name,
                    },
                    parent,
                    session,
                },
            );
        }
        Ok(snapshot)
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Owner {
    backend_node_id: i64,
}

#[derive(Deserialize)]
struct Geometry {
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    scale_x: f64,
    scale_y: f64,
    unsupported: bool,
}

const OWNER_GEOMETRY: &str = r#"function() {
    let unsupported = typeof this.checkVisibility !== 'function' || !this.checkVisibility({checkOpacity:true,checkVisibilityCSS:true,contentVisibilityAuto:true});
    let depth = 0;
    for (let e = this; e; e = e.assignedSlot || e.parentElement || e.getRootNode().host) {
        if (++depth > 64) { unsupported = true; break; }
        const s = getComputedStyle(e);
        if (s.transform !== 'none') {
            const m = new DOMMatrixReadOnly(s.transform);
            if (!m.is2D || Math.abs(m.b) > 1e-8 || Math.abs(m.c) > 1e-8 || m.a <= 0 || m.d <= 0) unsupported = true;
        }
        if (s.scale && s.scale !== 'none' && s.scale.split(/\s+/).some(v => !(parseFloat(v) > 0))) unsupported = true;
        if (s.perspective !== 'none' || (s.rotate && s.rotate !== 'none' && s.rotate !== '0deg') ||
            s.visibility !== 'visible' || s.display === 'none' || Number(s.opacity) === 0 ||
            s.contentVisibility === 'hidden') unsupported = true;
    }
    const s = getComputedStyle(this), r = this.getBoundingClientRect();
    const n = v => parseFloat(v) || 0;
    const pl=n(s.paddingLeft), pr=n(s.paddingRight), pt=n(s.paddingTop), pb=n(s.paddingBottom);
    const bl=n(s.borderLeftWidth), br=n(s.borderRightWidth), bt=n(s.borderTopWidth), bb=n(s.borderBottomWidth);
    const bw=n(s.width)+(s.boxSizing==='border-box'?0:pl+pr+bl+br);
    const bh=n(s.height)+(s.boxSizing==='border-box'?0:pt+pb+bt+bb);
    const sx=bw>0?r.width/bw:0, sy=bh>0?r.height/bh:0;
    return {x:r.left+(bl+pl)*sx, y:r.top+(bt+pt)*sy,
        width:bw-pl-pr-bl-br, height:bh-pt-pb-bt-bb, scale_x:sx, scale_y:sy,
        unsupported:unsupported || !this.getClientRects().length};
}"#;

const OWNER_HIT_TEST: &str = r#"function(x, y) {
    let hit = this.ownerDocument.elementFromPoint(x, y);
    for (let depth = 0; hit?.shadowRoot && depth < 64; depth++) {
        const inner = hit.shadowRoot.elementFromPoint(x, y);
        if (inner === hit) break;
        hit = inner;
    }
    return hit === this;
}"#;

impl Page {
    pub(crate) async fn frame_target_id(&self, frame_id: &str) -> Result<String> {
        let snapshot = Snapshot::capture(self).await?;
        Ok(snapshot.get(frame_id)?.session.target_id().to_owned())
    }

    /// Convert frame-local CSS coordinates to top-level viewport coordinates.
    /// Accounts for borders, padding, scrolling, positive axis-aligned scale
    /// and translation. Unsupported owner transforms and hidden owners fail,
    /// including reflection through closed shadow slots. Points outside any
    /// ancestor viewport are rejected. This is not an overlay hit-test guarantee.
    pub async fn frame_point_to_viewport(
        &self,
        frame_id: &str,
        x: f64,
        y: f64,
    ) -> Result<(f64, f64)> {
        self.map_frame_point(frame_id, x, y, false).await
    }

    /// Map a frame-local input point to the root viewport, rejecting covered iframe owners.
    /// Each parent document must hit its owning iframe at the mapped point.
    /// Includes the geometry checks of [`Page::frame_point_to_viewport`].
    /// Does not hit-test a leaf element or prevent mutations after this snapshot;
    /// callers must validate their target and dispatch input without changing the point.
    /// Inaccessible shadow-root hit paths fail closed.
    pub async fn frame_point_for_input(
        &self,
        frame_id: &str,
        x: f64,
        y: f64,
    ) -> Result<(f64, f64)> {
        self.map_frame_point(frame_id, x, y, true).await
    }

    async fn map_frame_point(
        &self,
        frame_id: &str,
        x: f64,
        y: f64,
        hit_test: bool,
    ) -> Result<(f64, f64)> {
        if !x.is_finite() || !y.is_finite() {
            return Err(Error::cdp_msg("Frame coordinates must be finite"));
        }
        let snapshot = Snapshot::capture(self).await?;
        let mut id = frame_id;
        let (mut x, mut y) = (x, y);
        loop {
            let entry = snapshot.get(id)?;
            let context = entry.session.create_isolated_world(id).await?;
            let size: [f64; 2] = self.eval_impl(
                entry
                    .session
                    .evaluate_in_context("[innerWidth, innerHeight]", context)
                    .await?,
            )?;
            if x < 0.0 || y < 0.0 || x >= size[0] || y >= size[1] {
                return Err(Error::cdp_msg(
                    "Frame point is outside a viewport in its ancestor chain",
                ));
            }
            let Some(parent_id) = entry.parent.as_deref() else {
                return Ok((x, y));
            };
            let parent = snapshot.get(parent_id)?;
            let owner: Owner = parent
                .session
                .send("DOM.getFrameOwner", &json!({"frameId": id}))
                .await?;
            let parent_context = parent.session.create_isolated_world(parent_id).await?;
            let objects = FrameObjects::new(parent.session.clone());
            let resolved: Value = parent.session.send("DOM.resolveNode", &json!({
                "backendNodeId": owner.backend_node_id, "executionContextId": parent_context,
                "objectGroup":objects.group,
            })).await?;
            let object_id = resolved["object"]["objectId"]
                .as_str()
                .ok_or_else(|| Error::cdp_msg("Frame owner could not be resolved"))?;
            let model: Value = parent
                .session
                .send("DOM.getBoxModel", &json!({"objectId":object_id}))
                .await?;
            let quad: [f64; 8] = serde_json::from_value(model["model"]["content"].clone())?;
            if quad.iter().any(|v| !v.is_finite())
                || quad[2] <= quad[0]
                || quad[5] <= quad[3]
                || (quad[1] - quad[3]).abs() > 1e-6
                || (quad[2] - quad[4]).abs() > 1e-6
                || (quad[5] - quad[7]).abs() > 1e-6
                || (quad[6] - quad[0]).abs() > 1e-6
            {
                return Err(Error::cdp_msg("Unsupported rendered frame owner transform"));
            }
            let result: Result<RuntimeEvaluateResult> = parent
                .session
                .send(
                    "Runtime.callFunctionOn",
                    &json!({
                        "objectId": object_id, "functionDeclaration": OWNER_GEOMETRY,
                        "returnByValue": true,
                    }),
                )
                .await;
            let geometry: Geometry = self.eval_impl(result?)?;
            if geometry.unsupported
                || geometry.width <= 0.0
                || geometry.height <= 0.0
                || !geometry.scale_x.is_finite()
                || !geometry.scale_y.is_finite()
                || geometry.scale_x <= 0.0
                || geometry.scale_y <= 0.0
            {
                return Err(Error::cdp_msg(
                    "Unsupported transformed or hidden frame owner",
                ));
            }
            if (geometry.width - size[0]).abs() > 1.0 || (geometry.height - size[1]).abs() > 1.0 {
                return Err(Error::cdp_msg("Frame owner and viewport dimensions differ"));
            }
            x = geometry.x + x * geometry.scale_x;
            y = geometry.y + y * geometry.scale_y;
            if hit_test {
                let result: RuntimeEvaluateResult = parent
                    .session
                    .send(
                        "Runtime.callFunctionOn",
                        &json!({
                            "objectId": object_id,
                            "functionDeclaration": OWNER_HIT_TEST,
                            "arguments":[{"value":x},{"value":y}],
                            "returnByValue":true,
                        }),
                    )
                    .await?;
                if !self.eval_impl::<bool>(result)? {
                    return Err(Error::cdp_msg(
                        "Frame input point is covered by another element",
                    ));
                }
            }
            objects.release().await?;
            id = parent_id;
        }
    }

    /// Get this page's frame tree, including nested out-of-process iframe targets.
    pub async fn frames(&self) -> Result<Vec<FrameInfo>> {
        self.nested_frame_infos().await
    }

    /// Evaluate JavaScript in an iframe selected by CSS in this page's top document.
    ///
    /// The selector must match exactly one `iframe` or `frame` element. Frame URLs
    /// and numeric indices are not selectors; use [`Page::evaluate_in_frame_id`]
    /// with IDs from [`Page::frames`] for nested frames or explicit selection.
    /// Cross-origin/OOPIF frames are routed through CDP, in an isolated world:
    /// DOM access works, but page-owned JavaScript globals are not available.
    /// No parent-to-child `contentWindow` access or `Function` constructor is used.
    ///
    /// `expression` is executed as code. Do not supply untrusted code.
    pub async fn evaluate_in_frame<T: serde::de::DeserializeOwned>(
        &self,
        frame_selector: &str,
        expression: &str,
    ) -> Result<T> {
        if frame_selector.len() > 4096 {
            return Err(Error::cdp_msg("Frame selector exceeds 4096 bytes"));
        }
        let root = self.session.get_frame_tree().await?.frame.id;
        let context = self.session.create_isolated_world(&root).await?;
        let objects = FrameObjects::new(self.session.clone());
        let selector = serde_json::to_string(frame_selector)?;
        let selection = format!("(() => {{ const matches=document.querySelectorAll({selector}); if(matches.length!==1 || !['iframe','frame'].includes(matches[0].localName)) throw new Error('Frame selector must match exactly one iframe or frame element'); return matches[0]; }})()");
        let remote: RuntimeEvaluateResult = self
            .session
            .send(
                "Runtime.evaluate",
                &json!({
                    "expression":selection, "contextId":context, "objectGroup":objects.group,
                    "returnByValue":false, "awaitPromise":false,
                }),
            )
            .await?;
        let object_id = self
            .check_js_result(remote)?
            .object_id
            .ok_or_else(|| Error::cdp_msg("Frame element unavailable"))?;
        let described: Value = self
            .session
            .send("DOM.describeNode", &json!({"objectId":object_id}))
            .await?;
        let frame_id = described["node"]["frameId"]
            .as_str()
            .ok_or_else(|| Error::cdp_msg("Selected frame has no attached document"))?;
        objects.release().await?;
        self.evaluate_in_frame_id(frame_id, expression).await
    }

    /// Return snapshot-time ancestor IDs, from the immediate parent to this page's root.
    ///
    /// The queried frame is excluded; the root has no ancestors. Stale or unrelated
    /// IDs are rejected. IDs describe this snapshot, not durable navigation identity.
    pub async fn frame_ancestor_ids(&self, frame_id: &str) -> Result<Vec<String>> {
        let snapshot = Snapshot::capture(self).await?;
        let mut entry = snapshot.get(frame_id)?;
        let mut seen = HashSet::from([frame_id.to_string()]);
        let mut ancestors = Vec::new();
        while let Some(parent) = &entry.parent {
            if !seen.insert(parent.clone()) {
                return Err(Error::cdp_msg("Cycle in frame ancestry"));
            }
            ancestors.push(parent.clone());
            entry = snapshot.get(parent)?;
        }
        Ok(ancestors)
    }

    /// Return the ordered rendered content quad of one frame-local element.
    ///
    /// Selects the zero-based `index` among at most 64 CSS selector matches in
    /// this page's frame, including OOPIFs. Index must be below 64 and selectors
    /// at most 4096 bytes. Missing, detached, fragmented or degenerate boxes fail.
    /// The vertices retain content corner order under transforms, including
    /// open/closed shadow-slot ancestry; they are not a normalized bounding box.
    /// Coordinates belong to the owning CDP session's viewport, not necessarily
    /// the selected frame or root page. This is snapshot geometry, not durable
    /// element identity or a visibility/hit-testing guarantee.
    /// Temporary objects are group-released on completion; errors/cancellation
    /// schedule bounded best-effort release, including a lost evaluation reply.
    pub async fn frame_element_content_quad(
        &self,
        frame_id: &str,
        selector: &str,
        index: usize,
    ) -> Result<[f64; 8]> {
        if index >= 64 || selector.len() > 4096 {
            return Err(Error::cdp_msg("Frame quad selection limit exceeded"));
        }
        let snapshot = Snapshot::capture(self).await?;
        let entry = snapshot.get(frame_id)?;
        let context = entry.session.create_isolated_world(frame_id).await?;
        let objects = FrameObjects::new(entry.session.clone());
        let selector = serde_json::to_string(selector)?;
        let expression = format!("(() => {{ const matches=document.querySelectorAll({selector}); if(matches.length>64 || !matches[{index}] || matches[{index}].getClientRects().length!==1) throw new Error('Unsupported frame quad selection'); return matches[{index}]; }})()");
        let remote: RuntimeEvaluateResult = entry
            .session
            .send(
                "Runtime.evaluate",
                &json!({
                    "expression":expression, "contextId":context, "objectGroup":objects.group,
                    "returnByValue":false, "awaitPromise":false,
                }),
            )
            .await?;
        let object_id = self
            .check_js_result(remote)?
            .object_id
            .ok_or_else(|| Error::cdp_msg("Frame quad element unavailable"))?;
        let model: Value = entry
            .session
            .send("DOM.getBoxModel", &json!({"objectId":object_id}))
            .await?;
        let quad: [f64; 8] = serde_json::from_value(model["model"]["content"].clone())?;
        let area = (0..4)
            .map(|i| {
                let next = (i + 1) % 4;
                quad[i * 2] * quad[next * 2 + 1] - quad[next * 2] * quad[i * 2 + 1]
            })
            .sum::<f64>();
        if quad.iter().any(|v| !v.is_finite()) || !area.is_finite() || area.abs() < 1e-8 {
            return Err(Error::cdp_msg("Degenerate frame content quad"));
        }
        objects.release().await?;
        Ok(quad)
    }

    pub(super) async fn nested_frame_infos(&self) -> Result<Vec<FrameInfo>> {
        let snapshot = Snapshot::capture(self).await?;
        Ok(snapshot
            .order
            .iter()
            .map(|id| snapshot.entries[id].info.clone())
            .collect())
    }

    /// Evaluate in a frame belonging to this page, including nested OOPIFs.
    /// Frame IDs come from `frames()`. Detached/unrelated IDs are rejected.
    /// Uses an isolated world, not the page's main-world JavaScript globals.
    /// No Runtime.enable or cross-origin DOM access is required.
    pub async fn evaluate_in_frame_id<T: serde::de::DeserializeOwned>(
        &self,
        frame_id: &str,
        expression: &str,
    ) -> Result<T> {
        let snapshot = Snapshot::capture(self).await?;
        let entry = snapshot.get(frame_id)?;
        let context = entry.session.create_isolated_world(frame_id).await?;
        self.eval_impl(
            entry
                .session
                .evaluate_in_context(expression, context)
                .await?,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn frame_objects_release_on_success_error_and_cancelled_replies() {
        use futures_util::{SinkExt, StreamExt};
        use std::sync::{Arc, Mutex};
        use tokio_tungstenite::tungstenite::Message;
        for mode in ["normal", "error", "cancel-evaluate", "cancel-box"] {
            let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
            let url = format!("ws://{}", listener.local_addr().unwrap());
            let observed = Arc::new(Mutex::new((String::new(), Vec::new())));
            let released = Arc::new(tokio::sync::Notify::new());
            let stalled = Arc::new(tokio::sync::Notify::new());
            let (seen, done, paused) = (observed.clone(), released.clone(), stalled.clone());
            let peer = tokio::spawn(async move {
                let (socket, _) = listener.accept().await.unwrap();
                let mut ws = tokio_tungstenite::accept_async(socket).await.unwrap();
                while let Some(Ok(Message::Text(text))) = ws.next().await {
                    let command: Value = serde_json::from_str(&text).unwrap();
                    let method = command["method"].as_str().unwrap();
                    let result = match method {
                        "Target.attachToTarget" => json!({"sessionId":"session"}),
                        "Target.getTargets" => json!({"targetInfos":[]}),
                        "Page.getFrameTree" => {
                            json!({"frameTree":{"frame":{"id":"root","url":"http://fixture.test"}}})
                        }
                        "Page.createIsolatedWorld" => json!({"executionContextId":1}),
                        "Runtime.evaluate" => {
                            seen.lock().unwrap().0 = command["params"]["objectGroup"]
                                .as_str()
                                .unwrap()
                                .to_string();
                            if mode == "cancel-evaluate" {
                                paused.notify_one();
                                continue;
                            }
                            json!({"result":{"type":"object","objectId":"fixture-element"}})
                        }
                        "DOM.getBoxModel" => {
                            if mode == "cancel-box" {
                                paused.notify_one();
                                continue;
                            }
                            if mode == "error" {
                                ws.send(Message::Text(json!({"id":command["id"],"error":{"code":-32000,"message":"detached"}}).to_string().into())).await.unwrap();
                                continue;
                            }
                            json!({"model":{"content":[0,0,20,0,20,10,0,10]}})
                        }
                        "Runtime.releaseObjectGroup" => {
                            seen.lock().unwrap().1.push(
                                command["params"]["objectGroup"]
                                    .as_str()
                                    .unwrap()
                                    .to_string(),
                            );
                            done.notify_one();
                            json!({})
                        }
                        _ => panic!("unexpected command: {method}"),
                    };
                    ws.send(Message::Text(
                        json!({"id":command["id"],"result":result})
                            .to_string()
                            .into(),
                    ))
                    .await
                    .unwrap();
                }
            });
            let transport = crate::cdp::Transport::connect(&url, 30).await.unwrap();
            let connection = crate::cdp::Connection::new(transport);
            let session = connection.attach_to_target("root").await.unwrap();
            let page = Page::new(session, Arc::new(crate::StealthConfig::default()));
            if mode.starts_with("cancel") {
                let operation = async {
                    let quad = page.frame_element_content_quad("root", "img", 0);
                    tokio::pin!(quad);
                    tokio::select! {
                        _ = &mut quad => panic!("fixture should stall"),
                        _ = stalled.notified() => {},
                    }
                };
                tokio::time::timeout(std::time::Duration::from_secs(2), operation)
                    .await
                    .unwrap();
            } else {
                let result = page.frame_element_content_quad("root", "img", 0).await;
                assert_eq!(result.is_ok(), mode == "normal");
            }
            tokio::time::timeout(std::time::Duration::from_secs(2), released.notified())
                .await
                .unwrap();
            let observed = observed.lock().unwrap();
            assert!(!observed.0.is_empty());
            assert_eq!(observed.1, vec![observed.0.clone()]);
            peer.abort();
        }
    }

    #[test]
    fn only_known_discovery_races_are_retried() {
        assert!(frame_discovery_raced(&Error::cdp_msg(
            "No target with given id found"
        )));
        assert!(frame_discovery_raced(&Error::cdp_msg(
            "Session with given id not found"
        )));
        assert!(!frame_discovery_raced(&Error::cdp_msg(
            "Invalid parameters"
        )));
        assert!(!frame_discovery_raced(&Error::Decode(
            "No target with given id found".into()
        )));
    }
}