stepshots-cli 1.0.1

CLI for recording, bundling, and uploading Stepshots demos
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
use manifest::Viewport;
use serde::{Deserialize, Serialize};

use crate::browser::Browser;
use crate::error::CliError;
use crate::output;

#[derive(Debug, Deserialize, Serialize)]
struct InteractiveElement {
    index: usize,
    tag: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    element_type: Option<String>,
    selector: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    text: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    placeholder: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    aria_label: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    href: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    bounds: Option<Bounds>,
}

#[derive(Debug, Deserialize, Serialize)]
struct Bounds {
    x: f64,
    y: f64,
    width: f64,
    height: f64,
}

const SCAN_JS: &str = r#"
(() => {
    const INTERACTIVE_QUERY = [
        'a[href]', 'button', 'input', 'textarea', 'select',
        '[role="button"]', '[role="link"]', '[role="checkbox"]',
        '[role="tab"]', '[onclick]', '[tabindex]:not([tabindex="-1"])'
    ].join(', ');

    const ALL_VISIBLE_QUERY = '*';

    function isVisible(el) {
        const style = getComputedStyle(el);
        if (style.display === 'none' || style.visibility === 'hidden') return false;
        const rect = el.getBoundingClientRect();
        return rect.width > 0 && rect.height > 0;
    }

    function uniqueSelector(el) {
        // Try id
        if (el.id) {
            const sel = '#' + CSS.escape(el.id);
            if (document.querySelectorAll(sel).length === 1) return sel;
        }

        const tag = el.tagName.toLowerCase();

        // Try tag + meaningful attributes
        for (const attr of ['name', 'data-testid', 'aria-label', 'href', 'type', 'placeholder']) {
            const val = el.getAttribute(attr);
            if (val) {
                const sel = `${tag}[${attr}="${CSS.escape(val)}"]`;
                if (document.querySelectorAll(sel).length === 1) return sel;
            }
        }

        // Try tag + classes
        if (el.classList.length > 0) {
            const sel = tag + '.' + Array.from(el.classList).map(c => CSS.escape(c)).join('.');
            if (document.querySelectorAll(sel).length === 1) return sel;
        }

        // Path-based: walk up to find a unique ancestor
        let current = el;
        const parts = [];
        while (current && current !== document.body) {
            const ctag = current.tagName.toLowerCase();
            const parent = current.parentElement;
            if (parent) {
                const siblings = Array.from(parent.children).filter(c => c.tagName === current.tagName);
                if (siblings.length > 1) {
                    const idx = siblings.indexOf(current) + 1;
                    parts.unshift(`${ctag}:nth-of-type(${idx})`);
                } else {
                    parts.unshift(ctag);
                }
            } else {
                parts.unshift(ctag);
            }
            // Check if current path is unique
            const candidate = parts.join(' > ');
            if (document.querySelectorAll(candidate).length === 1) return candidate;
            current = parent;
        }

        return parts.join(' > ');
    }

    function scan(allElements) {
        const query = allElements ? ALL_VISIBLE_QUERY : INTERACTIVE_QUERY;
        const elements = Array.from(document.querySelectorAll(query));
        const results = [];

        for (const el of elements) {
            if (!isVisible(el)) continue;
            // Skip html/head/body/script/style for 'all' mode
            const tag = el.tagName.toLowerCase();
            if (['html', 'head', 'body', 'script', 'style', 'meta', 'link', 'noscript'].includes(tag)) continue;

            const rect = el.getBoundingClientRect();
            const text = el.innerText?.trim().substring(0, 80) || null;
            const placeholder = el.getAttribute('placeholder') || null;
            const ariaLabel = el.getAttribute('aria-label') || null;
            const href = el.getAttribute('href') || null;
            const elType = el.getAttribute('type') || null;

            results.push({
                tag,
                element_type: elType,
                selector: uniqueSelector(el),
                text: text || null,
                placeholder,
                aria_label: ariaLabel,
                href,
                bounds: { x: rect.x, y: rect.y, width: rect.width, height: rect.height }
            });
        }

        // Sort by position: top-to-bottom, left-to-right
        results.sort((a, b) => {
            const dy = a.bounds.y - b.bounds.y;
            if (Math.abs(dy) > 5) return dy;
            return a.bounds.x - b.bounds.x;
        });

        // Add index
        results.forEach((r, i) => { r.index = i + 1; });

        return results;
    }

    return JSON.stringify(scan(!!window.__STEPSHOTS_SCAN_ALL));
})()
"#;

