chrome-agent 0.14.0

Browser automation for AI agents. Single binary, zero deps, CDP direct to Chrome.
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
use std::io::{BufRead, BufReader, Read as _, Write as _};
use std::process::{Command, Stdio};

use serde_json::Value;

mod common;
use common::TestBrowser;

fn binary() -> String {
    let mut path = std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .to_path_buf();
    path.push("chrome-agent");
    path.to_string_lossy().into_owned()
}

fn run(args: &[&str]) -> (String, String, i32) {
    let output = Command::new(binary())
        .args(args)
        .output()
        .expect("run chrome-agent");
    (
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.code().unwrap_or(-1),
    )
}

fn run_with_stdin(args: &[&str], input: &str) -> (String, String, i32) {
    let mut child = Command::new(binary())
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("run chrome-agent with stdin");
    child
        .stdin
        .take()
        .unwrap()
        .write_all(input.as_bytes())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    (
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
        output.status.code().unwrap_or(-1),
    )
}

fn run_json(args: &[&str]) -> Value {
    let (stdout, stderr, code) = run(args);
    assert_eq!(
        code, 0,
        "command failed: {args:?}\nstdout: {stdout}\nstderr: {stderr}"
    );
    serde_json::from_str(&stdout)
        .unwrap_or_else(|error| panic!("invalid JSON for {args:?}: {error}\nstdout: {stdout}"))
}

struct TempRecording(std::path::PathBuf);

impl TempRecording {
    fn new(name: &str, contents: &str) -> Self {
        // Unique: two concurrent runs writing one path is the same collision as two runs
        // sharing one browser, and this file is read back after it is written.
        let path = common::temp_path(name, "jsonl");
        std::fs::write(&path, contents).unwrap();
        Self(path)
    }

    fn path(&self) -> &str {
        self.0.to_str().unwrap()
    }
}

impl Drop for TempRecording {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.0);
    }
}

fn read_page_metrics(browser: &str, page: &str) -> Value {
    run_json(&[
        "--browser",
        browser,
        "--page",
        page,
        "--json",
        "eval",
        r"({screen:[screen.width,screen.height],dpr:devicePixelRatio,touch:navigator.maxTouchPoints,coarse:matchMedia('(pointer: coarse)').matches})",
    ])["result"]
        .clone()
}

