@szc-ft/mcp-szcd-client 0.27.1 → 0.27.2

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.
@@ -1,24 +1,41 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * local-browser-executor.js - 本地浏览器自动化执行器
3
+ * local-browser-executor.cjs - 本地浏览器自动化执行器
4
4
  *
5
5
  * 功能:
6
- * --action diagnose 全面诊断(JS错误、网络请求、API检查、资源加载)
7
- * --action list-pages 列出所有标签页
8
- * --action screenshot 截图
9
- * --action api-check 检查 API 请求状态
10
- * --action plan 执行测试计划 JSON
6
+ * --action diagnose 全面诊断(JS错误、网络请求、API检查、资源加载)
7
+ * --action list-pages 列出所有标签页
8
+ * --action screenshot 截图(支持 --full-page 滚动截图)
9
+ * --action api-check 检查 API 请求状态
10
+ * --action snapshot 结构化 DOM 快照(类似 browser-use take_snapshot)
11
+ * --action interact 智能交互(支持 text=/role= 选择器,类似 Playwright)
12
+ * --action perf 性能指标采集(FCP/LCP/CLS/TBT)
13
+ * --action watch 持续监控模式(定时截图+错误检测)
14
+ * --action plan 执行测试计划 JSON
11
15
  *
12
16
  * 连接方式:
13
- * --cdp-url http://localhost:9222 (默认自动检测 9222)
14
- * --auto-detect 自动检测 Chrome/Edge 进程
17
+ * --cdp-url http://localhost:9222 (默认 9222)
15
18
  *
16
19
  * 通用参数:
17
- * --url <url> 目标页面 URL
18
- * --url-contains <s> 按关键词查找已打开的页面
19
- * --wait <ms> 额外等待时间(默认 5000)
20
- * --output <path> 结果输出文件
21
- * --output-dir <dir> 截图/产物输出目录
20
+ * --url <url> 目标页面 URL
21
+ * --url-contains <s> 按关键词查找已打开的页面
22
+ * --wait <ms> 额外等待时间(默认 5000)
23
+ * --output <path> 结果输出文件
24
+ * --output-dir <dir> 截图/产物输出目录
25
+ *
26
+ * snapshot 参数:
27
+ * --selector <css> 快照根元素(默认 body)
28
+ * --depth <n> DOM 深度(默认 5)
29
+ *
30
+ * interact 参数:
31
+ * --click <selector> 点击元素(支持 text=、role=、CSS)
32
+ * --type <text> 输入文本(配合 --click 定位输入框)
33
+ * --hover <selector> 悬停元素
34
+ * --scroll <direction> 滚动(up/down/left/right,配合 --scroll-amount)
35
+ *
36
+ * watch 参数:
37
+ * --interval <ms> 监控间隔(默认 5000)
38
+ * --max-rounds <n> 最大轮数(默认 60)
22
39
  */
23
40
 
24
41
  const puppeteer = require('puppeteer-core');
@@ -65,7 +82,41 @@ function checkCDP(url) {
65
82
  }
66
83
 