pub async fn run(url: &str, width: u32, height: u32, json: bool) -> Result<(), CliError> {
    let viewport = Viewport {
        width,
        height,
        device_scale_factor: None,
    };

    if !json {
        println!("Launching browser...");
    }
    let browser = Browser::launch(&viewport, json).await?;

    if !json {
        println!("Navigating to {url}");
    }
    browser.navigate(url).await?;
    browser.wait_idle(1500).await;

    let mut elements = scan_elements(&browser, false).await?;

    if json {
        let out = output::InspectOutput {
            url: url.to_string(),
            elements: elements
                .into_iter()
                .map(|el| output::InspectElement {
                    index: el.index,
                    tag: el.tag,
                    element_type: el.element_type,
                    selector: el.selector,
                    text: el.text,
                    placeholder: el.placeholder,
                    aria_label: el.aria_label,
                    href: el.href,
                    bounds: el.bounds.map(|b| output::InspectBounds {
                        x: b.x,
                        y: b.y,
                        width: b.width,
                        height: b.height,
                    }),
                })
                .collect(),
        };
        println!("{}", serde_json::to_string_pretty(&out).unwrap());
        return Ok(());
    }

    print_table(url, &elements);

    // Interactive REPL
    println!(
        "\nCommands: <number> detail | r refresh | all scan all | nav <url> navigate | q quit"
    );

    loop {
        let line = match read_line_or_ctrl_c().await {
            Some(l) => l,
            None => break, // Ctrl+C
        };

        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        match line {
            "q" | "quit" => break,
            "r" | "refresh" => {
                elements = scan_elements(&browser, false).await?;
                print_table(url, &elements);
            }
            "all" => {
                elements = scan_elements(&browser, true).await?;
                print_table(url, &elements);
            }
            cmd if cmd.starts_with("nav ") => {
                let target = cmd.strip_prefix("nav ").unwrap().trim();
                let resolved = resolve_url(url, target);
                println!("Navigating to {resolved}...");
                browser.navigate(&resolved).await?;
                browser.wait_idle(1500).await;
                elements = scan_elements(&browser, false).await?;
                print_table(&resolved, &elements);
            }
            _ => {
                if let Ok(num) = line.parse::<usize>() {
                    if let Some(el) = elements.iter().find(|e| e.index == num) {
                        print_detail(el);
                    } else {
                        println!("No element #{num}. Range: 1-{}", elements.len());
                    }
                } else {
                    println!("Unknown command: {line}");
                    println!(
                        "Commands: <number> detail | r refresh | all scan all | nav <url> navigate | q quit"
                    );
                }
            }
        }
    }

    println!("Bye!");
    Ok(())
}

async fn scan_elements(browser: &Browser, all: bool) -> Result<Vec<InteractiveElement>, CliError> {
    // Set the scan mode flag
    let flag_js = format!(
        "window.__STEPSHOTS_SCAN_ALL = {}",
        if all { "true" } else { "false" }
    );
    browser
        .page()
        .evaluate(flag_js)
        .await
        .map_err(|e| CliError::Browser(format!("Failed to set scan flag: {e}")))?;

    let result = browser
        .page()
        .evaluate(SCAN_JS)
        .await
        .map_err(|e| CliError::Browser(format!("Failed to scan elements: {e}")))?;

    let json_str = result
        .into_value::<String>()
        .map_err(|_| CliError::Browser("Failed to parse scan result as string".into()))?;

    let elements: Vec<InteractiveElement> = serde_json::from_str(&json_str)
        .map_err(|e| CliError::Browser(format!("Failed to deserialize elements: {e}")))?;

    Ok(elements)
}

