zendriver 0.4.3

Async-first browser automation via the Chrome DevTools Protocol, with anti-detection controls on by default
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
//! Phase 5 end-to-end tests against real Chrome + wiremock.
//!
//! Each test serves a tiny HTML fixture via `wiremock`, launches a headless
//! Chrome via `Browser::builder()`, exercises one P5 sub-area
//! (interception / expect_* / cloudflare detection), and asserts on
//! observable behavior via CDP, wiremock counters, or page-side JS state.
//!
//! Gated behind the `integration-tests` feature so CI can skip on
//! Chrome-less runners. The `fetcher` happy-path is *not* covered here —
//! downloading a full CfT zip is too heavy for PR CI; that lives behind
//! the separate `fetcher-network-tests` feature.

#![cfg(feature = "integration-tests")]
#![allow(clippy::panic, clippy::unwrap_used)]

use std::io::Write;
use std::time::Duration;

use serial_test::serial;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use zendriver::Browser;

/// Short pause after `intercept().start()` so the actor's fire-and-forget
/// `Fetch.enable` has reached Chrome before we issue a navigation that may
/// otherwise race past the interception attachment.
const INTERCEPT_SETTLE: Duration = Duration::from_millis(150);

#[tokio::test]
#[serial]
async fn interception_block_rule_prevents_request() {
    // Wiremock serves a real `/blocked/x.json` endpoint with `.expect(0)`
    // — wiremock's `Drop` will panic if any request lands on it. The
    // interception block rule should intercept the in-page `fetch()` before
    // the network bytes leave Chrome, so the count stays zero.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <script>
                window.fetchErr = null;
                fetch('/blocked/x.json')
                  .then(r => r.text())
                  .then(t => { window.fetchResult = t; })
                  .catch(e => { window.fetchErr = String(e); });
              </script>
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;
    Mock::given(method("GET"))
        .and(path("/blocked/x.json"))
        .respond_with(ResponseTemplate::new(200).set_body_string("should-not-arrive"))
        .expect(0)
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();

    // Start interception BEFORE the navigation; the actor's `Fetch.enable`
    // is fire-and-forget so give it a brief moment to land before goto.
    let _intercept = tab.intercept().block("*/blocked/*").unwrap().start();
    tokio::time::sleep(INTERCEPT_SETTLE).await;

    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    // The page's fetch fires synchronously during the body parse; wait
    // long enough for Chrome to either complete it or hit the block path.
    // 500ms is comfortably above the localhost RTT + intercept dispatch.
    tokio::time::sleep(Duration::from_millis(500)).await;

    // The fetch should have rejected with a network error since Chrome
    // refused the request via `Fetch.failRequest { BlockedByClient }`.
    let err: Option<String> = tab.evaluate_main("window.fetchErr").await.unwrap_or(None);
    assert!(
        err.is_some(),
        "fetch to /blocked/x.json should have errored; got window.fetchErr = {err:?}"
    );

    browser.close().await.unwrap();

    // wiremock's `Drop` runs `verify()` on the MockServer; the
    // `.expect(0)` on `/blocked/x.json` panics if the request landed.
    drop(mock);
}

