browser-pilot 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -0
- package/dist/actions.d.cts +2 -2
- package/dist/actions.d.ts +2 -2
- package/dist/browser.cjs +7 -0
- package/dist/browser.d.cts +2 -2
- package/dist/browser.d.ts +2 -2
- package/dist/browser.mjs +1 -1
- package/dist/{chunk-NP56KSAN.mjs → chunk-PCNEJAJ7.mjs} +7 -0
- package/dist/cli.cjs +1008 -0
- package/dist/cli.mjs +1002 -1
- package/dist/index.cjs +7 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/{types-DKz34hii.d.ts → types-CbdmaocU.d.ts} +5 -0
- package/dist/{types-DQhA8uQZ.d.cts → types-TVlTA7nH.d.cts} +5 -0
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -1464,6 +1464,13 @@ var Page = class {
|
|
|
1464
1464
|
this.cdp = cdp;
|
|
1465
1465
|
this.batchExecutor = new BatchExecutor(this);
|
|
1466
1466
|
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Get the underlying CDP client for advanced operations.
|
|
1469
|
+
* Use with caution - prefer high-level Page methods when possible.
|
|
1470
|
+
*/
|
|
1471
|
+
get cdpClient() {
|
|
1472
|
+
return this.cdp;
|
|
1473
|
+
}
|
|
1467
1474
|
/**
|
|
1468
1475
|
* Initialize the page (enable required CDP domains)
|
|
1469
1476
|
*/
|
|
@@ -3460,12 +3467,1007 @@ COMMON ACTIONS
|
|
|
3460
3467
|
snapshot {"action":"snapshot"}
|
|
3461
3468
|
screenshot {"action":"screenshot"}
|
|
3462
3469
|
|
|
3470
|
+
RECORDING (FOR HUMANS)
|
|
3471
|
+
Want to create automations by demonstrating instead of coding?
|
|
3472
|
+
Use 'bp record' to capture your browser interactions as replayable JSON:
|
|
3473
|
+
|
|
3474
|
+
bp record # Record from local Chrome
|
|
3475
|
+
bp exec --file login.json # Replay the recording
|
|
3476
|
+
|
|
3477
|
+
Great for creating initial automation scripts that AI agents can refine.
|
|
3478
|
+
|
|
3463
3479
|
Run 'bp actions' for the complete action reference.
|
|
3464
3480
|
`;
|
|
3465
3481
|
async function quickstartCommand() {
|
|
3466
3482
|
console.log(QUICKSTART);
|
|
3467
3483
|
}
|
|
3468
3484
|
|
|
3485
|
+
// src/recording/aggregator.ts
|
|
3486
|
+
var INPUT_DEBOUNCE_MS = 300;
|
|
3487
|
+
var NAVIGATION_DEBOUNCE_MS = 500;
|
|
3488
|
+
function selectBestSelectors(candidates) {
|
|
3489
|
+
const qualityOrder = {
|
|
3490
|
+
"role-name": 0,
|
|
3491
|
+
text: 1,
|
|
3492
|
+
"aria-label": 2,
|
|
3493
|
+
testid: 3,
|
|
3494
|
+
"stable-attr": 4,
|
|
3495
|
+
id: 5,
|
|
3496
|
+
"name-attr": 6,
|
|
3497
|
+
"css-path": 7
|
|
3498
|
+
};
|
|
3499
|
+
const sorted = [...candidates].sort((a, b) => {
|
|
3500
|
+
const aOrder = qualityOrder[a.quality] ?? 8;
|
|
3501
|
+
const bOrder = qualityOrder[b.quality] ?? 8;
|
|
3502
|
+
return aOrder - bOrder;
|
|
3503
|
+
});
|
|
3504
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3505
|
+
const result = [];
|
|
3506
|
+
for (const candidate of sorted) {
|
|
3507
|
+
if (!seen.has(candidate.selector)) {
|
|
3508
|
+
seen.add(candidate.selector);
|
|
3509
|
+
result.push(candidate.selector);
|
|
3510
|
+
}
|
|
3511
|
+
}
|
|
3512
|
+
return result;
|
|
3513
|
+
}
|
|
3514
|
+
function generateAnnotation(event) {
|
|
3515
|
+
const { kind, element, url } = event;
|
|
3516
|
+
const name = element?.accessibleName || element?.text || element?.ariaLabel;
|
|
3517
|
+
const role = element?.computedRole || element?.role || element?.tag || "";
|
|
3518
|
+
switch (kind) {
|
|
3519
|
+
case "click":
|
|
3520
|
+
if (name && role) {
|
|
3521
|
+
return `Clicked '${name}' ${role}`;
|
|
3522
|
+
} else if (name) {
|
|
3523
|
+
return `Clicked '${name}'`;
|
|
3524
|
+
} else if (role) {
|
|
3525
|
+
return `Clicked ${role}`;
|
|
3526
|
+
}
|
|
3527
|
+
return "Clicked element";
|
|
3528
|
+
case "dblclick":
|
|
3529
|
+
if (name && role) {
|
|
3530
|
+
return `Double-clicked '${name}' ${role}`;
|
|
3531
|
+
}
|
|
3532
|
+
return "Double-clicked element";
|
|
3533
|
+
case "input":
|
|
3534
|
+
if (name) {
|
|
3535
|
+
return `Filled '${name}' with value`;
|
|
3536
|
+
}
|
|
3537
|
+
return "Filled input with value";
|
|
3538
|
+
case "change":
|
|
3539
|
+
if (element?.type === "checkbox" || element?.type === "radio") {
|
|
3540
|
+
const action = event.checked ? "Checked" : "Unchecked";
|
|
3541
|
+
if (name) {
|
|
3542
|
+
return `${action} '${name}' ${element.type}`;
|
|
3543
|
+
}
|
|
3544
|
+
return `${action} ${element.type}`;
|
|
3545
|
+
}
|
|
3546
|
+
if (element?.tag === "select") {
|
|
3547
|
+
if (name) {
|
|
3548
|
+
return `Selected option in '${name}'`;
|
|
3549
|
+
}
|
|
3550
|
+
return "Selected option";
|
|
3551
|
+
}
|
|
3552
|
+
if (name) {
|
|
3553
|
+
return `Changed '${name}'`;
|
|
3554
|
+
}
|
|
3555
|
+
return "Changed element";
|
|
3556
|
+
case "submit":
|
|
3557
|
+
if (name) {
|
|
3558
|
+
return `Submitted '${name}' form`;
|
|
3559
|
+
}
|
|
3560
|
+
return "Submitted form";
|
|
3561
|
+
case "keydown":
|
|
3562
|
+
if (event.key === "Enter") {
|
|
3563
|
+
return "Pressed Enter";
|
|
3564
|
+
}
|
|
3565
|
+
return `Pressed ${event.key}`;
|
|
3566
|
+
case "navigation":
|
|
3567
|
+
return `Navigated to ${url}`;
|
|
3568
|
+
default:
|
|
3569
|
+
if (name) {
|
|
3570
|
+
return `${kind} on '${name}'`;
|
|
3571
|
+
}
|
|
3572
|
+
return `${kind} on element`;
|
|
3573
|
+
}
|
|
3574
|
+
}
|
|
3575
|
+
function debounceInputEvents(events) {
|
|
3576
|
+
const result = [];
|
|
3577
|
+
for (let i = 0; i < events.length; i++) {
|
|
3578
|
+
const event = events[i];
|
|
3579
|
+
if (event.kind !== "input") {
|
|
3580
|
+
result.push(event);
|
|
3581
|
+
continue;
|
|
3582
|
+
}
|
|
3583
|
+
const primarySelector = event.selectors[0]?.selector;
|
|
3584
|
+
if (!primarySelector) {
|
|
3585
|
+
result.push(event);
|
|
3586
|
+
continue;
|
|
3587
|
+
}
|
|
3588
|
+
let finalEvent = event;
|
|
3589
|
+
let j = i + 1;
|
|
3590
|
+
while (j < events.length) {
|
|
3591
|
+
const nextEvent = events[j];
|
|
3592
|
+
if (nextEvent.timestamp - finalEvent.timestamp > INPUT_DEBOUNCE_MS) {
|
|
3593
|
+
break;
|
|
3594
|
+
}
|
|
3595
|
+
if (nextEvent.kind !== "input") {
|
|
3596
|
+
break;
|
|
3597
|
+
}
|
|
3598
|
+
const nextPrimarySelector = nextEvent.selectors[0]?.selector;
|
|
3599
|
+
if (nextPrimarySelector !== primarySelector) {
|
|
3600
|
+
break;
|
|
3601
|
+
}
|
|
3602
|
+
finalEvent = nextEvent;
|
|
3603
|
+
j++;
|
|
3604
|
+
}
|
|
3605
|
+
i = j - 1;
|
|
3606
|
+
result.push(finalEvent);
|
|
3607
|
+
}
|
|
3608
|
+
return result;
|
|
3609
|
+
}
|
|
3610
|
+
function debounceNavigationEvents(events) {
|
|
3611
|
+
const result = [];
|
|
3612
|
+
for (let i = 0; i < events.length; i++) {
|
|
3613
|
+
const event = events[i];
|
|
3614
|
+
if (event.kind !== "navigation") {
|
|
3615
|
+
result.push(event);
|
|
3616
|
+
continue;
|
|
3617
|
+
}
|
|
3618
|
+
let finalEvent = event;
|
|
3619
|
+
let j = i + 1;
|
|
3620
|
+
while (j < events.length) {
|
|
3621
|
+
const nextEvent = events[j];
|
|
3622
|
+
if (nextEvent.timestamp - finalEvent.timestamp > NAVIGATION_DEBOUNCE_MS) {
|
|
3623
|
+
break;
|
|
3624
|
+
}
|
|
3625
|
+
if (nextEvent.kind !== "navigation") {
|
|
3626
|
+
break;
|
|
3627
|
+
}
|
|
3628
|
+
finalEvent = nextEvent;
|
|
3629
|
+
j++;
|
|
3630
|
+
}
|
|
3631
|
+
i = j - 1;
|
|
3632
|
+
result.push(finalEvent);
|
|
3633
|
+
}
|
|
3634
|
+
return result;
|
|
3635
|
+
}
|
|
3636
|
+
function insertNavigationSteps(events, startUrl) {
|
|
3637
|
+
const result = [];
|
|
3638
|
+
let lastUrl = startUrl || null;
|
|
3639
|
+
for (const event of events) {
|
|
3640
|
+
if (lastUrl !== null && event.url !== lastUrl) {
|
|
3641
|
+
result.push({
|
|
3642
|
+
kind: "navigation",
|
|
3643
|
+
timestamp: event.timestamp,
|
|
3644
|
+
url: event.url,
|
|
3645
|
+
selectors: []
|
|
3646
|
+
});
|
|
3647
|
+
}
|
|
3648
|
+
result.push(event);
|
|
3649
|
+
lastUrl = event.url;
|
|
3650
|
+
}
|
|
3651
|
+
return result;
|
|
3652
|
+
}
|
|
3653
|
+
function buildElementMeta(event) {
|
|
3654
|
+
const el = event.element;
|
|
3655
|
+
if (!el) return void 0;
|
|
3656
|
+
return {
|
|
3657
|
+
role: el.computedRole || el.role,
|
|
3658
|
+
name: el.accessibleName || el.text || el.ariaLabel,
|
|
3659
|
+
tag: el.tag
|
|
3660
|
+
};
|
|
3661
|
+
}
|
|
3662
|
+
function eventToStep(event) {
|
|
3663
|
+
const selectors = selectBestSelectors(event.selectors);
|
|
3664
|
+
const elementMeta = buildElementMeta(event);
|
|
3665
|
+
const annotation = generateAnnotation(event);
|
|
3666
|
+
switch (event.kind) {
|
|
3667
|
+
case "click":
|
|
3668
|
+
case "dblclick":
|
|
3669
|
+
if (selectors.length === 0) return null;
|
|
3670
|
+
return {
|
|
3671
|
+
action: "click",
|
|
3672
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3673
|
+
element: elementMeta,
|
|
3674
|
+
annotation
|
|
3675
|
+
};
|
|
3676
|
+
case "input":
|
|
3677
|
+
if (selectors.length === 0) return null;
|
|
3678
|
+
return {
|
|
3679
|
+
action: "fill",
|
|
3680
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3681
|
+
value: event.value ?? "",
|
|
3682
|
+
element: elementMeta,
|
|
3683
|
+
annotation
|
|
3684
|
+
};
|
|
3685
|
+
case "change": {
|
|
3686
|
+
if (selectors.length === 0) return null;
|
|
3687
|
+
const element = event.element;
|
|
3688
|
+
const tag = element?.tag;
|
|
3689
|
+
const type = element?.type?.toLowerCase();
|
|
3690
|
+
if (tag === "select") {
|
|
3691
|
+
return {
|
|
3692
|
+
action: "select",
|
|
3693
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3694
|
+
value: event.value ?? "",
|
|
3695
|
+
element: elementMeta,
|
|
3696
|
+
annotation
|
|
3697
|
+
};
|
|
3698
|
+
}
|
|
3699
|
+
if (type === "checkbox" || type === "radio") {
|
|
3700
|
+
return {
|
|
3701
|
+
action: event.checked ? "check" : "uncheck",
|
|
3702
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3703
|
+
element: elementMeta,
|
|
3704
|
+
annotation
|
|
3705
|
+
};
|
|
3706
|
+
}
|
|
3707
|
+
return {
|
|
3708
|
+
action: "fill",
|
|
3709
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3710
|
+
value: event.value ?? "",
|
|
3711
|
+
element: elementMeta,
|
|
3712
|
+
annotation
|
|
3713
|
+
};
|
|
3714
|
+
}
|
|
3715
|
+
case "keydown":
|
|
3716
|
+
if (event.key === "Enter") {
|
|
3717
|
+
if (selectors.length === 0) return null;
|
|
3718
|
+
return {
|
|
3719
|
+
action: "submit",
|
|
3720
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3721
|
+
method: "enter",
|
|
3722
|
+
element: elementMeta,
|
|
3723
|
+
annotation
|
|
3724
|
+
};
|
|
3725
|
+
}
|
|
3726
|
+
return null;
|
|
3727
|
+
case "submit":
|
|
3728
|
+
if (selectors.length === 0) return null;
|
|
3729
|
+
return {
|
|
3730
|
+
action: "submit",
|
|
3731
|
+
selector: selectors.length === 1 ? selectors[0] : selectors,
|
|
3732
|
+
element: elementMeta,
|
|
3733
|
+
annotation
|
|
3734
|
+
};
|
|
3735
|
+
case "navigation":
|
|
3736
|
+
return {
|
|
3737
|
+
action: "goto",
|
|
3738
|
+
url: event.url,
|
|
3739
|
+
annotation
|
|
3740
|
+
};
|
|
3741
|
+
default:
|
|
3742
|
+
return null;
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
function deduplicateSteps(steps) {
|
|
3746
|
+
const result = [];
|
|
3747
|
+
for (let i = 0; i < steps.length; i++) {
|
|
3748
|
+
const step = steps[i];
|
|
3749
|
+
const prevStep = result[result.length - 1];
|
|
3750
|
+
if (step.action === "submit" && prevStep?.action === "submit" && JSON.stringify(step.selector) === JSON.stringify(prevStep.selector)) {
|
|
3751
|
+
continue;
|
|
3752
|
+
}
|
|
3753
|
+
result.push(step);
|
|
3754
|
+
}
|
|
3755
|
+
return result;
|
|
3756
|
+
}
|
|
3757
|
+
function aggregateEvents(events, startUrl) {
|
|
3758
|
+
if (events.length === 0) return [];
|
|
3759
|
+
let processed = insertNavigationSteps(events, startUrl);
|
|
3760
|
+
processed = debounceNavigationEvents(processed);
|
|
3761
|
+
processed = debounceInputEvents(processed);
|
|
3762
|
+
const steps = [];
|
|
3763
|
+
for (const event of processed) {
|
|
3764
|
+
const step = eventToStep(event);
|
|
3765
|
+
if (step) {
|
|
3766
|
+
steps.push(step);
|
|
3767
|
+
}
|
|
3768
|
+
}
|
|
3769
|
+
return deduplicateSteps(steps);
|
|
3770
|
+
}
|
|
3771
|
+
|
|
3772
|
+
// src/recording/script.ts
|
|
3773
|
+
var RECORDER_BINDING_NAME = "__recorder";
|
|
3774
|
+
var RECORDER_SCRIPT = `(function() {
|
|
3775
|
+
// Guard against multiple installations
|
|
3776
|
+
if (window.__recorderInstalled) return;
|
|
3777
|
+
window.__recorderInstalled = true;
|
|
3778
|
+
|
|
3779
|
+
const BINDING_NAME = '__recorder';
|
|
3780
|
+
|
|
3781
|
+
// Safe JSON stringify
|
|
3782
|
+
function safeJson(obj) {
|
|
3783
|
+
try {
|
|
3784
|
+
return JSON.stringify(obj);
|
|
3785
|
+
} catch (e) {
|
|
3786
|
+
return JSON.stringify({ error: 'unserializable' });
|
|
3787
|
+
}
|
|
3788
|
+
}
|
|
3789
|
+
|
|
3790
|
+
// Send event to CDP client via binding
|
|
3791
|
+
function sendEvent(payload) {
|
|
3792
|
+
try {
|
|
3793
|
+
if (typeof window[BINDING_NAME] === 'function') {
|
|
3794
|
+
window[BINDING_NAME](safeJson(payload));
|
|
3795
|
+
}
|
|
3796
|
+
} catch (e) {
|
|
3797
|
+
// Binding not ready, ignore
|
|
3798
|
+
}
|
|
3799
|
+
}
|
|
3800
|
+
|
|
3801
|
+
// CSS escape for identifiers
|
|
3802
|
+
function cssEscape(str) {
|
|
3803
|
+
return String(str).replace(/([\\[\\]#.:>+~=|^$*!"'(){}])/g, '\\\\$1');
|
|
3804
|
+
}
|
|
3805
|
+
|
|
3806
|
+
// Check if selector is unique in document
|
|
3807
|
+
function isUnique(selector, root) {
|
|
3808
|
+
try {
|
|
3809
|
+
return (root || document).querySelectorAll(selector).length === 1;
|
|
3810
|
+
} catch (e) {
|
|
3811
|
+
return false;
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
|
|
3815
|
+
// Get stable attribute selector (data-testid, aria-label, name, etc.)
|
|
3816
|
+
function getStableAttrSelector(el) {
|
|
3817
|
+
if (!el || el.nodeType !== 1) return null;
|
|
3818
|
+
const attrs = ['data-testid', 'data-test', 'data-qa', 'aria-label', 'name'];
|
|
3819
|
+
for (const attr of attrs) {
|
|
3820
|
+
const val = el.getAttribute(attr);
|
|
3821
|
+
if (val && val.length <= 200) {
|
|
3822
|
+
const escaped = val.replace(/"/g, '\\\\"');
|
|
3823
|
+
return '[' + attr + '="' + escaped + '"]';
|
|
3824
|
+
}
|
|
3825
|
+
}
|
|
3826
|
+
return null;
|
|
3827
|
+
}
|
|
3828
|
+
|
|
3829
|
+
// Get ID selector
|
|
3830
|
+
function getIdSelector(el) {
|
|
3831
|
+
if (!el || !el.id || el.id.length > 100) return null;
|
|
3832
|
+
// Skip dynamic-looking IDs
|
|
3833
|
+
if (/^[0-9]|^:/.test(el.id)) return null;
|
|
3834
|
+
return '#' + cssEscape(el.id);
|
|
3835
|
+
}
|
|
3836
|
+
|
|
3837
|
+
// Build CSS path for element
|
|
3838
|
+
function buildCssPath(el) {
|
|
3839
|
+
if (!el || el.nodeType !== 1) return null;
|
|
3840
|
+
const parts = [];
|
|
3841
|
+
let cur = el;
|
|
3842
|
+
|
|
3843
|
+
for (let depth = 0; cur && cur !== document.body && depth < 8; depth++) {
|
|
3844
|
+
let part = cur.tagName.toLowerCase();
|
|
3845
|
+
|
|
3846
|
+
// If ID exists and looks stable, use it and stop
|
|
3847
|
+
if (cur.id && !/^[0-9]|^:/.test(cur.id) && cur.id.length <= 50) {
|
|
3848
|
+
part = '#' + cssEscape(cur.id);
|
|
3849
|
+
parts.unshift(part);
|
|
3850
|
+
break;
|
|
3851
|
+
}
|
|
3852
|
+
|
|
3853
|
+
// Add stable classes (skip dynamic ones)
|
|
3854
|
+
const classes = Array.from(cur.classList || [])
|
|
3855
|
+
.filter(c => c.length < 40 && !/^css-|^_|^[0-9]/.test(c))
|
|
3856
|
+
.slice(0, 2);
|
|
3857
|
+
if (classes.length) {
|
|
3858
|
+
part += '.' + classes.map(cssEscape).join('.');
|
|
3859
|
+
}
|
|
3860
|
+
|
|
3861
|
+
// Add position if siblings have same tag
|
|
3862
|
+
const parent = cur.parentElement;
|
|
3863
|
+
if (parent) {
|
|
3864
|
+
const sameTag = Array.from(parent.children).filter(c => c.tagName === cur.tagName);
|
|
3865
|
+
if (sameTag.length > 1) {
|
|
3866
|
+
const idx = sameTag.indexOf(cur) + 1;
|
|
3867
|
+
part += ':nth-of-type(' + idx + ')';
|
|
3868
|
+
}
|
|
3869
|
+
}
|
|
3870
|
+
|
|
3871
|
+
parts.unshift(part);
|
|
3872
|
+
cur = cur.parentElement;
|
|
3873
|
+
}
|
|
3874
|
+
|
|
3875
|
+
return parts.join(' > ');
|
|
3876
|
+
}
|
|
3877
|
+
|
|
3878
|
+
// Generate selector candidates ordered by quality
|
|
3879
|
+
function getSelectorCandidates(el) {
|
|
3880
|
+
const candidates = [];
|
|
3881
|
+
|
|
3882
|
+
// Get semantic info for role-based selectors
|
|
3883
|
+
const role = getRole(el);
|
|
3884
|
+
const name = getAccessibleName(el);
|
|
3885
|
+
|
|
3886
|
+
// 1. Role + name selector (highest priority for semantic elements)
|
|
3887
|
+
if (role && name) {
|
|
3888
|
+
const escapedName = name.replace(/'/g, "\\\\'");
|
|
3889
|
+
candidates.push({
|
|
3890
|
+
selector: "role=" + role + "[name='" + escapedName + "']",
|
|
3891
|
+
quality: 'role-name'
|
|
3892
|
+
});
|
|
3893
|
+
}
|
|
3894
|
+
|
|
3895
|
+
// 2. Text-based selector (for buttons, links, menuitems)
|
|
3896
|
+
if (name && ['button', 'link', 'menuitem'].includes(role)) {
|
|
3897
|
+
candidates.push({
|
|
3898
|
+
selector: "text=" + name,
|
|
3899
|
+
quality: 'text'
|
|
3900
|
+
});
|
|
3901
|
+
}
|
|
3902
|
+
|
|
3903
|
+
// 3. aria-label attribute selector
|
|
3904
|
+
const ariaLabel = el.getAttribute('aria-label');
|
|
3905
|
+
if (ariaLabel) {
|
|
3906
|
+
const escaped = ariaLabel.replace(/"/g, '\\\\"');
|
|
3907
|
+
candidates.push({
|
|
3908
|
+
selector: '[aria-label="' + escaped + '"]',
|
|
3909
|
+
quality: 'aria-label'
|
|
3910
|
+
});
|
|
3911
|
+
}
|
|
3912
|
+
|
|
3913
|
+
// 4. Stable attributes (testid, name)
|
|
3914
|
+
const stableAttr = getStableAttrSelector(el);
|
|
3915
|
+
if (stableAttr) {
|
|
3916
|
+
candidates.push({ selector: stableAttr, quality: 'stable-attr' });
|
|
3917
|
+
}
|
|
3918
|
+
|
|
3919
|
+
// 5. ID selector
|
|
3920
|
+
const idSel = getIdSelector(el);
|
|
3921
|
+
if (idSel) {
|
|
3922
|
+
candidates.push({ selector: idSel, quality: 'id' });
|
|
3923
|
+
}
|
|
3924
|
+
|
|
3925
|
+
// 6. CSS path (fallback)
|
|
3926
|
+
const cssPath = buildCssPath(el);
|
|
3927
|
+
if (cssPath) {
|
|
3928
|
+
candidates.push({ selector: cssPath, quality: 'css-path' });
|
|
3929
|
+
}
|
|
3930
|
+
|
|
3931
|
+
return candidates;
|
|
3932
|
+
}
|
|
3933
|
+
|
|
3934
|
+
// Compute accessible name per W3C AccName spec
|
|
3935
|
+
// Priority: aria-labelledby > aria-label > label > title > content > alt > placeholder
|
|
3936
|
+
function getAccessibleName(el) {
|
|
3937
|
+
if (!el || el.nodeType !== 1) return null;
|
|
3938
|
+
|
|
3939
|
+
// 1. aria-labelledby
|
|
3940
|
+
const labelledBy = el.getAttribute('aria-labelledby');
|
|
3941
|
+
if (labelledBy) {
|
|
3942
|
+
const labels = labelledBy.split(/\\s+/)
|
|
3943
|
+
.map(function(id) {
|
|
3944
|
+
const ref = document.getElementById(id);
|
|
3945
|
+
return ref ? ref.textContent : null;
|
|
3946
|
+
})
|
|
3947
|
+
.filter(Boolean);
|
|
3948
|
+
if (labels.length) return labels.join(' ').trim().slice(0, 100);
|
|
3949
|
+
}
|
|
3950
|
+
|
|
3951
|
+
// 2. aria-label
|
|
3952
|
+
const ariaLabel = el.getAttribute('aria-label');
|
|
3953
|
+
if (ariaLabel) return ariaLabel.trim().slice(0, 100);
|
|
3954
|
+
|
|
3955
|
+
// 3. Native <label> for form elements
|
|
3956
|
+
if (el.labels && el.labels.length) {
|
|
3957
|
+
const labelTexts = Array.from(el.labels)
|
|
3958
|
+
.map(function(l) { return l.textContent; })
|
|
3959
|
+
.filter(Boolean);
|
|
3960
|
+
if (labelTexts.length) return labelTexts.join(' ').trim().slice(0, 100);
|
|
3961
|
+
}
|
|
3962
|
+
|
|
3963
|
+
// 4. title attribute
|
|
3964
|
+
const title = el.getAttribute('title');
|
|
3965
|
+
if (title) return title.trim().slice(0, 100);
|
|
3966
|
+
|
|
3967
|
+
// 5. Content for buttons, links, summary
|
|
3968
|
+
const tag = el.tagName.toLowerCase();
|
|
3969
|
+
const role = el.getAttribute('role');
|
|
3970
|
+
if (['button', 'a', 'summary'].includes(tag) || role === 'button' || role === 'link' || role === 'menuitem') {
|
|
3971
|
+
const text = (el.textContent || '').trim();
|
|
3972
|
+
if (text) return text.slice(0, 100);
|
|
3973
|
+
}
|
|
3974
|
+
|
|
3975
|
+
// 6. alt for images
|
|
3976
|
+
if (tag === 'img') {
|
|
3977
|
+
const alt = el.getAttribute('alt');
|
|
3978
|
+
if (alt) return alt.trim().slice(0, 100);
|
|
3979
|
+
}
|
|
3980
|
+
|
|
3981
|
+
// 7. placeholder for inputs
|
|
3982
|
+
if (['input', 'textarea'].includes(tag)) {
|
|
3983
|
+
const placeholder = el.getAttribute('placeholder');
|
|
3984
|
+
if (placeholder) return placeholder.trim().slice(0, 100);
|
|
3985
|
+
}
|
|
3986
|
+
|
|
3987
|
+
return null;
|
|
3988
|
+
}
|
|
3989
|
+
|
|
3990
|
+
// Get explicit ARIA role or implicit role from HTML tag
|
|
3991
|
+
function getRole(el) {
|
|
3992
|
+
if (!el || el.nodeType !== 1) return null;
|
|
3993
|
+
|
|
3994
|
+
// 1. Explicit role attribute
|
|
3995
|
+
const explicitRole = el.getAttribute('role');
|
|
3996
|
+
if (explicitRole) return explicitRole;
|
|
3997
|
+
|
|
3998
|
+
// 2. Implicit role from tag/type
|
|
3999
|
+
const tag = el.tagName.toLowerCase();
|
|
4000
|
+
const type = (el.getAttribute('type') || '').toLowerCase();
|
|
4001
|
+
|
|
4002
|
+
// Input types to roles
|
|
4003
|
+
if (tag === 'input') {
|
|
4004
|
+
var inputRoles = {
|
|
4005
|
+
'button': 'button',
|
|
4006
|
+
'submit': 'button',
|
|
4007
|
+
'reset': 'button',
|
|
4008
|
+
'image': 'button',
|
|
4009
|
+
'checkbox': 'checkbox',
|
|
4010
|
+
'radio': 'radio',
|
|
4011
|
+
'range': 'slider',
|
|
4012
|
+
'search': 'searchbox'
|
|
4013
|
+
};
|
|
4014
|
+
if (inputRoles[type]) return inputRoles[type];
|
|
4015
|
+
// text, email, tel, url, number, password all map to textbox
|
|
4016
|
+
return 'textbox';
|
|
4017
|
+
}
|
|
4018
|
+
|
|
4019
|
+
// Other tags with implicit roles
|
|
4020
|
+
var tagRoles = {
|
|
4021
|
+
'button': 'button',
|
|
4022
|
+
'select': 'combobox',
|
|
4023
|
+
'textarea': 'textbox',
|
|
4024
|
+
'nav': 'navigation',
|
|
4025
|
+
'main': 'main',
|
|
4026
|
+
'header': 'banner',
|
|
4027
|
+
'footer': 'contentinfo',
|
|
4028
|
+
'aside': 'complementary',
|
|
4029
|
+
'article': 'article',
|
|
4030
|
+
'ul': 'list',
|
|
4031
|
+
'ol': 'list',
|
|
4032
|
+
'li': 'listitem',
|
|
4033
|
+
'table': 'table',
|
|
4034
|
+
'tr': 'row',
|
|
4035
|
+
'td': 'cell',
|
|
4036
|
+
'th': 'columnheader',
|
|
4037
|
+
'form': 'form',
|
|
4038
|
+
'img': 'img',
|
|
4039
|
+
'dialog': 'dialog',
|
|
4040
|
+
'menu': 'menu',
|
|
4041
|
+
'summary': 'button'
|
|
4042
|
+
};
|
|
4043
|
+
if (tagRoles[tag]) return tagRoles[tag];
|
|
4044
|
+
|
|
4045
|
+
// Anchor with href is a link
|
|
4046
|
+
if (tag === 'a' && el.hasAttribute('href')) return 'link';
|
|
4047
|
+
|
|
4048
|
+
// Section with aria-label or aria-labelledby is a region
|
|
4049
|
+
if (tag === 'section' && (el.hasAttribute('aria-label') || el.hasAttribute('aria-labelledby'))) {
|
|
4050
|
+
return 'region';
|
|
4051
|
+
}
|
|
4052
|
+
|
|
4053
|
+
return null;
|
|
4054
|
+
}
|
|
4055
|
+
|
|
4056
|
+
// Get element summary for debugging
|
|
4057
|
+
function getElementSummary(el) {
|
|
4058
|
+
if (!el || el.nodeType !== 1) return null;
|
|
4059
|
+
const text = (el.innerText || '').trim().replace(/\\s+/g, ' ').slice(0, 120);
|
|
4060
|
+
return {
|
|
4061
|
+
tag: el.tagName.toLowerCase(),
|
|
4062
|
+
id: el.id || null,
|
|
4063
|
+
name: el.getAttribute('name') || null,
|
|
4064
|
+
type: el.getAttribute('type') || null,
|
|
4065
|
+
role: el.getAttribute('role') || null,
|
|
4066
|
+
ariaLabel: el.getAttribute('aria-label') || null,
|
|
4067
|
+
testid: el.getAttribute('data-testid') || null,
|
|
4068
|
+
text: text || null,
|
|
4069
|
+
accessibleName: getAccessibleName(el),
|
|
4070
|
+
computedRole: getRole(el)
|
|
4071
|
+
};
|
|
4072
|
+
}
|
|
4073
|
+
|
|
4074
|
+
// Get event target, handling shadow DOM via composedPath
|
|
4075
|
+
function getEventTarget(ev) {
|
|
4076
|
+
const path = ev.composedPath ? ev.composedPath() : null;
|
|
4077
|
+
if (path && path.length > 0) {
|
|
4078
|
+
for (const node of path) {
|
|
4079
|
+
if (node && node.nodeType === 1) return node;
|
|
4080
|
+
}
|
|
4081
|
+
}
|
|
4082
|
+
return ev.target && ev.target.nodeType === 1 ? ev.target : null;
|
|
4083
|
+
}
|
|
4084
|
+
|
|
4085
|
+
// Find clickable ancestor (button, a, [role=button])
|
|
4086
|
+
function findClickableAncestor(el) {
|
|
4087
|
+
if (!el) return el;
|
|
4088
|
+
const clickable = el.closest('button, a, [role="button"], [role="link"]');
|
|
4089
|
+
return clickable || el;
|
|
4090
|
+
}
|
|
4091
|
+
|
|
4092
|
+
// Check if element is a password input
|
|
4093
|
+
function isPasswordInput(el) {
|
|
4094
|
+
if (!el) return false;
|
|
4095
|
+
const tag = el.tagName.toLowerCase();
|
|
4096
|
+
if (tag !== 'input') return false;
|
|
4097
|
+
const type = (el.getAttribute('type') || '').toLowerCase();
|
|
4098
|
+
return type === 'password';
|
|
4099
|
+
}
|
|
4100
|
+
|
|
4101
|
+
// Get input value, redacting passwords
|
|
4102
|
+
function getInputValue(el) {
|
|
4103
|
+
if (isPasswordInput(el)) return '[REDACTED]';
|
|
4104
|
+
if (el.value !== undefined) return el.value;
|
|
4105
|
+
if (el.isContentEditable) return el.textContent || '';
|
|
4106
|
+
return '';
|
|
4107
|
+
}
|
|
4108
|
+
|
|
4109
|
+
// Current timestamp
|
|
4110
|
+
function now() { return Date.now(); }
|
|
4111
|
+
|
|
4112
|
+
// Click handler
|
|
4113
|
+
window.addEventListener('click', function(ev) {
|
|
4114
|
+
const rawTarget = getEventTarget(ev);
|
|
4115
|
+
if (!rawTarget) return;
|
|
4116
|
+
|
|
4117
|
+
// Bubble up to clickable ancestor for better selectors
|
|
4118
|
+
const el = findClickableAncestor(rawTarget);
|
|
4119
|
+
|
|
4120
|
+
sendEvent({
|
|
4121
|
+
kind: 'click',
|
|
4122
|
+
timestamp: now(),
|
|
4123
|
+
url: location.href,
|
|
4124
|
+
element: getElementSummary(el),
|
|
4125
|
+
selectors: getSelectorCandidates(el),
|
|
4126
|
+
client: { x: ev.clientX, y: ev.clientY }
|
|
4127
|
+
});
|
|
4128
|
+
}, true);
|
|
4129
|
+
|
|
4130
|
+
// Double click handler
|
|
4131
|
+
window.addEventListener('dblclick', function(ev) {
|
|
4132
|
+
const rawTarget = getEventTarget(ev);
|
|
4133
|
+
if (!rawTarget) return;
|
|
4134
|
+
|
|
4135
|
+
const el = findClickableAncestor(rawTarget);
|
|
4136
|
+
|
|
4137
|
+
sendEvent({
|
|
4138
|
+
kind: 'dblclick',
|
|
4139
|
+
timestamp: now(),
|
|
4140
|
+
url: location.href,
|
|
4141
|
+
element: getElementSummary(el),
|
|
4142
|
+
selectors: getSelectorCandidates(el),
|
|
4143
|
+
client: { x: ev.clientX, y: ev.clientY }
|
|
4144
|
+
});
|
|
4145
|
+
}, true);
|
|
4146
|
+
|
|
4147
|
+
// Input handler (for text inputs, textareas, contenteditable)
|
|
4148
|
+
window.addEventListener('input', function(ev) {
|
|
4149
|
+
const el = getEventTarget(ev);
|
|
4150
|
+
if (!el) return;
|
|
4151
|
+
|
|
4152
|
+
const tag = el.tagName.toLowerCase();
|
|
4153
|
+
const isTexty = tag === 'input' || tag === 'textarea' || el.isContentEditable;
|
|
4154
|
+
if (!isTexty) return;
|
|
4155
|
+
|
|
4156
|
+
sendEvent({
|
|
4157
|
+
kind: 'input',
|
|
4158
|
+
timestamp: now(),
|
|
4159
|
+
url: location.href,
|
|
4160
|
+
element: getElementSummary(el),
|
|
4161
|
+
selectors: getSelectorCandidates(el),
|
|
4162
|
+
value: getInputValue(el)
|
|
4163
|
+
});
|
|
4164
|
+
}, true);
|
|
4165
|
+
|
|
4166
|
+
// Change handler (for select, checkbox, radio)
|
|
4167
|
+
window.addEventListener('change', function(ev) {
|
|
4168
|
+
const el = getEventTarget(ev);
|
|
4169
|
+
if (!el) return;
|
|
4170
|
+
|
|
4171
|
+
const tag = el.tagName.toLowerCase();
|
|
4172
|
+
const type = (el.getAttribute('type') || '').toLowerCase();
|
|
4173
|
+
const isCheckable = type === 'checkbox' || type === 'radio';
|
|
4174
|
+
|
|
4175
|
+
sendEvent({
|
|
4176
|
+
kind: 'change',
|
|
4177
|
+
timestamp: now(),
|
|
4178
|
+
url: location.href,
|
|
4179
|
+
element: getElementSummary(el),
|
|
4180
|
+
selectors: getSelectorCandidates(el),
|
|
4181
|
+
value: isCheckable ? undefined : getInputValue(el),
|
|
4182
|
+
checked: isCheckable ? el.checked : undefined
|
|
4183
|
+
});
|
|
4184
|
+
}, true);
|
|
4185
|
+
|
|
4186
|
+
// Keydown handler (capture Enter for form submission)
|
|
4187
|
+
window.addEventListener('keydown', function(ev) {
|
|
4188
|
+
if (ev.key !== 'Enter') return;
|
|
4189
|
+
|
|
4190
|
+
const el = getEventTarget(ev);
|
|
4191
|
+
|
|
4192
|
+
sendEvent({
|
|
4193
|
+
kind: 'keydown',
|
|
4194
|
+
timestamp: now(),
|
|
4195
|
+
url: location.href,
|
|
4196
|
+
key: ev.key,
|
|
4197
|
+
element: el ? getElementSummary(el) : null,
|
|
4198
|
+
selectors: el ? getSelectorCandidates(el) : []
|
|
4199
|
+
});
|
|
4200
|
+
}, true);
|
|
4201
|
+
|
|
4202
|
+
// Submit handler
|
|
4203
|
+
window.addEventListener('submit', function(ev) {
|
|
4204
|
+
const el = getEventTarget(ev);
|
|
4205
|
+
|
|
4206
|
+
sendEvent({
|
|
4207
|
+
kind: 'submit',
|
|
4208
|
+
timestamp: now(),
|
|
4209
|
+
url: location.href,
|
|
4210
|
+
element: el ? getElementSummary(el) : null,
|
|
4211
|
+
selectors: el ? getSelectorCandidates(el) : []
|
|
4212
|
+
});
|
|
4213
|
+
}, true);
|
|
4214
|
+
})();`;
|
|
4215
|
+
|
|
4216
|
+
// src/recording/recorder.ts
|
|
4217
|
+
var Recorder = class {
|
|
4218
|
+
cdp;
|
|
4219
|
+
events = [];
|
|
4220
|
+
recording = false;
|
|
4221
|
+
startTime = 0;
|
|
4222
|
+
startUrl = "";
|
|
4223
|
+
bindingHandler = null;
|
|
4224
|
+
constructor(cdp) {
|
|
4225
|
+
this.cdp = cdp;
|
|
4226
|
+
}
|
|
4227
|
+
/**
|
|
4228
|
+
* Check if recording is currently active.
|
|
4229
|
+
*/
|
|
4230
|
+
get isRecording() {
|
|
4231
|
+
return this.recording;
|
|
4232
|
+
}
|
|
4233
|
+
/**
|
|
4234
|
+
* Start recording browser interactions.
|
|
4235
|
+
*
|
|
4236
|
+
* Sets up CDP bindings and injects the recorder script into
|
|
4237
|
+
* the current page and all future navigations.
|
|
4238
|
+
*/
|
|
4239
|
+
async start() {
|
|
4240
|
+
if (this.recording) {
|
|
4241
|
+
throw new Error("Recording already in progress");
|
|
4242
|
+
}
|
|
4243
|
+
this.events = [];
|
|
4244
|
+
this.startTime = Date.now();
|
|
4245
|
+
this.recording = true;
|
|
4246
|
+
await this.cdp.send("Runtime.enable");
|
|
4247
|
+
await this.cdp.send("Page.enable");
|
|
4248
|
+
try {
|
|
4249
|
+
const result = await this.cdp.send("Runtime.evaluate", {
|
|
4250
|
+
expression: "location.href",
|
|
4251
|
+
returnByValue: true
|
|
4252
|
+
});
|
|
4253
|
+
this.startUrl = result.result.value;
|
|
4254
|
+
} catch {
|
|
4255
|
+
this.startUrl = "";
|
|
4256
|
+
}
|
|
4257
|
+
await this.cdp.send("Runtime.addBinding", { name: RECORDER_BINDING_NAME });
|
|
4258
|
+
await this.cdp.send("Page.addScriptToEvaluateOnNewDocument", {
|
|
4259
|
+
source: RECORDER_SCRIPT
|
|
4260
|
+
});
|
|
4261
|
+
await this.cdp.send("Runtime.evaluate", {
|
|
4262
|
+
expression: RECORDER_SCRIPT,
|
|
4263
|
+
awaitPromise: false
|
|
4264
|
+
});
|
|
4265
|
+
this.bindingHandler = (params) => {
|
|
4266
|
+
if (params["name"] === RECORDER_BINDING_NAME) {
|
|
4267
|
+
this.handleBindingCall(params["payload"]);
|
|
4268
|
+
}
|
|
4269
|
+
};
|
|
4270
|
+
this.cdp.on("Runtime.bindingCalled", this.bindingHandler);
|
|
4271
|
+
}
|
|
4272
|
+
/**
|
|
4273
|
+
* Stop recording and return aggregated output.
|
|
4274
|
+
*
|
|
4275
|
+
* Returns a RecordingOutput with steps compatible with page.batch().
|
|
4276
|
+
*/
|
|
4277
|
+
async stop() {
|
|
4278
|
+
if (!this.recording) {
|
|
4279
|
+
throw new Error("No recording in progress");
|
|
4280
|
+
}
|
|
4281
|
+
this.recording = false;
|
|
4282
|
+
const duration = Date.now() - this.startTime;
|
|
4283
|
+
if (this.bindingHandler) {
|
|
4284
|
+
this.cdp.off("Runtime.bindingCalled", this.bindingHandler);
|
|
4285
|
+
this.bindingHandler = null;
|
|
4286
|
+
}
|
|
4287
|
+
const steps = aggregateEvents(this.events, this.startUrl);
|
|
4288
|
+
return {
|
|
4289
|
+
recordedAt: new Date(this.startTime).toISOString(),
|
|
4290
|
+
startUrl: this.startUrl,
|
|
4291
|
+
duration,
|
|
4292
|
+
steps
|
|
4293
|
+
};
|
|
4294
|
+
}
|
|
4295
|
+
/**
|
|
4296
|
+
* Get raw recorded events (for debugging).
|
|
4297
|
+
*/
|
|
4298
|
+
getEvents() {
|
|
4299
|
+
return [...this.events];
|
|
4300
|
+
}
|
|
4301
|
+
/**
|
|
4302
|
+
* Handle incoming binding call from the browser.
|
|
4303
|
+
*/
|
|
4304
|
+
handleBindingCall(payload) {
|
|
4305
|
+
if (!this.recording) return;
|
|
4306
|
+
try {
|
|
4307
|
+
const event = JSON.parse(payload);
|
|
4308
|
+
this.events.push(event);
|
|
4309
|
+
} catch {
|
|
4310
|
+
}
|
|
4311
|
+
}
|
|
4312
|
+
};
|
|
4313
|
+
|
|
4314
|
+
// src/cli/commands/record.ts
|
|
4315
|
+
var RECORD_HELP = `
|
|
4316
|
+
bp record - Record browser actions to JSON
|
|
4317
|
+
|
|
4318
|
+
Usage:
|
|
4319
|
+
bp record [options]
|
|
4320
|
+
|
|
4321
|
+
Options:
|
|
4322
|
+
-s, --session [id] Session to use:
|
|
4323
|
+
- omit -s: auto-connect to local browser
|
|
4324
|
+
- -s alone: use most recent session
|
|
4325
|
+
- -s <id>: use specific session
|
|
4326
|
+
-f, --file <path> Output file (default: recording.json)
|
|
4327
|
+
--timeout <ms> Auto-stop after timeout (optional)
|
|
4328
|
+
-h, --help Show this help
|
|
4329
|
+
|
|
4330
|
+
Examples:
|
|
4331
|
+
bp record # Auto-connect to local Chrome
|
|
4332
|
+
bp record -s # Use most recent session
|
|
4333
|
+
bp record -s mysession # Use specific session
|
|
4334
|
+
bp record -f login.json # Save to specific file
|
|
4335
|
+
bp record --timeout 60000 # Auto-stop after 60s
|
|
4336
|
+
|
|
4337
|
+
Recording captures: clicks, inputs, form submissions, navigation.
|
|
4338
|
+
Password fields are automatically redacted as [REDACTED].
|
|
4339
|
+
|
|
4340
|
+
Press Ctrl+C to stop recording and save.
|
|
4341
|
+
`;
|
|
4342
|
+
function parseRecordArgs(args) {
|
|
4343
|
+
const options = {};
|
|
4344
|
+
for (let i = 0; i < args.length; i++) {
|
|
4345
|
+
const arg = args[i];
|
|
4346
|
+
if (arg === "-f" || arg === "--file") {
|
|
4347
|
+
options.file = args[++i];
|
|
4348
|
+
} else if (arg === "--timeout") {
|
|
4349
|
+
options.timeout = Number.parseInt(args[++i] ?? "", 10);
|
|
4350
|
+
} else if (arg === "-h" || arg === "--help") {
|
|
4351
|
+
options.help = true;
|
|
4352
|
+
} else if (arg === "-s" || arg === "--session") {
|
|
4353
|
+
const nextArg = args[i + 1];
|
|
4354
|
+
if (!nextArg || nextArg.startsWith("-")) {
|
|
4355
|
+
options.useLatestSession = true;
|
|
4356
|
+
}
|
|
4357
|
+
}
|
|
4358
|
+
}
|
|
4359
|
+
return options;
|
|
4360
|
+
}
|
|
4361
|
+
async function resolveConnection(sessionId, useLatestSession, trace) {
|
|
4362
|
+
if (sessionId) {
|
|
4363
|
+
const session2 = await loadSession(sessionId);
|
|
4364
|
+
const browser2 = await connect({
|
|
4365
|
+
provider: session2.provider,
|
|
4366
|
+
wsUrl: session2.wsUrl,
|
|
4367
|
+
debug: trace
|
|
4368
|
+
});
|
|
4369
|
+
return { browser: browser2, session: session2, isNewSession: false };
|
|
4370
|
+
}
|
|
4371
|
+
if (useLatestSession) {
|
|
4372
|
+
const session2 = await getDefaultSession();
|
|
4373
|
+
if (!session2) {
|
|
4374
|
+
throw new Error(
|
|
4375
|
+
'No sessions found. Run "bp connect" first or use "bp record" to auto-connect.'
|
|
4376
|
+
);
|
|
4377
|
+
}
|
|
4378
|
+
const browser2 = await connect({
|
|
4379
|
+
provider: session2.provider,
|
|
4380
|
+
wsUrl: session2.wsUrl,
|
|
4381
|
+
debug: trace
|
|
4382
|
+
});
|
|
4383
|
+
return { browser: browser2, session: session2, isNewSession: false };
|
|
4384
|
+
}
|
|
4385
|
+
let wsUrl;
|
|
4386
|
+
try {
|
|
4387
|
+
wsUrl = await getBrowserWebSocketUrl("localhost:9222");
|
|
4388
|
+
} catch {
|
|
4389
|
+
throw new Error(
|
|
4390
|
+
"Could not auto-discover browser.\nEither:\n 1. Start Chrome with: --remote-debugging-port=9222\n 2. Use an existing session: bp record -s <session-id>\n 3. Use latest session: bp record -s"
|
|
4391
|
+
);
|
|
4392
|
+
}
|
|
4393
|
+
const browser = await connect({
|
|
4394
|
+
provider: "generic",
|
|
4395
|
+
wsUrl,
|
|
4396
|
+
debug: trace
|
|
4397
|
+
});
|
|
4398
|
+
const page = await browser.page();
|
|
4399
|
+
const currentUrl = await page.url();
|
|
4400
|
+
const newSessionId = generateSessionId();
|
|
4401
|
+
const session = {
|
|
4402
|
+
id: newSessionId,
|
|
4403
|
+
provider: "generic",
|
|
4404
|
+
wsUrl: browser.wsUrl,
|
|
4405
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4406
|
+
lastActivity: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4407
|
+
currentUrl
|
|
4408
|
+
};
|
|
4409
|
+
await saveSession(session);
|
|
4410
|
+
return { browser, session, isNewSession: true };
|
|
4411
|
+
}
|
|
4412
|
+
async function recordCommand(args, globalOptions) {
|
|
4413
|
+
const options = parseRecordArgs(args);
|
|
4414
|
+
if (options.help || globalOptions.help) {
|
|
4415
|
+
console.log(RECORD_HELP);
|
|
4416
|
+
return;
|
|
4417
|
+
}
|
|
4418
|
+
const outputFile = options.file ?? "recording.json";
|
|
4419
|
+
const { browser, session, isNewSession } = await resolveConnection(
|
|
4420
|
+
globalOptions.session,
|
|
4421
|
+
options.useLatestSession ?? false,
|
|
4422
|
+
globalOptions.trace ?? false
|
|
4423
|
+
);
|
|
4424
|
+
if (isNewSession) {
|
|
4425
|
+
console.log(`Created new session: ${session.id}`);
|
|
4426
|
+
}
|
|
4427
|
+
const page = await browser.page();
|
|
4428
|
+
const cdp = page.cdpClient;
|
|
4429
|
+
const recorder = new Recorder(cdp);
|
|
4430
|
+
let stopping = false;
|
|
4431
|
+
async function stopAndSave() {
|
|
4432
|
+
if (stopping) return;
|
|
4433
|
+
stopping = true;
|
|
4434
|
+
try {
|
|
4435
|
+
const recording = await recorder.stop();
|
|
4436
|
+
const fs = await import("fs/promises");
|
|
4437
|
+
await fs.writeFile(outputFile, JSON.stringify(recording, null, 2));
|
|
4438
|
+
const currentUrl = await page.url();
|
|
4439
|
+
await updateSession(session.id, { currentUrl });
|
|
4440
|
+
await browser.disconnect();
|
|
4441
|
+
console.log(`
|
|
4442
|
+
Saved ${recording.steps.length} steps to ${outputFile}`);
|
|
4443
|
+
if (globalOptions.output === "json") {
|
|
4444
|
+
output(
|
|
4445
|
+
{
|
|
4446
|
+
success: true,
|
|
4447
|
+
file: outputFile,
|
|
4448
|
+
steps: recording.steps.length,
|
|
4449
|
+
duration: recording.duration
|
|
4450
|
+
},
|
|
4451
|
+
"json"
|
|
4452
|
+
);
|
|
4453
|
+
}
|
|
4454
|
+
process.exit(0);
|
|
4455
|
+
} catch (error) {
|
|
4456
|
+
console.error("Error saving recording:", error);
|
|
4457
|
+
process.exit(1);
|
|
4458
|
+
}
|
|
4459
|
+
}
|
|
4460
|
+
process.on("SIGINT", stopAndSave);
|
|
4461
|
+
process.on("SIGTERM", stopAndSave);
|
|
4462
|
+
if (options.timeout && options.timeout > 0) {
|
|
4463
|
+
setTimeout(stopAndSave, options.timeout);
|
|
4464
|
+
}
|
|
4465
|
+
await recorder.start();
|
|
4466
|
+
console.log(`Recording... Press Ctrl+C to stop and save to ${outputFile}`);
|
|
4467
|
+
console.log(`Session: ${session.id}`);
|
|
4468
|
+
console.log(`URL: ${await page.url()}`);
|
|
4469
|
+
}
|
|
4470
|
+
|
|
3469
4471
|
// src/cli/commands/screenshot.ts
|
|
3470
4472
|
function parseScreenshotArgs(args) {
|
|
3471
4473
|
const options = {};
|
|
@@ -3639,6 +4641,7 @@ Commands:
|
|
|
3639
4641
|
quickstart Getting started guide (start here!)
|
|
3640
4642
|
connect Create browser session
|
|
3641
4643
|
exec Execute actions
|
|
4644
|
+
record Record browser actions to JSON
|
|
3642
4645
|
snapshot Get page with element refs
|
|
3643
4646
|
text Extract text content
|
|
3644
4647
|
screenshot Take screenshot
|
|
@@ -3659,6 +4662,8 @@ Examples:
|
|
|
3659
4662
|
bp exec '{"action":"goto","url":"https://example.com"}'
|
|
3660
4663
|
bp snapshot --format text
|
|
3661
4664
|
bp exec '{"action":"click","selector":"ref:e3"}'
|
|
4665
|
+
bp record # Record from local browser
|
|
4666
|
+
bp record -s -f login.json # Record from latest session
|
|
3662
4667
|
|
|
3663
4668
|
Run 'bp quickstart' for CLI workflow guide.
|
|
3664
4669
|
Run 'bp actions' for complete action reference.
|
|
@@ -3761,6 +4766,9 @@ async function main() {
|
|
|
3761
4766
|
case "actions":
|
|
3762
4767
|
await actionsCommand();
|
|
3763
4768
|
break;
|
|
4769
|
+
case "record":
|
|
4770
|
+
await recordCommand(remaining, options);
|
|
4771
|
+
break;
|
|
3764
4772
|
case "help":
|
|
3765
4773
|
case "--help":
|
|
3766
4774
|
case "-h":
|