#[test]
fn emulation_stays_on_its_page_reapplies_and_cleans_up() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let probe = common::fixture_url("emulation_probe.html");
    let other = common::fixture_url("extract_cards.html");

    let response = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "goto",
        &probe,
    ]);
    assert_eq!(response["ok"], true);

    let applied = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "device",
        "--label",
        "checkout phone",
        "--width",
        "412",
        "--height",
        "915",
        "--dpr",
        "2.625",
        "--mobile",
        "--touch",
        "--orientation",
        "portrait",
    ]);
    assert_eq!(applied["emulation"]["label"], "checkout phone");
    assert_eq!(applied["emulation"]["dpr"], 2.625);
    assert!(applied["emulation"].get("deviceScaleFactor").is_none());
    assert_eq!(applied["emulation"]["orientation"], "portrait");
    assert_eq!(applied["effective"]["screen"]["width"], 412);
    assert_eq!(applied["effective"]["screen"]["height"], 915);
    assert_eq!(applied["effective"]["layoutViewport"]["width"], 412);
    assert_eq!(applied["effective"]["deviceScaleFactor"], 2.625);
    assert_eq!(applied["effective"]["touchPoints"], 1);
    assert_eq!(applied["effective"]["coarsePointer"], true);
    assert_eq!(applied["effective"]["orientation"], "portrait");

    // A fresh CLI invocation must reapply `--touch` without making CDP mouse dispatch hang.
    // chrome-agent synthesizes a tap, and Chromium follows it with compatibility mouse events.
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "eval",
            "(() => { window.__inputEvents = []; const el = document.querySelector('#marker'); for (const name of ['touchstart', 'touchend', 'mousedown', 'mouseup', 'click']) el.addEventListener(name, () => window.__inputEvents.push(name)); return true; })()",
        ])["result"],
        true
    );
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "click",
            "--selector",
            "#marker",
        ])["ok"],
        true
    );
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "eval",
            "window.__inputEvents",
        ])["result"],
        serde_json::json!(["touchstart", "touchend", "mousedown", "mouseup", "click"])
    );

    let (apply_text, stderr, code) = run(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "emulate",
        "device",
        "--width",
        "412",
        "--height",
        "915",
        "--dpr",
        "2.625",
        "--mobile",
        "--touch",
    ]);
    assert_eq!(code, 0, "text apply failed: {stderr}");
    assert!(apply_text.contains("Device emulation:"), "{apply_text}");
    assert!(
        apply_text.contains("Effective: viewport=412x915"),
        "{apply_text}"
    );

    let (status_text, stderr, code) = run(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "emulate",
        "status",
    ]);
    assert_eq!(code, 0, "text status failed: {stderr}");
    assert!(status_text.contains("Device emulation:"), "{status_text}");
    assert!(
        status_text.contains(
            "Effective: viewport=412x915 screen=412x915 dpr=2.625 touch_points=1 coarse_pointer=true orientation=portrait"
        ),
        "{status_text}"
    );

    // Every CLI call opens a new CDP connection. The same observed values therefore prove that
    // the named page's persisted configuration was reapplied rather than retained in this process.
    let mobile = read_page_metrics(browser.name(), "mobile");
    assert_eq!(mobile["screen"], serde_json::json!([412, 915]));
    assert_eq!(mobile["dpr"], 2.625);
    assert_eq!(mobile["touch"], 1);
    assert_eq!(mobile["coarse"], true);

    // The override is target-scoped: a sibling created afterwards must retain Chrome's defaults.
    let desktop_goto = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "desktop",
        "--json",
        "goto",
        &probe,
    ]);
    assert_eq!(desktop_goto["ok"], true);
    let desktop_status = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "desktop",
        "--json",
        "emulate",
        "status",
    ]);
    assert!(desktop_status["emulation"].is_null());
    let desktop = read_page_metrics(browser.name(), "desktop");
    assert_eq!(desktop["dpr"], 1.0);
    assert_eq!(desktop["touch"], 0);
    assert_eq!(desktop["coarse"], false);

    // Creating a sibling makes it Chrome's active target. Reconnecting to the emulated page must
    // reactivate that target so Screen Orientation does not silently fall back to the sibling's.
    let mobile_after_sibling = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert_eq!(mobile_after_sibling["effective"]["orientation"], "portrait");

    // Navigation replaces the document but not the target, so the named page keeps its metrics.
    let navigated = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "goto",
        &other,
    ]);
    assert_eq!(navigated["ok"], true);
    let after_navigation = read_page_metrics(browser.name(), "mobile");
    assert_eq!(after_navigation["screen"], serde_json::json!([412, 915]));
    assert_eq!(after_navigation["dpr"], 2.625);
    assert_eq!(after_navigation["touch"], 1);

    let reset = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "reset",
    ]);
    assert!(reset["emulation"].is_null());
    let clean = read_page_metrics(browser.name(), "mobile");
    assert_eq!(clean["dpr"], 1.0);
    assert_eq!(clean["touch"], 0);
    assert_eq!(clean["coarse"], false);

    // Chromium rejects `maxTouchPoints: 0`; a non-touch configuration must omit that field while
    // clearing touch capability and coarse-pointer detection.
    let non_touch = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "device",
        "--width",
        "1024",
        "--height",
        "768",
    ]);
    assert_eq!(non_touch["ok"], true);
    assert_eq!(non_touch["emulation"]["touch"], false);
    assert_eq!(non_touch["effective"]["touchPoints"], 0);
    assert_eq!(non_touch["effective"]["coarsePointer"], false);
}

