agentchrome 1.14.0

A CLI tool for browser automation via the Chrome DevTools Protocol
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
use serde::Serialize;

use agentchrome::connection::ManagedSession;
use agentchrome::error::{AppError, ExitCode};

use crate::cli::{GlobalOpts, PageScreenshotArgs, ScreenshotFormat};

use super::{get_viewport_dimensions, print_output, setup_session};

// =============================================================================
// Output types
// =============================================================================

/// Output when screenshot is returned as base64 (no --file).
#[derive(Serialize)]
struct ScreenshotResult {
    format: String,
    data: String,
    width: u32,
    height: u32,
}

/// Output when screenshot is saved to a file (--file).
#[derive(Serialize)]
struct ScreenshotFileResult {
    format: String,
    file: String,
    width: u32,
    height: u32,
}

// =============================================================================
// Clip region
// =============================================================================

/// A clip region for CDP's `Page.captureScreenshot` clip parameter.
#[derive(Debug)]
struct ClipRegion {
    x: f64,
    y: f64,
    width: f64,
    height: f64,
}

/// Parse a "X,Y,WIDTH,HEIGHT" string into a [`ClipRegion`].
fn parse_clip(input: &str) -> Result<ClipRegion, AppError> {
    let parts: Vec<&str> = input.split(',').collect();
    if parts.len() != 4 {
        return Err(AppError::invalid_clip(input));
    }
    let values: Result<Vec<f64>, _> = parts.iter().map(|p| p.trim().parse::<f64>()).collect();
    let values = values.map_err(|_| AppError::invalid_clip(input))?;
    Ok(ClipRegion {
        x: values[0],
        y: values[1],
        width: values[2],
        height: values[3],
    })
}

/// Extract a [`ClipRegion`] from a `DOM.getBoxModel` response.
fn extract_clip_from_box_model(box_result: &serde_json::Value) -> Option<ClipRegion> {
    let content = box_result["model"]["content"].as_array()?;
    if content.len() < 8 {
        return None;
    }
    let x1 = content[0].as_f64()?;
    let y1 = content[1].as_f64()?;
    let x3 = content[4].as_f64()?;
    let y3 = content[5].as_f64()?;
    Some(ClipRegion {
        x: x1,
        y: y1,
        width: x3 - x1,
        height: y3 - y1,
    })
}

// =============================================================================
// Clip resolution helpers
// =============================================================================

/// Resolve an element's bounding box as a clip region using a CSS selector.
async fn resolve_selector_clip(
    managed: &ManagedSession,
    selector: &str,
) -> Result<ClipRegion, AppError> {
    let doc = managed
        .send_command("DOM.getDocument", None)
        .await
        .map_err(|e| AppError::screenshot_failed(&e.to_string()))?;
    let root_node_id = doc["root"]["nodeId"]
        .as_i64()
        .ok_or_else(|| AppError::screenshot_failed("DOM.getDocument missing root nodeId"))?;

    let query_result = managed
        .send_command(
            "DOM.querySelector",
            Some(serde_json::json!({
                "nodeId": root_node_id,
                "selector": selector,
            })),
        )
        .await
        .map_err(|e| AppError::screenshot_failed(&format!("CSS selector query failed: {e}")))?;

    let node_id = query_result["nodeId"]
        .as_i64()
        .filter(|&id| id > 0)
        .ok_or_else(|| AppError::element_not_found(selector))?;

    let box_result = managed
        .send_command(
            "DOM.getBoxModel",
            Some(serde_json::json!({ "nodeId": node_id })),
        )
        .await
        .map_err(|e| {
            AppError::screenshot_failed(&format!("Failed to get element bounding box: {e}"))
        })?;

    extract_clip_from_box_model(&box_result)
        .ok_or_else(|| AppError::screenshot_failed("Element has no visible bounding box"))
}