fn print_table(url: &str, elements: &[InteractiveElement]) {
    let mode = if elements.iter().any(|e| {
        !matches!(
            e.tag.as_str(),
            "a" | "button" | "input" | "textarea" | "select"
        )
    }) {
        " (all visible)"
    } else {
        ""
    };

    println!("\nInteractive elements on {url}{mode}:\n");

    if elements.is_empty() {
        println!("  (no elements found)");
        return;
    }

    // Column widths
    let idx_w = 4;
    let tag_w = 10;
    let sel_w = 50;

    println!(
        "  {:>idx_w$}  | {:<tag_w$} | {:<sel_w$} | Text / Label",
        "#", "Tag", "Selector"
    );
    println!(
        "  {:->idx_w$}--+-{:-<tag_w$}-+-{:-<sel_w$}-+---------------------------",
        "", "", ""
    );

    for el in elements {
        let label = element_label(el);
        let selector = truncate(&el.selector, sel_w);
        println!(
            "  {:>idx_w$}  | {:<tag_w$} | {:<sel_w$} | {}",
            el.index, el.tag, selector, label
        );
    }

    println!("\n  {} elements found.", elements.len());
}

fn print_detail(el: &InteractiveElement) {
    println!("\n  Element #{}", el.index);
    println!("  Tag:         {}", el.tag);
    if let Some(ref t) = el.element_type {
        println!("  Type:        {t}");
    }
    println!("  Selector:    {}", el.selector);
    if let Some(ref t) = el.text {
        println!("  Text:        \"{t}\"");
    }
    if let Some(ref p) = el.placeholder {
        println!("  Placeholder: \"{p}\"");
    }
    if let Some(ref a) = el.aria_label {
        println!("  Aria-label:  \"{a}\"");
    }
    if let Some(ref h) = el.href {
        println!("  Href:        {h}");
    }
    if let Some(ref b) = el.bounds {
        println!(
            "  Bounds:      x={:.0} y={:.0} w={:.0} h={:.0}",
            b.x, b.y, b.width, b.height
        );
    }
    println!();
}

fn element_label(el: &InteractiveElement) -> String {
    if let Some(ref t) = el.text
        && !t.is_empty()
    {
        return format!("\"{}\"", truncate(t, 40));
    }
    if let Some(ref p) = el.placeholder {
        return format!("placeholder: \"{p}\"");
    }
    if let Some(ref a) = el.aria_label {
        return format!("aria: \"{a}\"");
    }
    if let Some(ref h) = el.href {
        return truncate(h, 40).to_string();
    }
    String::new()
}

fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}...", &s[..max.saturating_sub(3)])
    }
}

fn resolve_url(base: &str, path: &str) -> String {
    if path.starts_with("http://") || path.starts_with("https://") {
        return path.to_string();
    }
    // Extract origin from base URL
    if let Some(idx) = base.find("://") {
        let after_scheme = &base[idx + 3..];
        let origin_end = after_scheme.find('/').unwrap_or(after_scheme.len());
        let origin = &base[..idx + 3 + origin_end];
        let path = if path.starts_with('/') {
            path.to_string()
        } else {
            format!("/{path}")
        };
        format!("{origin}{path}")
    } else {
        format!("{base}{path}")
    }
}

async fn read_line_or_ctrl_c() -> Option<String> {
    let line_future = tokio::task::spawn_blocking(|| {
        let mut buf = String::new();
        eprint!("inspect> ");
        match std::io::stdin().read_line(&mut buf) {
            Ok(0) => None,
            Ok(_) => Some(buf),
            Err(_) => None,
        }
    });

    tokio::select! {
        result = line_future => {
            match result {
                Ok(Some(line)) => Some(line),
                _ => None,
            }
        }
        _ = tokio::signal::ctrl_c() => {
            println!();
            None
        }
    }
}