#[test]
fn pipe_and_batch_share_the_same_page_scoped_state() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let probe = common::fixture_url("emulation_probe.html");
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &probe,
        ])["ok"],
        true
    );

    let commands = concat!(
        "{\"cmd\":\"emulate\",\"action\":\"device\",\"width\":390,\"height\":844,\"dpr\":\"bad\"}\n",
        "{\"cmd\":\"emulate\",\"action\":\"status\"}\n",
        "{\"cmd\":\"emulate\",\"action\":\"device\",\"label\":\"pipe phone\",",
        "\"width\":390,\"height\":844,\"dpr\":3,\"mobile\":true,\"touch\":true}\n",
        "{\"cmd\":\"emulate\",\"action\":\"status\"}\n",
    );
    let (stdout, stderr, code) = run_with_stdin(
        &["--browser", browser.name(), "--page", "mobile", "pipe"],
        commands,
    );
    assert_eq!(code, 0, "pipe failed: {stderr}");
    let responses: Vec<Value> = stdout
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(responses.len(), 4, "pipe responses: {responses:?}");
    assert_eq!(responses[0]["ok"], false);
    assert!(responses[0]["error"].as_str().unwrap().contains("dpr"));
    assert!(responses[1]["emulation"].is_null());
    assert_eq!(responses[3]["emulation"]["label"], "pipe phone");
    assert_eq!(responses[3]["effective"]["screen"]["width"], 390);

    // The pipe persisted the configuration, so a separate CLI process can reconnect and observe it.
    let status = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert_eq!(status["effective"]["deviceScaleFactor"], 3.0);
    assert_eq!(status["effective"]["touchPoints"], 1);

    let (stdout, stderr, code) = run_with_stdin(
        &[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "batch",
        ],
        r#"[{"cmd":"emulate","action":"reset"},{"cmd":"emulate","action":"status"}]"#,
    );
    assert_eq!(code, 0, "batch failed: {stderr}");
    let batch: Value = serde_json::from_str(&stdout).unwrap();
    assert!(batch["results"][0]["emulation"].is_null());
    assert!(batch["results"][1]["emulation"].is_null());
}

#[test]
fn status_stays_page_scoped_while_pipe_eval_stays_in_its_frame() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let parent = common::fixture_url("frame_parent.html");
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &parent,
        ])["ok"],
        true
    );

    let commands = concat!(
        "{\"cmd\":\"emulate\",\"action\":\"device\",\"width\":390,\"height\":844}\n",
        "{\"cmd\":\"frame\",\"target\":\"#the-frame\"}\n",
        "{\"cmd\":\"emulate\",\"action\":\"status\"}\n",
        "{\"cmd\":\"eval\",\"expression\":\"document.title\"}\n",
        "{\"cmd\":\"emulate\",\"action\":\"reset\"}\n",
    );
    let (stdout, stderr, code) = run_with_stdin(
        &["--browser", browser.name(), "--page", "mobile", "pipe"],
        commands,
    );
    assert_eq!(code, 0, "pipe failed: {stderr}");
    let responses: Vec<Value> = stdout
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(responses.len(), 5, "pipe responses: {responses:?}");

    let status = &responses[2];
    assert_eq!(status["effective"]["layoutViewport"]["width"], 390);
    assert_eq!(status["effective"]["layoutViewport"]["height"], 844);
    assert_eq!(status["effective"]["orientation"], "portrait");
    assert_eq!(responses[3]["result"], "CHILD FRAME TITLE");
    assert_eq!(responses[4]["ok"], true);
}