/// Resolve an element's bounding box as a clip region using an accessibility UID.
async fn resolve_uid_clip(managed: &mut ManagedSession, uid: &str) -> Result<ClipRegion, AppError> {
    let state = crate::snapshot::read_snapshot_state()
        .map_err(|e| AppError::screenshot_failed(&format!("Failed to read snapshot state: {e}")))?
        .ok_or_else(|| AppError {
            message: "No snapshot state found. Run 'agentchrome page snapshot' first.".to_string(),
            code: ExitCode::GeneralError,
            custom_json: None,
        })?;

    let backend_node_id = state
        .uid_map
        .get(uid)
        .ok_or_else(|| AppError::uid_not_found(uid))?;

    // Pass backendNodeId directly to DOM.getBoxModel instead of resolving via
    // describeNode first — the intermediate nodeId was not anchored in the document
    // tree, which caused "Could not find node with given id" errors.
    let box_result = managed
        .send_command(
            "DOM.getBoxModel",
            Some(serde_json::json!({ "backendNodeId": backend_node_id })),
        )
        .await
        .map_err(|e| {
            AppError::screenshot_failed(&format!("Failed to get element bounding box: {e}"))
        })?;

    extract_clip_from_box_model(&box_result)
        .ok_or_else(|| AppError::screenshot_failed("Element has no visible bounding box"))
}

// =============================================================================
// Page/viewport dimension helpers
// =============================================================================

/// Get the full page dimensions (scroll width/height) via `Runtime.evaluate`.
async fn get_page_dimensions(managed: &ManagedSession) -> Result<(f64, f64), AppError> {
    let result = managed
        .send_command(
            "Runtime.evaluate",
            Some(serde_json::json!({
                "expression": "JSON.stringify({ width: Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth), height: Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) })",
                "returnByValue": true,
            })),
        )
        .await
        .map_err(|e| AppError::screenshot_failed(&format!("Failed to get page dimensions: {e}")))?;

    let value_str = result["result"]["value"]
        .as_str()
        .ok_or_else(|| AppError::screenshot_failed("Failed to read page dimensions"))?;
    let dims: serde_json::Value = serde_json::from_str(value_str).map_err(|e| {
        AppError::screenshot_failed(&format!("Failed to parse page dimensions: {e}"))
    })?;

    let width = dims["width"].as_f64().unwrap_or(1280.0);
    let height = dims["height"].as_f64().unwrap_or(720.0);

    Ok((width, height))
}

/// Set the viewport size via `Emulation.setDeviceMetricsOverride`.
async fn set_viewport_size(
    managed: &ManagedSession,
    width: u32,
    height: u32,
) -> Result<(), AppError> {
    managed
        .send_command(
            "Emulation.setDeviceMetricsOverride",
            Some(serde_json::json!({
                "width": width,
                "height": height,
                "deviceScaleFactor": 1,
                "mobile": false,
            })),
        )
        .await
        .map_err(|e| AppError::screenshot_failed(&format!("Failed to set viewport: {e}")))?;
    Ok(())
}

/// Clear viewport override via `Emulation.clearDeviceMetricsOverride`.
async fn clear_viewport_override(managed: &ManagedSession) -> Result<(), AppError> {
    managed
        .send_command("Emulation.clearDeviceMetricsOverride", None)
        .await
        .map_err(|e| AppError::screenshot_failed(&format!("Failed to restore viewport: {e}")))?;
    Ok(())
}

// =============================================================================
// Format helpers
// =============================================================================

/// Map `ScreenshotFormat` to CDP format string.
fn screenshot_format_str(format: ScreenshotFormat) -> &'static str {
    match format {
        ScreenshotFormat::Png => "png",
        ScreenshotFormat::Jpeg => "jpeg",
        ScreenshotFormat::Webp => "webp",
    }
}

/// Large image threshold for base64 warnings (10MB).
const LARGE_IMAGE_THRESHOLD: usize = 10_000_000;

/// Convert a clip region's dimensions to `(width, height)` as `u32`.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn clip_dimensions(clip: &ClipRegion) -> (u32, u32) {
    (clip.width as u32, clip.height as u32)
}

// =============================================================================
// Command executor
// =============================================================================