67
84
  async function findPage(browser) {
68
- const pages = await browser.pages();
85
+ let pages;
86
+ try {
87
+ pages = await Promise.race([
88
+ browser.pages(),
89
+ new Promise((_, reject) => setTimeout(() => reject(new Error('pages() timeout')), 8000)),
90
+ ]);
91
+ } catch {
92
+ // browser.pages() 超时,尝试用 targets() 找到目标再 .page()
93
+ try {
94
+ const targets = browser.targets();
95
+ let matchTarget = null;
96
+ if (URL_CONTAINS) {
97
+ matchTarget = targets.find(t => t.type() === 'page' && t.url().includes(URL_CONTAINS));
98
+ }
99
+ if (TARGET_URL && !matchTarget) {
100
+ matchTarget = targets.find(t => t.type() === 'page' && t.url().includes(TARGET_URL));
101
+ }
102
+ if (!matchTarget) {
103
+ matchTarget = targets.find(t => t.type() === 'page' && !t.url().startsWith('about:') && !t.url().startsWith('devtools:'));
104
+ }
105
+ if (matchTarget) {
106
+ const page = await matchTarget.page();
107
+ if (page) return page;
108
+ }
109
+ } catch (e) {
110
+ log(`[WARN] targets() 回退失败: ${e.message}`);
111
+ }
112
+ // 最终回退
113
+ if (TARGET_URL) {
114
+ const page = await browser.newPage();
115
+ await page.goto(TARGET_URL, { waitUntil: 'domcontentloaded', timeout: 30000 });
116
+ return page;
117
+ }
118
+ return await browser.newPage();
119
+ }
69
120
  if (TARGET_URL) {
70
121
  for (const p of pages) {
71
122
  if (p.url().includes(TARGET_URL) || p.url() === TARGET_URL) return p;
@@ -90,7 +141,27 @@ async function findPage(browser) {
90
141
 
91
142
  // ===== Action: list-pages =====
92
143
  async function listPages(browser) {
93
- const pages = await browser.pages();
144
+ log('[INFO] 获取标签页列表...');
145
+ // browser.pages() 在大量标签页时可能卡住,用 CDP 直接获取
146
+ let pages;
147
+ try {
148
+ pages = await Promise.race([
149
+ browser.pages(),
150
+ new Promise((_, reject) => setTimeout(() => reject(new Error('pages() timeout')), 5000)),
151
+ ]);
152
+ } catch {
153
+ // 回退:通过 CDP Target API 获取
154
+ const client = await browser.target().createCDPSession();
155
+ const { targetInfos } = await client.send('Target.getTargets');
156
+ log(`[INFO] CDP 回退: 共 ${targetInfos.filter(t => t.type === 'page').length} 个标签页`);
157
+ for (const t of targetInfos.filter(t => t.type === 'page')) {
158
+ log(` ${t.url}`);
159
+ if (t.title) log(` title: ${t.title}`);
160
+ }
161
+ try { await client.detach(); } catch {}
162
+ return { total: targetInfos.filter(t => t.type === 'page').length };
163
+ }
164
+ log(`[INFO] 共 ${pages.length} 个标签页`);
94
165
  const result = [];
95
166
  for (let i = 0; i < pages.length; i++) {
96
167
  const p = pages[i];
@@ -365,6 +436,246 @@ async function executePlan(browser) {
365
436
  return { steps: results, summary };
366
437
  }
367
438
 
439
+ // ===== 智能选择器解析(融合 Playwright 风格) =====
440
+ async function smartResolve(pageOrFrame, selector) {
441
+ if (!selector) return null;
442
+ if (selector.startsWith('text=')) {
443
+ const text = selector.slice(5).replace(/^["']|["']$/g, '');
444
+ return pageOrFrame.evaluateHandle(t => {
445
+ const all = document.querySelectorAll('a, button, [role="button"], input, [class*="btn"], span, div, p');
446
+ return Array.from(all).find(el => el.textContent?.includes(t) && el.offsetHeight > 0) || null;
447
+ }, text);
448
+ }
449
+ if (selector.startsWith('role=')) {
450
+ return pageOrFrame.$(`[role="${selector.slice(5)}"]`);
451
+ }
452
+ if (selector.startsWith('~')) {
453
+ return pageOrFrame.$(`[class*="${selector.slice(1).replace(/^\./, '')}"]`);
454
+ }
455
+ return pageOrFrame.$(selector);
456
+ }
457
+
458
+ // ===== Action: snapshot (结构化 DOM 快照) =====
459
+ async function snapshot(browser) {
460
+ const page = await findPage(browser);
461
+ const selector = getArg('selector', 'body');
462
+ const maxDepth = parseInt(getArg('depth', '5'), 10);
463
+ log(`[PAGE] ${page.url()}`);
464
+
465
+ const tree = await page.evaluate((rootSel, depth) => {
466
+ function buildTree(el, d) {
467
+ if (d > depth || !el) return null;
468
+ const tag = el.tagName?.toLowerCase();
469
+ if (!tag || ['script','style','svg','path'].includes(tag)) return null;
470
+ const attrs = {};
471
+ for (const a of (el.attributes || [])) {
472
+ if (['id','class','href','src','role','aria-label','data-testid'].includes(a.name)) {
473
+ attrs[a.name] = a.value.substring(0, 100);
474
+ }
475
+ }
476
+ // 直接文本内容
477
+ let text = '';
478
+ for (const node of el.childNodes) {
479
+ if (node.nodeType === 3) text += node.textContent.trim();
480
+ }
481
+ // 子元素
482
+ const children = [];
483
+ for (const child of el.children) {
484
+ const childTree = buildTree(child, d + 1);
485
+ if (childTree) children.push(childTree);
486
+ }
487
+ // 可交互性
488
+ const interactive = ['a','button','input','select','textarea'].includes(tag) ||
489
+ el.getAttribute('role') === 'button' || el.onclick || el.style.cursor === 'pointer';
490
+ // 可见性
491
+ const rect = el.getBoundingClientRect();
492
+ const visible = rect.width > 0 && rect.height > 0;
493
+ return { tag, attrs, text: text.substring(0, 80), children, interactive, visible, depth: d };
494
+ }
495
+ const root = document.querySelector(rootSel) || document.body;
496
+ return buildTree(root, 0);
497
+ }, selector, maxDepth);
498
+
499
+ // 打印树形结构
500
+ function printTree(node, indent = '') {
501
+ if (!node) return;
502
+ const cls = node.attrs.class ? `.${node.attrs.class.split(' ')[0].substring(0, 30)}` : '';
503
+ const id = node.attrs.id ? `#${node.attrs.id}` : '';
504
+ const role = node.attrs.role ? `[role=${node.attrs.role}]` : '';
505
+ const interactive = node.interactive ? ' \u{1F517}' : '';
506
+ const vis = node.visible ? '' : ' [hidden]';
507
+ const txt = node.text ? ` "${node.text.substring(0, 40)}"` : '';
508
+ log(`${indent}<${node.tag}${id}${cls}${role}>${txt}${interactive}${vis}`);
509
+ for (const child of (node.children || [])) {
510
+ printTree(child, indent + ' ');
511
+ }
512
+ }
513
+ log('\n========== DOM 快照 ==========');
514
+ printTree(tree);
515
+
516
+ if (OUTPUT) {
517
+ fs.writeFileSync(OUTPUT, JSON.stringify(tree, null, 2));
518
+ log(`\n[OK] 结果已保存: ${OUTPUT}`);
519
+ }
520
+ return tree;
521
+ }
522
+
523
+ // ===== Action: interact (智能交互) =====
524
+ async function interact(browser) {
525
+ const page = await findPage(browser);
526
+ log(`[PAGE] ${page.url()}`);
527
+
528
+ const clickSel = getArg('click', null);
529
+ const typeText = getArg('type', null);
530
+ const hoverSel = getArg('hover', null);
531
+ const scrollDir = getArg('scroll', null);
532
+ const scrollAmt = parseInt(getArg('scroll-amount', '300'), 10);
533
+
534
+ if (clickSel) {
535
+ const el = await smartResolve(page, clickSel);
536
+ if (el) {
537
+ await el.click();
538
+ log(`[OK] 点击: ${clickSel}`);
539
+ await new Promise(r => setTimeout(r, 1000));
540
+ } else {
541
+ log(`[FAIL] 未找到元素: ${clickSel}`);
542
+ }
543
+ }
544
+ if (typeText) {
545
+ const target = clickSel ? await smartResolve(page, clickSel) : await page.$('input:focus, textarea:focus');
546
+ if (target) {
547
+ await target.type(typeText, { delay: 50 });
548
+ log(`[OK] 输入: "${typeText}"`);
549
+ } else {
550
+ log(`[FAIL] 未找到输入目标`);
551
+ }
552
+ }
553
+ if (hoverSel) {
554
+ const el = await smartResolve(page, hoverSel);
555
+ if (el) {
556
+ await el.hover();
557
+ log(`[OK] 悬停: ${hoverSel}`);
558
+ }
559
+ }
560
+ if (scrollDir) {
561
+ const dx = scrollDir === 'left' ? -scrollAmt : scrollDir === 'right' ? scrollAmt : 0;
562
+ const dy = scrollDir === 'up' ? -scrollAmt : scrollDir === 'down' ? scrollAmt : 0;
563
+ await page.evaluate(({ dx, dy }) => window.scrollBy(dx, dy), { dx, dy });
564
+ log(`[OK] 滚动: ${scrollDir} ${scrollAmt}px`);
565
+ }
566
+
567
+ // 交互后截图
568
+ if (hasFlag('screenshot-after')) {
569
+ ensureDir(OUTPUT_DIR);
570
+ const fp = path.join(OUTPUT_DIR, 'after-interact.png');
571
+ await page.screenshot({ path: fp });
572
+ log(`[OK] 截图: ${fp}`);
573
+ }
574
+ }
575
+
576
+ // ===== Action: perf (性能指标) =====
577
+ async function perf(browser) {
578
+ const page = await findPage(browser);
579
+ log(`[PAGE] ${page.url()}`);
580
+
581
+ // 收集性能指标
582
+ const perfMetrics = await page.evaluate(() => {
583
+ const perf = performance.getEntriesByType('navigation')[0];
584
+ const paint = performance.getEntriesByType('paint');
585
+ const fcp = paint.find(p => p.name === 'first-contentful-paint');
586
+ // LCP 通过 PerformanceObserver 获取
587
+ const resources = performance.getEntriesByType('resource');
588
+ return {
589
+ // 导航时间
590
+ dns: perf?.domainEnd - perf?.domainStart || 0,
591
+ tcp: perf?.connectEnd - perf?.connectStart || 0,
592
+ ttfb: perf?.responseStart - perf?.requestStart || 0,
593
+ domReady: perf?.domContentLoadedEventEnd - perf?.startTime || 0,
594
+ loadComplete: perf?.loadEventEnd - perf?.startTime || 0,
595
+ // 绘制
596
+ fcp: fcp?.startTime || null,
597
+ // 资源统计
598
+ totalResources: resources.length,
599
+ totalTransferSize: resources.reduce((sum, r) => sum + (r.transferSize || 0), 0),
600
+ slowResources: resources.filter(r => r.duration > 1000).map(r => ({
601
+ name: r.name.split('/').pop(), duration: Math.round(r.duration), size: r.transferSize
602
+ })),
603
+ };
604
+ });
605
+
606
+ // Web Vitals (通过 CDP)
607
+ let webVitals = {};
608
+ try {
609
+ const client = await page.target().createCDPSession();
610
+ await client.send('Performance.enable');
611
+ const metrics = await client.send('Performance.getMetrics');
612
+ const m = {};
613
+ metrics.metrics.forEach(item => { m[item.name] = item.value; });
614
+ webVitals = {
615
+ layoutCount: m.LayoutCount || 0,
616
+ recalcCount: m.RecalcLayoutCount || 0,
617
+ jsHeapUsed: Math.round((m.JSHeapUsedSize || 0) / 1024 / 1024) + 'MB',
618
+ jsHeapTotal: Math.round((m.JSHeapTotalSize || 0) / 1024 / 1024) + 'MB',
619
+ taskDuration: (m.TaskDuration || 0).toFixed(2) + 's',
620
+ };
621
+ await client.send('Performance.disable');
622
+ client.detach();
623
+ } catch {}
624
+
625
+ log('\n========== 性能指标 ==========');
626
+ log(`DNS: ${Math.round(perfMetrics.dns)}ms`);
627
+ log(`TCP: ${Math.round(perfMetrics.tcp)}ms`);
628
+ log(`TTFB: ${Math.round(perfMetrics.ttfb)}ms`);
629
+ log(`DOM Ready: ${Math.round(perfMetrics.domReady)}ms`);
630
+ log(`Load Complete: ${Math.round(perfMetrics.loadComplete)}ms`);
631
+ log(`FCP: ${perfMetrics.fcp ? Math.round(perfMetrics.fcp) + 'ms' : 'N/A'}`);
632
+ log(`\n资源总数: ${perfMetrics.totalResources}`);
633
+ log(`传输总量: ${Math.round(perfMetrics.totalTransferSize / 1024)}KB`);
634
+ if (perfMetrics.slowResources.length > 0) {
635
+ log(`\n慢资源 (>1s):`);
636
+ perfMetrics.slowResources.forEach(r => log(` ${r.duration}ms ${r.name} (${Math.round((r.size||0)/1024)}KB)`));
637
+ }
638
+ if (Object.keys(webVitals).length > 0) {
639
+ log('\n========== Chrome 内部指标 ==========');
640
+ Object.entries(webVitals).forEach(([k, v]) => log(` ${k}: ${v}`));
641
+ }
642
+
643
+ return { perfMetrics, webVitals };
644
+ }
645
+
646
+ // ===== Action: watch (持续监控) =====
647
+ async function watch(browser) {
648
+ const page = await findPage(browser);
649
+ const interval = parseInt(getArg('interval', '5000'), 10);
650
+ const maxRounds = parseInt(getArg('max-rounds', '60'), 10);
651
+ ensureDir(OUTPUT_DIR);
652
+ log(`[PAGE] ${page.url()}`);
653
+ log(`[WATCH] 间隔=${interval}ms, 最大轮数=${maxRounds}`);
654
+
655
+ const errors = [];
656
+ page.on('pageerror', err => errors.push({ time: new Date().toISOString(), error: err.message }));
657
+ page.on('console', msg => {
658
+ if (msg.type() === 'error') errors.push({ time: new Date().toISOString(), type: 'console', text: msg.text() });
659
+ });
660
+
661
+ for (let i = 1; i <= maxRounds; i++) {
662
+ const ts = new Date().toISOString().replace(/[:.]/g, '-');
663
+ const fp = path.join(OUTPUT_DIR, `watch-${String(i).padStart(3, '0')}-${ts}.png`);
664
+ await page.screenshot({ path: fp });
665
+ const errCount = errors.length;
666
+ log(`[${i}/${maxRounds}] 截图 | 累计错误: ${errCount}`);
667
+ if (errCount > 0 && i === 1) {
668
+ errors.forEach(e => log(` [ERROR] ${e.error || e.text}`));
669
+ }
670
+ await new Promise(r => setTimeout(r, interval));
671
+ }
672
+
673
+ log(`\n========== 监控结束 ==========`);
674
+ log(`总轮数: ${maxRounds}, 累计错误: ${errors.length}`);
675
+ errors.forEach(e => log(` [${e.time}] ${e.error || e.text}`));
676
+ return { rounds: maxRounds, errors };
677
+ }
678
+
368
679
  // ===== 主入口 =====
369
680
  (async () => {
370
681
  // 检测 CDP
@@ -378,7 +689,23 @@ async function executePlan(browser) {
378
689
  }
379
690
  log(`[CDP] ${cdpInfo.Browser || 'connected'} (${CDP_URL})`);
380
691
 
381
- const browser = await puppeteer.connect({ browserURL: CDP_URL, defaultViewport: null });
692
+ // 获取 WebSocket URL
693
+ let wsUrl;
694
+ try {
695
+ const versionData = await new Promise((resolve, reject) => {
696
+ const req = http.get(CDP_URL + '/json/version', { timeout: 3000 }, res => {
697
+ let d = ''; res.on('data', c => d += c); res.on('end', () => resolve(JSON.parse(d)));
698
+ });
699
+ req.on('error', reject);
700
+ });
701
+ wsUrl = versionData.webSocketDebuggerUrl;
702
+ } catch {}
703
+
704
+ const browser = await Promise.race([
705
+ puppeteer.connect({ browserWSEndpoint: wsUrl || CDP_URL, defaultViewport: null }),
706
+ new Promise((_, reject) => setTimeout(() => reject(new Error('CDP WebSocket 连接超时 (10s)')), 10000)),
707
+ ]);
708
+ log('[OK] 浏览器已连接');
382
709
 
383
710
  let result;
384
711
  switch (ACTION) {
@@ -386,10 +713,15 @@ async function executePlan(browser) {
386
713
  case 'diagnose': result = await diagnose(browser); break;
387
714
  case 'screenshot': result = await screenshot(browser); break;
388
715
  case 'api-check': result = await apiCheck(browser); break;
716
+ case 'snapshot': result = await snapshot(browser); break;
717
+ case 'interact': result = await interact(browser); break;
718
+ case 'perf': result = await perf(browser); break;
719
+ case 'watch': result = await watch(browser); break;
389
720
  case 'plan': result = await executePlan(browser); break;
390
721
  default: log(`[ERROR] 未知 action: ${ACTION}`);
391
722
  }
392
723
 
393
724
  browser.disconnect();
394
725
  log('\n[DONE]');
726
+ process.exit(0);
395
727
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@szc-ft/mcp-szcd-client",
3
- "version": "0.27.1",
3
+ "version": "0.27.2",
4
4
  "description": "MCP client for szcd component library - auto-configures AI coding tools with MCP server, skills, agents and commands",
5
5
  "keywords": [
6
6
  "mcp",
@@ -53,5 +53,5 @@ argument-hint: <目标页面URL> [可选:设计稿图片路径]
53
53
 
54
54
  **注意事项**:
55
55
  - 首次使用需确保 Chrome 以调试模式启动:`google-chrome --remote-debugging-port=9222`
56
- - 如执行器报错依赖缺失,引导用户安装:`npm install puppeteer-core pixelmatch pngjs sharp`
56
+ - 如执行器报错依赖缺失,优先按提示安装核心依赖;仅设计稿像素对比需要额外安装 `pixelmatch pngjs sharp`
57
57
  - connect 模式下天然继承用户浏览器的登录态和微前端环境
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "szcd-component-helper",
3
- "version": "0.27.1",
3
+ "version": "0.27.2",
4
4
  "description": "szcd 组件库 MCP 助手 — 查询组件信息、匹配需求、生成代码",
5
5
  "mcpServers": {
6
6
  "szcd-component-helper": {
@@ -17,6 +17,24 @@ compatibility:
17
17
  - "功能测试"、"能不能正常用"
18
18
  - "检查页面"、"页面渲染"
19
19
 
20
+ ## 主链路定位
21
+
22
+ local-browser-test 的长期主链路是:
23
+
24
+ ```
25
+ Agent / SKILL.md
26
+ → szcd-mcp-client/local-browser-executor.js
27
+ → lib/browser-engine.js
28
+ → puppeteer-core + CDP
29
+ → 用户真实 Chrome / Edge
30
+ ```
31
+
32
+ 目标是在用户真实登录态/真实微前端环境中形成 browser-use + Playwright 结合体:
33
+ - browser-use 侧:`observe → act → observe`,通过结构化页面观察和交互元素 index 支持自主任务执行。
34
+ - Playwright 侧:locator、actionability、assert、plan 报告逐步收敛到 `BrowserEngine`。
35
+
36
+ `standard-skill/local-browser-test/local-browser-executor.cjs` 是过渡兼容执行器;新增稳定能力优先进入 `szcd-mcp-client/lib/browser-engine.js` 和根目录 `local-browser-executor.js`。
37
+
20
38
  ## 浏览器模式
21
39
 
22
40
  支持两种模式,执行器自动检测优先使用 connect:
@@ -60,14 +78,17 @@ AI 直接构造测试计划 JSON:
60
78
 
61
79
  | type | 功能 | 参数 | 返回 |
62
80
  |------|------|------|------|
81
+ | observe | 结构化观察 | `selector`, `depth`, `frameContentContains`, `maxInteractiveElements` | `{ frames, tree, interactiveElements[] }` |
82
+ | act | 按 index/selector 交互 | `index` 或 `selector/click/hover`, `typeText`, `clear`, `frameContentContains` | `{ acted, action, element }` |
83
+ | assert | 轻量断言 | `selector`, `expect`, `urlContains`, `textContains` | `{ passed, checks }` |
63
84
  | navigate | 导航到 URL | `url`, `waitUntil` | `{ url, title, loadTime }` |
64
85
  | screenshot | 页面截图 | `fullPage`, `clip`, `filename` | `{ filepath, width, height }` |
65
86
  | compare | 设计稿对比 | `designImagePath`, `threshold`, `regions[]` | `{ fidelity, diffPixels, diffImagePath, regions[] }` |
66
- | click | 点击元素 | `selector`, `waitForNavigation` | `{ clicked, tagName }` |
67
- | type | 输入文本 | `selector`, `text`, `clear` | `{ typed, value }` |
87
+ | click | 点击元素(兼容) | `selector`, `waitForNavigation` | `{ clicked, tagName }` |
88
+ | type | 输入文本(兼容) | `selector`, `text`, `clear` | `{ typed, value }` |
68
89
  | waitFor | 等待条件 | `selector` 或 `url`, `timeout` | `{ matched, waited }` |
69
90
  | evaluate | 执行 JS | `expression` | `{ result }` |
70
- | checkElement | 元素断言 | `selector`, `expect: {visible, minCount, maxCount, hasText}` | `{ passed, checks, elementCount }` |
91
+ | checkElement | 元素断言(兼容) | `selector`, `expect: {visible, minCount, maxCount, hasText}` | `{ passed, checks, elementCount }` |
71
92
  | findPage | 查找已打开的标签页 | `urlIncludes` 或 `urlRegex` 或 `titleIncludes` | `{ found, url, title, totalPages }` |
72
93
  | findFrame | 查找微前端 iframe | `urlIncludes` 或 `urlRegex` 或 `titleIncludes` 或 `contentIncludes` 或 `frameIndex` | `{ found, frameUrl, matchedBy, totalFrames }` |
73
94
  | loginWait | 等待 SSO 登录完成 | `loginSelector`, `timeout`, `interval` | `{ loggedIn, elapsed, currentUrl }` |
@@ -141,15 +162,15 @@ AI 直接构造测试计划 JSON:
141
162
  wujie 微前端子应用渲染在 blob URL iframe 中,**坐标点击无法到达 iframe 内容**。
142
163
 
143
164
  ```json
144
- { "type": "findFrame", "contentIncludes": "知识采编" }
145
- { "type": "click", "selector": "a.edit-btn" }
146
- { "type": "checkElement", "selector": "~.editKnowledge", "expect": { "minCount": 1 } }
165
+ { "type": "observe", "frameContentContains": "知识采编", "depth": 5, "maxInteractiveElements": 80 }
166
+ { "type": "act", "index": 3, "frameContentContains": "知识采编" }
167
+ { "type": "assert", "selector": "~.editKnowledge", "expect": { "minCount": 1 } }
147
168
  ```
148
169
 
149
- - `findFrame` 支持 `urlIncludes`、`titleIncludes`、`contentIncludes` 多种发现方式
150
- - 优先用 `contentIncludes`(按页面文字内容查找),不依赖 URL/端口
151
- - `findFrame` 后,后续所有步骤自动在 iframe 内执行
152
- - iframe 内的 click 使用 `evaluate()` 触发 DOM click,不依赖坐标
170
+ - 优先用 `observe` 发现 iframe 内的 `interactiveElements[index]`,再用 `act.index` 操作,不要重新猜 selector
171
+ - `observe/act` 支持 `frameUrlContains`、`frameContentContains`、`frameIndex` 多种发现方式
172
+ - 优先用 `frameContentContains`(按页面文字内容查找),不依赖 URL/端口
173
+ - iframe 内的 act 使用 DOM 事件触发,不依赖坐标
153
174
 
154
175
  ### CSS Modules 哈希类名
155
176
  项目使用 CSS Modules,类名会被哈希化(如 `editKnowledge___ynQgI`)。
@@ -192,7 +213,41 @@ wujie 微前端子应用渲染在 blob URL iframe 中,**坐标点击无法到
192
213
 
193
214
  ## 执行命令
194
215
 
195
- ### 快速诊断(最常用)
216
+ ### 结构化观察(主链路起点)
217
+ ```bash
218
+ node {client_path}/local-browser-executor.js \
219
+ --action observe \
220
+ --url-contains "platform.aicityos.com" \
221
+ --frame-content-contains "数据集目录" \
222
+ --depth 5 \
223
+ --output /tmp/browser-observe.json
224
+ ```
225
+
226
+ `observe` 输出 `frames`、`tree`、`interactiveElements[index]`、`selectorCandidates`、`bbox`、`state`。Agent 后续应优先使用 `index` 调用 `act`,不要从 DOM 树重新猜 selector。直接 `--action observe` 默认只连接用户已启动的真实浏览器 CDP;如确需无 CDP 时启动 headless 回退,追加 `--allow-launch`。
227
+
228
+ ### 观察后按 index 交互(主链路闭环)
229
+ ```bash
230
+ node {client_path}/local-browser-executor.js \
231
+ --action act \
232
+ --url-contains "platform.aicityos.com" \
233
+ --frame-content-contains "数据集目录" \
234
+ --index 3 \
235
+ --observe-after \
236
+ --output /tmp/browser-act-observe.json
237
+ ```
238
+
239
+ ### 轻量 DOM 断言
240
+ ```bash
241
+ node {client_path}/local-browser-executor.js \
242
+ --action assert \
243
+ --url-contains "platform.aicityos.com" \
244
+ --selector ".ant-table" \
245
+ --expect-visible \
246
+ --min-count 1 \
247
+ --output /tmp/browser-assert.json
248
+ ```
249
+
250
+ ### 快速诊断(过渡兼容)
196
251
  ```bash
197
252
  NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
198
253
  --action diagnose \
@@ -232,16 +287,98 @@ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor
232
287
  --output /tmp/browser-test-result.json
233
288
  ```
234
289
 
290
+ ### DOM 快照(结构化页面树,类似 browser-use take_snapshot)
291
+ ```bash
292
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
293
+ --action snapshot \
294
+ --url-contains "platform.aicityos.com" \
295
+ --depth 4 \
296
+ --selector ".page-container"
297
+ ```
298
+ 输出示例:
299
+ ```
300
+ <div.page-container>
301
+ <section.page-layout>
302
+ <header.page-header>
303
+ <div.page-header-left> <div.logo>
304
+ <section.h-box>
305
+ <aside.ant-layout-sider> 🔗
306
+ <main.ant-layout-content>
307
+ <div.layout-wrap>
308
+ ```
309
+
310
+ ### 智能交互(支持 text=、role=、~ 选择器,类似 Playwright)
311
+ ```bash
312
+ # 点击包含“构建”文本的按钮
313
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
314
+ --action interact \
315
+ --url-contains "jenkins" \
316
+ --click "text=Build"
317
+
318
+ # 在输入框中输入文本
319
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
320
+ --action interact \
321
+ --click "#search-input" \
322
+ --type "hello world"
323
+
324
+ # 悬停 + 截图
325
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
326
+ --action interact \
327
+ --hover "~ant-menu-item" \
328
+ --screenshot-after
329
+
330
+ # 滚动页面
331
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
332
+ --action interact \
333
+ --scroll down --scroll-amount 500
334
+ ```
335
+ 选择器语法:
336
+ - `text=Build` → 按文本内容查找(类似 Playwright text selector)
337
+ - `role=button` → 按 ARIA role 查找
338
+ - `~ant-menu-item` → CSS class 模糊匹配(`[class*="ant-menu-item"]`)
339
+ - `.my-class` → 精确 CSS 选择器
340
+
341
+ ### 性能指标采集(FCP/TTFB/资源统计)
342
+ ```bash
343
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
344
+ --action perf \
345
+ --url-contains "platform.aicityos.com"
346
+ ```
347
+
348
+ ### 持续监控模式(定时截图 + 错误检测)
349
+ ```bash
350
+ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor.cjs \
351
+ --action watch \
352
+ --url-contains "platform.aicityos.com" \
353
+ --interval 5000 \
354
+ --max-rounds 60 \
355
+ --output-dir /tmp/browser-watch
356
+ ```
357
+
235
358
  ### 可选参数
236
359
 
237
360
  - `--cdp-url http://localhost:9222`:CDP 地址(默认 9222)
238
361
  - `--url <url>`:目标页面 URL(不存在则导航)
239
362
  - `--url-contains <keyword>`:按关键词查找已打开的页面
363
+ - `--frame-url-contains <keyword>`:observe 时按 iframe URL 片段匹配目标 frame
364
+ - `--frame-content-contains <keyword>`:observe 时按 iframe 文本内容匹配目标 frame
365
+ - `--frame-index <n>`:observe 时按 frame 下标选择目标 frame
240
366
  - `--wait <ms>`:额外等待时间(默认 5000ms,微前端建议 15000)
241
367
  - `--output <path>`:JSON 结果输出路径
242
368
  - `--output-dir <dir>`:截图/产物目录
243
369
  - `--filename <name>`:截图文件名
244
370
  - `--full-page`:全页截图
371
+ - `--selector <css>`:快照/observe 根元素(默认 body)
372
+ - `--depth <n>`:DOM 深度(默认 5)
373
+ - `--click <selector>`:点击元素(支持 text=/role=/~)
374
+ - `--type <text>`:输入文本
375
+ - `--hover <selector>`:悬停元素
376
+ - `--scroll <up|down|left|right>`:滚动方向
377
+ - `--scroll-amount <px>`:滚动距离(默认 300)
378
+ - `--screenshot-after`:交互后自动截图
379
+ - `--interval <ms>`:watch 模式间隔(默认 5000)
380
+ - `--max-rounds <n>`:watch 最大轮数(默认 60)
381
+ - `--allow-launch`:根执行器 action 模式允许无 CDP 时启动 headless Chrome;默认不启用,优先保障真实用户浏览器链路
245
382
 
246
383
  ## AI 语义断言(aiAssert)
247
384
 
@@ -268,12 +405,17 @@ NODE_PATH=~/.szcd-mcp/deps/node_modules node {skill_path}/local-browser-executor
268
405
 
269
406
  ## 依赖管理
270
407
 
271
- 浏览器测试依赖(puppeteer-core, pixelmatch, pngjs, sharp)采用**共享缓存**方案:
272
- - 首次使用时自动安装到 `~/.szcd-mcp/deps/`
408
+ 浏览器测试依赖采用**共享缓存 + 按需安装**方案:
409
+ - `observe/act/assert/diagnose/screenshot` 主链路只安装 `puppeteer-core` 到 `~/.szcd-mcp/deps/`
410
+ - 只有执行设计稿像素对比 `compare` 时才安装 `pixelmatch/pngjs/sharp`
273
411
  - 后续所有项目共享复用,无需重复安装
274
- - 如果自动安装失败,引导用户手动执行:
412
+ - 如果核心依赖自动安装失败,引导用户手动执行:
413
+ ```bash
414
+ cd ~/.szcd-mcp/deps && npm install --registry=https://registry.npmmirror.com puppeteer-core
415
+ ```
416
+ - 如果视觉对比依赖自动安装失败,引导用户手动执行:
275
417
  ```bash
276
- cd ~/.szcd-mcp/deps && npm install puppeteer-core pixelmatch pngjs sharp --registry=https://registry.npmmirror.com
418
+ cd ~/.szcd-mcp/deps && npm install --registry=https://registry.npmmirror.com --@img:registry=https://registry.npmmirror.com --prefer-offline --no-audit --no-fund --omit=dev --package-lock=false --sharp_binary_host=https://npmmirror.com/mirrors/sharp --sharp_libvips_binary_host=https://npmmirror.com/mirrors/sharp-libvips pixelmatch pngjs sharp
277
419
  ```
278
420
 
279
421
  ## 结果解读