#[test]
fn an_open_pipe_publishes_emulation_changes_immediately() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let probe = common::fixture_url("emulation_probe.html");
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &probe,
        ])["ok"],
        true
    );

    let mut child = Command::new(binary())
        .args(["--browser", browser.name(), "--page", "mobile", "pipe"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("start pipe");
    let mut input = child.stdin.take().unwrap();
    let mut output = BufReader::new(child.stdout.take().unwrap());

    writeln!(
        input,
        r#"{{"cmd":"emulate","action":"device","width":390,"height":844,"dpr":3,"mobile":true,"touch":true}}"#
    )
    .unwrap();
    input.flush().unwrap();
    let mut line = String::new();
    output.read_line(&mut line).unwrap();
    let applied: Value = serde_json::from_str(&line).unwrap();
    assert_eq!(applied["ok"], true);

    // The pipe still owns stdin and has not reached its final save. Visibility here therefore
    // proves that a successful device command commits its session state immediately — a pipe
    // that only persisted on exit would leave a crashed session's emulation unrecorded.
    let concurrent_status = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert_eq!(concurrent_status["emulation"]["width"], 390);
    assert_eq!(concurrent_status["effective"]["deviceScaleFactor"], 3.0);

    // Reset through the pipe itself (via batch, which shares the recovery state), then confirm
    // a fresh CLI process reads the cleared configuration. Concurrent WRITES from a second
    // process are deliberately not exercised: one writer per --browser is the store's contract.
    writeln!(
        input,
        r#"{{"cmd":"batch","commands":[{{"cmd":"emulate","action":"reset"}}]}}"#
    )
    .unwrap();
    input.flush().unwrap();
    line.clear();
    output.read_line(&mut line).unwrap();
    let reset: Value = serde_json::from_str(&line).unwrap();
    assert_eq!(reset["ok"], true);
    assert_eq!(reset["results"][0]["ok"], true);

    let concurrent_reset = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert!(concurrent_reset["emulation"].is_null());

    drop(input);
    let status = child.wait().unwrap();
    let mut stderr = String::new();
    child
        .stderr
        .take()
        .unwrap()
        .read_to_string(&mut stderr)
        .unwrap();
    assert!(status.success(), "pipe failed: {stderr}");
}

#[test]
fn replay_applies_reports_and_resets_page_emulation() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let probe = common::fixture_url("emulation_probe.html");
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &probe,
        ])["ok"],
        true
    );

    let recording = TempRecording::new(
        browser.name(),
        concat!(
            "{\"cmd\":\"emulate\",\"action\":\"device\",\"width\":390,\"height\":844,\"dpr\":3,\"mobile\":true,\"touch\":true}\n",
            "{\"cmd\":\"emulate\",\"action\":\"status\"}\n",
            "{\"cmd\":\"emulate\",\"action\":\"reset\"}\n",
            "{\"cmd\":\"emulate\",\"action\":\"status\"}\n",
        ),
    );
    let (stdout, stderr, code) = run(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "replay",
        recording.path(),
    ]);
    assert_eq!(code, 0, "replay failed: {stderr}");
    let responses: Vec<Value> = stdout
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(responses.len(), 4, "replay responses: {responses:?}");
    assert_eq!(responses[0]["effective"]["deviceScaleFactor"], 3.0);
    assert_eq!(responses[1]["emulation"]["width"], 390);
    assert!(responses[2]["emulation"].is_null());
    assert!(responses[3]["emulation"].is_null());

    let status = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert!(status["emulation"].is_null());
}

#[test]
fn browser_restart_discards_page_emulation() {
    if !common::browser_ready() {
        return;
    }
    let browser = TestBrowser::new("device-emulation");
    let probe = common::fixture_url("emulation_probe.html");

    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &probe,
        ])["ok"],
        true
    );
    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "emulate",
            "device",
            "--width",
            "412",
            "--height",
            "915",
            "--dpr",
            "2.625",
            "--mobile",
            "--touch",
        ])["ok"],
        true
    );

    let (_, stderr, code) = run(&["--browser", browser.name(), "close"]);
    assert_eq!(code, 0, "close failed: {stderr}");

    assert_eq!(
        run_json(&[
            "--browser",
            browser.name(),
            "--page",
            "mobile",
            "--json",
            "goto",
            &probe,
        ])["ok"],
        true
    );
    let status = run_json(&[
        "--browser",
        browser.name(),
        "--page",
        "mobile",
        "--json",
        "emulate",
        "status",
    ]);
    assert!(
        status["emulation"].is_null(),
        "restart retained stale state: {status}"
    );
}