#[allow(clippy::too_many_lines)]
pub async fn execute_screenshot(
    global: &GlobalOpts,
    args: &PageScreenshotArgs,
) -> Result<(), AppError> {
    // Validate mutual exclusion: --full-page vs --selector/--uid
    if args.full_page && (args.selector.is_some() || args.uid.is_some()) {
        return Err(AppError::screenshot_failed(
            "Cannot combine --full-page with --selector or --uid",
        ));
    }

    let (_client, mut managed) = setup_session(global).await?;
    if global.auto_dismiss_dialogs {
        let _dismiss = managed.spawn_auto_dismiss().await?;
    }

    managed.ensure_domain("Page").await?;
    managed.ensure_domain("Runtime").await?;

    let format_str = screenshot_format_str(args.format);

    // Determine capture strategy
    let (clip, capture_beyond_viewport, dimensions) = if let Some(ref selector) = args.selector {
        managed.ensure_domain("DOM").await?;
        let clip = resolve_selector_clip(&managed, selector).await?;
        let dims = clip_dimensions(&clip);
        (Some(clip), false, dims)
    } else if let Some(ref uid) = args.uid {
        let clip = resolve_uid_clip(&mut managed, uid).await?;
        let dims = clip_dimensions(&clip);
        (Some(clip), false, dims)
    } else if args.full_page {
        let (page_w, page_h) = get_page_dimensions(&managed).await?;
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let dims = (page_w as u32, page_h as u32);
        set_viewport_size(&managed, dims.0, dims.1).await?;
        (None, true, dims)
    } else if let Some(ref clip_str) = args.clip {
        let clip = parse_clip(clip_str)?;
        let dims = clip_dimensions(&clip);
        (Some(clip), false, dims)
    } else {
        let dims = get_viewport_dimensions(&managed).await?;
        (None, false, dims)
    };

    // Build CDP params
    let mut params = serde_json::json!({ "format": format_str });

    if !matches!(args.format, ScreenshotFormat::Png) {
        let quality = args.quality.unwrap_or(80);
        params["quality"] = serde_json::json!(quality);
    }

    if let Some(ref clip) = clip {
        params["clip"] = serde_json::json!({
            "x": clip.x,
            "y": clip.y,
            "width": clip.width,
            "height": clip.height,
            "scale": 1,
        });
    }

    if capture_beyond_viewport {
        params["captureBeyondViewport"] = serde_json::json!(true);
    }

    // Capture
    let result = managed
        .send_command("Page.captureScreenshot", Some(params))
        .await
        .map_err(|e| AppError::screenshot_failed(&e.to_string()))?;

    // Restore viewport if full-page
    if args.full_page {
        clear_viewport_override(&managed).await?;
    }

    let data = result["data"]
        .as_str()
        .ok_or_else(|| AppError::screenshot_failed("No image data in response"))?;

    let (width, height) = dimensions;

    if data.len() > LARGE_IMAGE_THRESHOLD {
        eprintln!(
            "warning: screenshot data is {}MB (base64)",
            data.len() / 1_000_000
        );
    }

    if let Some(ref file_path) = args.file {
        use base64::Engine;
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(data)
            .map_err(|e| {
                AppError::screenshot_failed(&format!("Failed to decode image data: {e}"))
            })?;
        std::fs::write(file_path, &bytes).map_err(|e| {
            AppError::screenshot_failed(&format!(
                "Failed to write screenshot to file: {}: {e}",
                file_path.display()
            ))
        })?;

        let output = ScreenshotFileResult {
            format: format_str.to_string(),
            file: file_path.display().to_string(),
            width,
            height,
        };
        print_output(&output, &global.output)
    } else {
        let output = ScreenshotResult {
            format: format_str.to_string(),
            data: data.to_string(),
            width,
            height,
        };
        print_output(&output, &global.output)
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    // =========================================================================
    // Screenshot output type serialization tests
    // =========================================================================

    #[test]
    fn screenshot_result_serialization() {
        let result = ScreenshotResult {
            format: "png".to_string(),
            data: "iVBORw0KGgo=".to_string(),
            width: 1280,
            height: 720,
        };
        let json: serde_json::Value = serde_json::to_value(&result).unwrap();
        assert_eq!(json["format"], "png");
        assert_eq!(json["data"], "iVBORw0KGgo=");
        assert_eq!(json["width"], 1280);
        assert_eq!(json["height"], 720);
        assert!(json.get("file").is_none());
    }

    #[test]
    fn screenshot_file_result_serialization() {
        let result = ScreenshotFileResult {
            format: "jpeg".to_string(),
            file: "/tmp/screenshot.jpg".to_string(),
            width: 800,
            height: 600,
        };
        let json: serde_json::Value = serde_json::to_value(&result).unwrap();
        assert_eq!(json["format"], "jpeg");
        assert_eq!(json["file"], "/tmp/screenshot.jpg");
        assert_eq!(json["width"], 800);
        assert_eq!(json["height"], 600);
        assert!(json.get("data").is_none());
    }

    #[test]
    fn screenshot_result_webp_format() {
        let result = ScreenshotResult {
            format: "webp".to_string(),
            data: "UklGR...".to_string(),
            width: 640,
            height: 480,
        };
        let json: serde_json::Value = serde_json::to_value(&result).unwrap();
        assert_eq!(json["format"], "webp");
    }

    // =========================================================================
    // parse_clip tests
    // =========================================================================

    #[test]
    fn parse_clip_valid() {
        let clip = parse_clip("10,20,200,100").unwrap();
        assert!((clip.x - 10.0).abs() < f64::EPSILON);
        assert!((clip.y - 20.0).abs() < f64::EPSILON);
        assert!((clip.width - 200.0).abs() < f64::EPSILON);
        assert!((clip.height - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn parse_clip_decimal() {
        let clip = parse_clip("10.5,20.5,200.5,100.5").unwrap();
        assert!((clip.x - 10.5).abs() < f64::EPSILON);
        assert!((clip.y - 20.5).abs() < f64::EPSILON);
        assert!((clip.width - 200.5).abs() < f64::EPSILON);
        assert!((clip.height - 100.5).abs() < f64::EPSILON);
    }

    #[test]
    fn parse_clip_with_spaces() {
        let clip = parse_clip("10 , 20 , 200 , 100").unwrap();
        assert!((clip.x - 10.0).abs() < f64::EPSILON);
        assert!((clip.y - 20.0).abs() < f64::EPSILON);
    }

    #[test]
    fn parse_clip_invalid_text() {
        let err = parse_clip("abc").unwrap_err();
        assert!(err.message.contains("Invalid clip format"));
    }

    #[test]
    fn parse_clip_too_few_values() {
        let err = parse_clip("10,20,200").unwrap_err();
        assert!(err.message.contains("Invalid clip format"));
    }

    #[test]
    fn parse_clip_non_numeric() {
        let err = parse_clip("10,abc,200,100").unwrap_err();
        assert!(err.message.contains("Invalid clip format"));
    }

    #[test]
    fn parse_clip_empty() {
        let err = parse_clip("").unwrap_err();
        assert!(err.message.contains("Invalid clip format"));
    }

    // =========================================================================
    // screenshot_format_str tests
    // =========================================================================

    #[test]
    fn screenshot_format_str_mapping() {
        assert_eq!(screenshot_format_str(ScreenshotFormat::Png), "png");
        assert_eq!(screenshot_format_str(ScreenshotFormat::Jpeg), "jpeg");
        assert_eq!(screenshot_format_str(ScreenshotFormat::Webp), "webp");
    }

    // =========================================================================
    // extract_clip_from_box_model tests
    // =========================================================================

    #[test]
    fn extract_clip_valid_box_model() {
        let box_model = serde_json::json!({
            "model": {
                "content": [10.0, 20.0, 210.0, 20.0, 210.0, 120.0, 10.0, 120.0]
            }
        });
        let clip = extract_clip_from_box_model(&box_model).unwrap();
        assert!((clip.x - 10.0).abs() < f64::EPSILON);
        assert!((clip.y - 20.0).abs() < f64::EPSILON);
        assert!((clip.width - 200.0).abs() < f64::EPSILON);
        assert!((clip.height - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn extract_clip_insufficient_content() {
        let box_model = serde_json::json!({
            "model": { "content": [10.0, 20.0] }
        });
        assert!(extract_clip_from_box_model(&box_model).is_none());
    }

    #[test]
    fn extract_clip_missing_model() {
        let box_model = serde_json::json!({});
        assert!(extract_clip_from_box_model(&box_model).is_none());
    }
}