#[tokio::test]
#[serial]
async fn interception_respond_serves_fake_body() {
    // The page calls `fetch('/api/health')` which would normally 404
    // (we never mount a real route for it). The respond rule synthesizes
    // a 200 with the fake body, so the page sees `"hello-from-respond"`.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <script>
                window.r = null;
                fetch('/api/health').then(r => r.text()).then(t => { window.r = t; });
              </script>
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();

    let body = b"hello-from-respond".to_vec();
    let _intercept = tab
        .intercept()
        .respond(
            "*/api/health",
            200,
            vec![("content-type".into(), "text/plain".into())],
            body,
        )
        .unwrap()
        .start();
    tokio::time::sleep(INTERCEPT_SETTLE).await;

    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    // Poll for the fetch to resolve; `window.r` flips from `null` to the
    // body string. 2s budget covers Chrome scheduling on slow CI.
    let deadline = std::time::Instant::now() + Duration::from_secs(2);
    let r: String = loop {
        let v: Option<String> = tab.evaluate_main("window.r").await.unwrap_or(None);
        if let Some(s) = v {
            break s;
        }
        if std::time::Instant::now() >= deadline {
            panic!("respond rule never delivered a body to window.r within 2s");
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    };

    assert_eq!(r, "hello-from-respond");

    browser.close().await.unwrap();
}

#[tokio::test]
#[serial]
async fn expect_response_returns_matched() {
    // The page fires an XHR ~200ms after load against `/api/data`. The
    // expectation is registered BEFORE goto so the subscriber is live
    // when the response lands.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/api/data"))
        .respond_with(ResponseTemplate::new(200).set_body_string(r#"{"ok":true}"#))
        .mount(&mock)
        .await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <script>
                setTimeout(() => { fetch('/api/data'); }, 200);
              </script>
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();

    // Register expectation first so the `Network.responseReceived`
    // subscriber is in place before the navigation triggers the fetch.
    let expectation = tab
        .expect_response("/api/data")
        .timeout(Duration::from_secs(5));

    tab.goto(&mock.uri()).await.unwrap();

    let matched = expectation.await.expect("expect_response should resolve");
    assert!(
        matched.url.contains("/api/data"),
        "matched URL should contain /api/data; got {}",
        matched.url
    );
    assert_eq!(matched.status, 200);

    browser.close().await.unwrap();
}

// `expect_dialog` against a real dialog on real Chrome, driven by a user
// gesture (a click whose handler calls `alert()`).
//
// This test was `#[ignore]`d for a long time with a note blaming
// `--headless=new` for "suppressing script-driven `alert()`". That was
// WRONG. The real bug: `expect_dialog` subscribed to
// `Page.javascriptDialogOpened` — a CDP event that DOES NOT EXIST (Chrome
// fires `Page.javascriptDialogOpening`). So the expectation always timed
// out against real Chrome, while the mock unit test — firing the same
// non-existent name — stayed green and hid it. Fixed in `expect/dialog.rs`.
// (Verified after the fix: a plain script-driven `alert()` resolves fine
// too, headless included — the MCP `end_to_end_dialog_accept` test drives
// exactly that. The gesture trigger here is just a stronger, more realistic
// end-to-end.) `#[ignore]` now only because it needs real Chrome — runs in
// the `nightly-ignored-tests` lane.
#[tokio::test]
#[serial]
#[ignore = "real-Chrome; runs in the nightly-ignored-tests lane"]
async fn expect_dialog_resolves_on_alert() {
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <button id="go" onclick="alert('hi')">go</button>
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();
    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    // Register BEFORE the click so the `Page.javascriptDialogOpened`
    // subscriber is live when the dialog opens.
    let expectation = tab.expect_dialog().timeout(Duration::from_secs(5));

    // Click WITHOUT awaiting: `alert()` blocks the renderer's JS thread, so
    // the mouseup dispatch won't return until the dialog is handled —
    // awaiting the click here would deadlock against the expectation below.
    // Fire it on a task; it completes once we `accept()` and JS unblocks.
    let btn = tab.find().css("#go").one().await.unwrap();
    let click = tokio::spawn(async move { btn.click().await });

    let matched = expectation.await.expect("expect_dialog should resolve");
    assert_eq!(matched.message, "hi");
    matched.accept(None).await.unwrap();
    let _ = click.await;

    browser.close().await.unwrap();
}

#[tokio::test]
#[serial]
async fn cloudflare_is_challenge_present_returns_false_on_normal_page() {
    // A vanilla page has no Cloudflare Turnstile iframe in any shadow
    // root, so the detector script returns null → `is_challenge_present`
    // returns `false`. Confirms the detector doesn't false-positive on
    // ordinary pages.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body><h1>plain page</h1></body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();
    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    let present = tab
        .cloudflare()
        .is_challenge_present()
        .await
        .expect("is_challenge_present should not error on a normal page");
    assert!(
        !present,
        "vanilla page must not be reported as carrying a Cloudflare challenge"
    );

    browser.close().await.unwrap();
}

// Bucketed into the nightly-only `#[ignore]` lane (per the recently-added
// `nightly-ignored-tests` job) rather than the per-PR gate: this exercises a
// brand-new CDP flow (`Page.setInterceptFileChooserDialog`) rather than a
// known Chrome limitation like `expect_dialog_resolves_on_alert` above.
// Verified locally against real headless Chrome before landing.
#[tokio::test]
#[serial]
#[ignore = "new Page.setInterceptFileChooserDialog flow; nightly-only per project convention"]
async fn expect_file_chooser_intercepts_button_triggered_hidden_input() {
    // The button's onclick indirects through a hidden <input type=file> —
    // the case `Element::upload_files` can't reach (it only knows how to
    // target a direct file input's backend node) but `expect_file_chooser`
    // can, since it intercepts `Page.fileChooserOpened` regardless of what
    // triggered it.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <input type="file" id="hidden-input" style="display:none" />
              <button id="picker-btn"
                onclick="document.getElementById('hidden-input').click()">
                Pick file
              </button>
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();
    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    writeln!(tmp, "hello from a button-triggered picker").unwrap();
    let path = tmp.path().to_path_buf();

    // Register BEFORE the click — the intercept must be enabled on Chrome's
    // side before the click can race it.
    let fc = tab
        .expect_file_chooser(&[&path])
        .await
        .unwrap()
        .timeout(Duration::from_secs(5));

    let button = tab.find().css("#picker-btn").one().await.unwrap();
    button.click().await.unwrap();

    let matched = fc.await.expect("expect_file_chooser should resolve");
    assert_eq!(matched.mode, zendriver::FileChooserMode::SelectSingle);

    let file_count: i64 = tab
        .evaluate_main("document.getElementById('hidden-input').files.length")
        .await
        .unwrap();
    assert_eq!(file_count, 1);

    let file_name: String = tab
        .evaluate_main("document.getElementById('hidden-input').files[0].name")
        .await
        .unwrap();
    let expected_name = path.file_name().unwrap().to_string_lossy().into_owned();
    assert_eq!(file_name, expected_name);

    browser.close().await.unwrap();
}

// Same nightly-only rationale as the button test above.
#[tokio::test]
#[serial]
#[ignore = "new Page.setInterceptFileChooserDialog flow; nightly-only per project convention"]
async fn expect_file_chooser_also_answers_a_directly_clicked_input() {
    // Confirms the same intercept path works when the file input is clicked
    // directly (not just via a button/label indirection) — the same CDP
    // event fires either way.
    let mock = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw(
                r#"<!doctype html><html><body>
              <input type="file" id="f" />
            </body></html>"#
                    .as_bytes()
                    .to_vec(),
                "text/html",
            ),
        )
        .mount(&mock)
        .await;

    let browser = Browser::builder().headless(true).launch().await.unwrap();
    let tab = browser.main_tab();
    tab.goto(&mock.uri()).await.unwrap();
    tab.wait_for_load().await.unwrap();

    let mut tmp = tempfile::NamedTempFile::new().unwrap();
    writeln!(tmp, "hello from a directly-clicked input").unwrap();
    let path = tmp.path().to_path_buf();

    let fc = tab
        .expect_file_chooser(&[&path])
        .await
        .unwrap()
        .timeout(Duration::from_secs(5));

    let input = tab.find().css("#f").one().await.unwrap();
    input.click().await.unwrap();

    fc.await.expect("expect_file_chooser should resolve");

    let file_name: String = tab
        .evaluate_main("document.getElementById('f').files[0].name")
        .await
        .unwrap();
    let expected_name = path.file_name().unwrap().to_string_lossy().into_owned();
    assert_eq!(file_name, expected_name);

    browser.close().await.unwrap();
}