lantern-harness 0.1.9 → 0.1.11

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.
Files changed (181) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +6 -6
  3. package/builtin/session/lh-fs.mjs +32 -14
  4. package/dist/500.html +258 -22
  5. package/dist/_astro/Base.Bgxs1QQl.css +1 -0
  6. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.CvbRi7Tl.js +1 -0
  7. package/dist/_astro/CaseExecBoard.Dttqp0wg.js +1 -0
  8. package/dist/_astro/CaseGenerateApp.DqW_nWMc.js +9 -0
  9. package/dist/_astro/CasesLibrary.8aMUUGp_.js +1 -0
  10. package/dist/_astro/ChatApp.BLxf0h1d.js +219 -0
  11. package/dist/_astro/ColumnResize.BEkEW7qw.js +1 -0
  12. package/dist/_astro/{ConfirmDialog.CBVsDJ8f.js → ConfirmDialog.BZzZWDci.js} +1 -1
  13. package/dist/_astro/{DashboardBoard.DCtfrrJz.js → DashboardBoard.CkSbhEDI.js} +3 -3
  14. package/dist/_astro/DatePicker.Bkl71M49.js +1 -0
  15. package/dist/_astro/DocsFileBrowser.MKVCfZrE.js +1 -0
  16. package/dist/_astro/DocsPager.DJY4foYL.js +1 -0
  17. package/dist/_astro/{DrawerField.CWMLhcPX.js → DrawerField.CHRg77LG.js} +1 -1
  18. package/dist/_astro/{ExcelFieldTags.B_XMwNNx.js → ExcelFieldTags.DXMeaDa5.js} +4 -4
  19. package/dist/_astro/KnowledgeLibrary.B-p1aoQN.js +1 -0
  20. package/dist/_astro/NamePromptDialog.Wa9O530a.js +1 -0
  21. package/dist/_astro/NavLibraries.C6LImMO_.js +1 -0
  22. package/dist/_astro/OverflowHint.D6v4Uj2N.js +1 -0
  23. package/dist/_astro/PluginsPage.-97n3x2U.js +39 -0
  24. package/dist/_astro/PluginsPage.DkmRPfbU.js +1 -0
  25. package/dist/_astro/PmBugsBoard.CMT4-eoB.js +1 -0
  26. package/dist/_astro/PmIterationsBoard.BQM_dzdZ.js +1 -0
  27. package/dist/_astro/PmProjectDetail.CzU80pat.js +1 -0
  28. package/dist/_astro/PmProjectsBoard.Cxsa7SYQ.js +1 -0
  29. package/dist/_astro/PmRequirementsBoard.4_JMDDN0.js +1 -0
  30. package/dist/_astro/PmRequirementsBoard.B8HL07cw.js +1 -0
  31. package/dist/_astro/PmShared.Ca2Ao7v_.js +1 -0
  32. package/dist/_astro/PromptsLibrary.C9BcIxKK.js +1 -0
  33. package/dist/_astro/ReportsPage.Fh5DRKLT.js +1 -0
  34. package/dist/_astro/RequirementsLibrary.CBkuWE8X.js +1 -0
  35. package/dist/_astro/SearchableSelect.DnjoSslK.js +1 -0
  36. package/dist/_astro/SelectDropdown.CuuDHa8s.js +1 -0
  37. package/dist/_astro/SettingsDialog.CVB5Gtk4.js +1 -0
  38. package/dist/_astro/SlideOverDrawer.B7B5Wu8I.js +1 -0
  39. package/dist/_astro/{SpaceSwitcher.CI3KDBOA.js → SpaceSwitcher.BVQLt_tL.js} +2 -2
  40. package/dist/_astro/TagSelect.CuN5RdF4.js +1 -0
  41. package/dist/_astro/TasksBoard.Bu-fpIZ7.js +1 -0
  42. package/dist/_astro/TemplatesLibrary.DSylQGlN.js +1 -0
  43. package/dist/_astro/TestStatsBoard.CPyKW4wc.js +1 -0
  44. package/dist/_astro/Toast.R8_2OWRX.js +1 -0
  45. package/dist/_astro/TodosBoard.8kvRTEW-.js +1 -0
  46. package/dist/_astro/UiAutoBoard.DFTiDqCp.js +9 -0
  47. package/dist/_astro/active-element-history.CED-m1Zp.js +1 -0
  48. package/dist/_astro/app-config.BUPzGl9f.js +1 -0
  49. package/dist/_astro/{client.Be_SakrB.js → client.BHNTqLjv.js} +3 -3
  50. package/dist/_astro/dialog.BAPTk9a_.js +1 -0
  51. package/dist/_astro/hydrate-cache.BXX604Bw.js +1 -0
  52. package/dist/_astro/{i18n.B52gLzii.js → i18n.BGPnP1Bg.js} +1 -1
  53. package/dist/_astro/index.astro_astro_type_script_index_0_lang.LPL9mPQY.js +1 -0
  54. package/dist/_astro/jsx-runtime.DUg_B_vD.js +1 -0
  55. package/dist/_astro/{lib.DJNDi_Ht.js → lib.CgXLzMMd.js} +15 -15
  56. package/dist/_astro/listbox.C96OCanh.js +1 -0
  57. package/dist/_astro/markdown-client.DRva7DS9.js +58 -0
  58. package/dist/_astro/menu.IPWdRoNM.js +1 -0
  59. package/dist/_astro/{open-docs-in-session.Xg3OSOfc.js → open-docs-in-session.CyFNgh1I.js} +1 -1
  60. package/dist/_astro/{plugins.BDZqJNOy.js → plugins.C2BMfzv1.js} +1 -1
  61. package/dist/_astro/{pm.Bl3hz9fF.js → pm.CagBA-ht.js} +2 -2
  62. package/dist/_astro/{portal.Cm9XlIPf.js → portal.C0hAhXDF.js} +2 -2
  63. package/dist/_astro/prompts-meta.Bua-j14I.js +2 -0
  64. package/dist/_astro/react-dom.DLW9pGCF.js +1 -0
  65. package/dist/_astro/{react-dom.CqAxpHMt.js → react.Blvh0GOs.js} +1 -1
  66. package/dist/_astro/report-prompt.CAfZ6RIo.js +1 -0
  67. package/dist/_astro/{settings.C6NWuiFO.js → settings.Ceefeil5.js} +2 -2
  68. package/dist/_astro/{spaces-client.k_ciWMai.js → spaces-client.BocUabwy.js} +1 -1
  69. package/dist/_astro/ui-auto-prompt.CX8fju7r.js +3 -0
  70. package/dist/_astro/ui-auto.BmC9JJ_X.js +1 -0
  71. package/dist/_astro/update.CtF8vsag.js +1 -0
  72. package/dist/_astro/use-text-value.DBQvHeNl.js +1 -0
  73. package/dist/_astro/use-tree-walker.DFU2AaAP.js +1 -0
  74. package/dist/_astro/useListAnim.BoerpmsB.js +1 -0
  75. package/dist/_astro/{view-prefs.Dyu_lmN2.js → view-prefs.BEAZRcxF.js} +1 -1
  76. package/dist/case-exec/index.html +458 -22
  77. package/dist/cases/generate/index.html +258 -22
  78. package/dist/cases/library/index.html +458 -22
  79. package/dist/dashboard/index.html +458 -22
  80. package/dist/docs/changelog/index.html +373 -23
  81. package/dist/docs/install/index.html +258 -22
  82. package/dist/docs/manual/index.html +314 -35
  83. package/dist/index.html +2 -281
  84. package/dist/knowledge/index.html +458 -22
  85. package/dist/knowledge//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/347/274/226/345/206/231/350/247/204/350/214/203/index.html +258 -22
  86. package/dist/plugins/index.html +258 -22
  87. package/dist/pm/bugs/index.html +458 -22
  88. package/dist/pm/iterations/index.html +458 -22
  89. package/dist/pm/projects/detail/index.html +458 -22
  90. package/dist/pm/projects/index.html +458 -22
  91. package/dist/pm/requirements/index.html +458 -22
  92. package/dist/prompts/index.html +458 -22
  93. package/dist/reports/index.html +458 -22
  94. package/dist/requirements/index.html +458 -22
  95. package/dist/tasks/index.html +458 -22
  96. package/dist/templates/index.html +458 -22
  97. package/dist/templates//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  98. package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  99. package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
  100. package/dist/templates//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
  101. package/dist/templates//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
  102. package/dist/test-automation/index.html +717 -0
  103. package/dist/test-stats/index.html +458 -22
  104. package/dist/todos/index.html +458 -22
  105. package/dist/workbench/index.html +517 -0
  106. package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +80 -43
  107. package/lh-plugins/lh-project-docs.mjs +14 -0
  108. package/lh-plugins/lh-uiauto.mjs +1445 -386
  109. package/lh-plugins/plugin-prompts.mjs +200 -0
  110. package/lh-plugins/storage-domain.mjs +6 -0
  111. package/lh-plugins/uiauto-adapters/index.mjs +57 -0
  112. package/lh-plugins/uiauto-adapters/playwright.mjs +215 -0
  113. package/lh-plugins/uiauto-adapters/pytest.mjs +151 -0
  114. package/lh-plugins/uiauto-adapters/selenium.mjs +163 -0
  115. package/lh-plugins/uiauto-adapters/shared.mjs +371 -0
  116. package/lh-plugins/uiauto-adapters/tasks.mjs +369 -0
  117. package/lh-plugins/uiauto-explorer/apps.mjs +332 -0
  118. package/lh-plugins/uiauto-explorer/diff.mjs +159 -0
  119. package/lh-plugins/uiauto-explorer/emit-files.mjs +506 -0
  120. package/lh-plugins/uiauto-explorer/emit-python.mjs +160 -0
  121. package/lh-plugins/uiauto-explorer/emit-ts.mjs +118 -0
  122. package/lh-plugins/uiauto-explorer/emit.mjs +15 -0
  123. package/lh-plugins/uiauto-explorer/ir.mjs +114 -0
  124. package/lh-plugins/uiauto-explorer/locators.mjs +267 -0
  125. package/lh-plugins/uiauto-explorer/merge.mjs +235 -0
  126. package/lh-plugins/uiauto-explorer/plan.mjs +165 -0
  127. package/lh-plugins/uiauto-explorer/runner.mjs +370 -0
  128. package/lh-plugins/uiauto-explorer/sessions.mjs +1373 -0
  129. package/lh-plugins/uiauto-explorer/snapshot.mjs +492 -0
  130. package/lh-plugins/uiauto-prompt.mjs +122 -0
  131. package/lh-plugins/uiauto-report.mjs +111 -0
  132. package/package.json +1 -1
  133. package/dist/_astro/Base.4X_wsSoR.css +0 -1
  134. package/dist/_astro/Base.astro_astro_type_script_index_0_lang.Dy7fJoxu.js +0 -1
  135. package/dist/_astro/CaseExecBoard.DKdfW5fM.js +0 -1
  136. package/dist/_astro/CaseGenerateApp.Bm8AyH9s.js +0 -9
  137. package/dist/_astro/CasesLibrary.CvVJZ74h.js +0 -1
  138. package/dist/_astro/ChatApp.BS-wLBRa.js +0 -276
  139. package/dist/_astro/ColumnResize.Bs6GL3DJ.js +0 -1
  140. package/dist/_astro/DatePicker.CMfX7F6z.js +0 -1
  141. package/dist/_astro/DocsFileBrowser.BtRoZ787.js +0 -1
  142. package/dist/_astro/DocsPager.C20ue_JZ.js +0 -1
  143. package/dist/_astro/KnowledgeLibrary.CV4DyStj.js +0 -1
  144. package/dist/_astro/NamePromptDialog.Dl5kSnQI.js +0 -1
  145. package/dist/_astro/NavLibraries.DmD2lZBZ.js +0 -1
  146. package/dist/_astro/OverflowHint.lzmplP5j.js +0 -1
  147. package/dist/_astro/PluginsPage.B59AMDrB.js +0 -1
  148. package/dist/_astro/PluginsPage.BEAPymNL.js +0 -39
  149. package/dist/_astro/PmBugsBoard.CPEyBp0l.js +0 -1
  150. package/dist/_astro/PmIterationsBoard.zCeLpcBo.js +0 -1
  151. package/dist/_astro/PmProjectDetail.ejIam5sD.js +0 -1
  152. package/dist/_astro/PmProjectsBoard.DbXzir8o.js +0 -1
  153. package/dist/_astro/PmRequirementsBoard.CynewRfv.js +0 -1
  154. package/dist/_astro/PmRequirementsBoard.D9eUMJWI.js +0 -1
  155. package/dist/_astro/PmShared.BxTHKdjY.js +0 -1
  156. package/dist/_astro/PromptsLibrary.Bkruu80b.js +0 -1
  157. package/dist/_astro/ReportsPage.BoBQshMQ.js +0 -1
  158. package/dist/_astro/RequirementsLibrary.BC0A8qdk.js +0 -1
  159. package/dist/_astro/SearchableSelect.CXa1hiDd.js +0 -1
  160. package/dist/_astro/SelectDropdown.Buv7rQJ_.js +0 -1
  161. package/dist/_astro/SettingsDialog.Dc-9QNJI.js +0 -1
  162. package/dist/_astro/SlideOverDrawer.TPbGEoWn.js +0 -1
  163. package/dist/_astro/TagSelect.u8DTop7v.js +0 -1
  164. package/dist/_astro/TasksBoard.Bb--oVV8.js +0 -1
  165. package/dist/_astro/TemplatesLibrary.D1wZq94E.js +0 -1
  166. package/dist/_astro/TestStatsBoard.DlVezlXl.js +0 -1
  167. package/dist/_astro/TodosBoard.S84wqcWY.js +0 -1
  168. package/dist/_astro/UiAutoBoard.BKp9ifxg.js +0 -1
  169. package/dist/_astro/active-element-history.CeefGkFQ.js +0 -1
  170. package/dist/_astro/app-config.DUyozIL9.js +0 -1
  171. package/dist/_astro/dialog.DXHznyjD.js +0 -1
  172. package/dist/_astro/jsx-runtime.BUXbF_vF.js +0 -1
  173. package/dist/_astro/listbox.Di-t5U37.js +0 -1
  174. package/dist/_astro/menu.Bkluqxic.js +0 -1
  175. package/dist/_astro/prompts-meta.F4wwkjUA.js +0 -2
  176. package/dist/_astro/report-prompt.Cmw4XzfY.js +0 -1
  177. package/dist/_astro/update.Bl_HfSe6.js +0 -1
  178. package/dist/_astro/use-text-value.BuY0Cm1z.js +0 -1
  179. package/dist/_astro/use-tree-walker.CfTHFX43.js +0 -1
  180. package/dist/_astro/useListAnim.CpGB1cft.js +0 -1
  181. package/dist/ui-automation/index.html +0 -281
@@ -0,0 +1,492 @@
1
+ /**
2
+ * 页面快照:把当前页面压成一棵「能点 / 能填」的精简树,每个节点带临时 ref。
3
+ *
4
+ * 设计对齐 Playwright MCP:用无障碍语义而不是截图,LLM 只认 ref。
5
+ * 区别在于 ref 存在页面里(Map + 元素上的 expando),并允许旧 ref 在元素
6
+ * 仍然存活时继续使用——比「快照即失效」宽容,少一次无谓的重新快照。
7
+ *
8
+ * ref 只是探索期的临时句柄,不能写进脚本;落盘前必须用 locators.mjs 转成
9
+ * getByRole / getByLabel 这类可导出的定位器。
10
+ */
11
+
12
+ /** 元素上记 ref 的属性名(只在页面内存里,不进 DOM 序列化) */
13
+ const REF_PROP = '__lanternRef';
14
+
15
+ /**
16
+ * 注意:这个函数会被 `page.evaluate` 序列化到页面里执行,
17
+ * 不能引用模块作用域的任何变量。
18
+ */
19
+ export function snapshotInPage(options) {
20
+ const opts = options || {};
21
+ const maxNodes = Number(opts.maxNodes) > 0 ? Number(opts.maxNodes) : 400;
22
+ const maxText = Number(opts.maxText) > 0 ? Number(opts.maxText) : 80;
23
+ const maxDepth = Number(opts.maxDepth) > 0 ? Number(opts.maxDepth) : 12;
24
+ const includeBoxes = Boolean(opts.includeBoxes);
25
+ const targetRef = opts.targetRef || '';
26
+
27
+ const refStore = window.__lanternRefs || (window.__lanternRefs = new Map());
28
+ let seq = Number(window.__lanternRefSeq) || 0;
29
+ // 每次导航后文档编号会变,旧 ref 不会恰好指向新页面里的同名序号
30
+ const prefix = window.__lanternRefPrefix || '';
31
+
32
+ // 清掉已经离开文档的旧 ref,避免 Map 无限长
33
+ for (const [key, element] of refStore) {
34
+ if (!element || !element.isConnected) refStore.delete(key);
35
+ }
36
+
37
+ const refOf = (element) => {
38
+ // 不能引用模块作用域常量:这个函数会被序列化到页面里执行
39
+ if (element.__lanternRef) return element.__lanternRef;
40
+ seq += 1;
41
+ const ref = `${prefix}e${seq}`;
42
+ element.__lanternRef = ref;
43
+ refStore.set(ref, element);
44
+ return ref;
45
+ };
46
+
47
+ const clean = (value) =>
48
+ String(value == null ? '' : value)
49
+ .replace(/\s+/g, ' ')
50
+ .trim();
51
+
52
+ const clip = (value) => {
53
+ const text = clean(value);
54
+ return text.length > maxText ? `${text.slice(0, maxText)}…` : text;
55
+ };
56
+
57
+ const escapeAttr = (value) => String(value).replace(/"/g, '\\"');
58
+
59
+ const roleOf = (element) => {
60
+ const explicit = element.getAttribute('role');
61
+ if (explicit) return explicit.split(/\s+/)[0];
62
+ const tag = element.tagName.toLowerCase();
63
+ if (tag === 'input') {
64
+ const type = (element.getAttribute('type') || 'text').toLowerCase();
65
+ if (type === 'checkbox') return 'checkbox';
66
+ if (type === 'radio') return 'radio';
67
+ if (type === 'range') return 'slider';
68
+ if (type === 'number') return 'spinbutton';
69
+ if (type === 'search') return 'searchbox';
70
+ if (type === 'submit' || type === 'button' || type === 'reset' || type === 'image')
71
+ return 'button';
72
+ if (type === 'file') return 'button';
73
+ if (type === 'hidden') return '';
74
+ return 'textbox';
75
+ }
76
+ if (tag === 'select') return element.multiple ? 'listbox' : 'combobox';
77
+ if (tag === 'textarea') return 'textbox';
78
+ if (/^h[1-6]$/.test(tag)) return 'heading';
79
+ if (tag === 'a') return element.hasAttribute('href') ? 'link' : '';
80
+ if (tag === 'img') return 'img';
81
+ if (tag === 'button') return 'button';
82
+ if (tag === 'summary') return 'button';
83
+ if (tag === 'li') return 'listitem';
84
+ if (tag === 'ul' || tag === 'ol') return 'list';
85
+ if (tag === 'table') return 'table';
86
+ if (tag === 'tr') return 'row';
87
+ if (tag === 'td') return 'cell';
88
+ if (tag === 'th') return 'columnheader';
89
+ if (tag === 'nav') return 'navigation';
90
+ if (tag === 'main') return 'main';
91
+ if (tag === 'header') return 'banner';
92
+ if (tag === 'footer') return 'contentinfo';
93
+ if (tag === 'form') return 'form';
94
+ if (tag === 'dialog') return 'dialog';
95
+ if (tag === 'section') return 'region';
96
+ if (tag === 'article') return 'article';
97
+ if (tag === 'aside') return 'complementary';
98
+ if (tag === 'p') return 'paragraph';
99
+ return '';
100
+ };
101
+
102
+ const INTERACTIVE = new Set([
103
+ 'button',
104
+ 'link',
105
+ 'textbox',
106
+ 'searchbox',
107
+ 'combobox',
108
+ 'listbox',
109
+ 'checkbox',
110
+ 'radio',
111
+ 'switch',
112
+ 'slider',
113
+ 'spinbutton',
114
+ 'menuitem',
115
+ 'tab',
116
+ 'option',
117
+ 'treeitem',
118
+ ]);
119
+ const STRUCTURAL = new Set([
120
+ 'heading',
121
+ 'main',
122
+ 'navigation',
123
+ 'banner',
124
+ 'contentinfo',
125
+ 'form',
126
+ 'dialog',
127
+ 'region',
128
+ 'list',
129
+ 'table',
130
+ 'article',
131
+ ]);
132
+
133
+ const accessibleName = (element) => {
134
+ const labelledBy = element.getAttribute('aria-labelledby');
135
+ if (labelledBy) {
136
+ const text = labelledBy
137
+ .split(/\s+/)
138
+ .map((id) => document.getElementById(id))
139
+ .filter(Boolean)
140
+ .map((node) => clean(node.textContent))
141
+ .join(' ');
142
+ if (text) return clip(text);
143
+ }
144
+ const ariaLabel = element.getAttribute('aria-label');
145
+ if (ariaLabel) return clip(ariaLabel);
146
+ if (element.id) {
147
+ const label = document.querySelector(`label[for="${escapeAttr(element.id)}"]`);
148
+ if (label) {
149
+ const text = clip(label.textContent);
150
+ if (text) return text;
151
+ }
152
+ }
153
+ const wrapper = element.closest('label');
154
+ if (wrapper && wrapper !== element) {
155
+ const text = clip(wrapper.textContent);
156
+ if (text) return text;
157
+ }
158
+ const placeholder = element.getAttribute('placeholder');
159
+ if (placeholder) return clip(placeholder);
160
+ const alt = element.getAttribute('alt');
161
+ if (alt) return clip(alt);
162
+ if (element.tagName === 'INPUT') {
163
+ const type = (element.getAttribute('type') || 'text').toLowerCase();
164
+ if (type === 'submit' || type === 'button' || type === 'reset') return clip(element.value);
165
+ return '';
166
+ }
167
+ if (element.tagName === 'SELECT' || element.tagName === 'TEXTAREA') return '';
168
+ const text = clip(element.textContent);
169
+ if (text) return text;
170
+ // 图标按钮:Ant Design 等把语义放在子节点的 aria-label / use#icon-* 上
171
+ const icon = element.querySelector('[aria-label]');
172
+ if (icon) {
173
+ const label = clip(icon.getAttribute('aria-label'));
174
+ if (label) return label;
175
+ }
176
+ const use = element.querySelector('use');
177
+ if (use) {
178
+ const href = use.getAttribute('href') || use.getAttribute('xlink:href') || '';
179
+ if (href) return clip(href.replace(/^#/, '').replace(/^icon-/, ''));
180
+ }
181
+ const svgTitle = element.querySelector('svg title');
182
+ if (svgTitle) {
183
+ const label = clip(svgTitle.textContent);
184
+ if (label) return label;
185
+ }
186
+ return clip(element.getAttribute('title'));
187
+ };
188
+
189
+ const isVisible = (element) => {
190
+ if (
191
+ element.tagName === 'INPUT' &&
192
+ (element.getAttribute('type') || '').toLowerCase() === 'hidden'
193
+ )
194
+ return false;
195
+ const rect = element.getBoundingClientRect();
196
+ if (rect.width < 1 || rect.height < 1) return false;
197
+ const style = getComputedStyle(element);
198
+ if (style.visibility === 'hidden' || style.display === 'none') return false;
199
+ if (Number(style.opacity) === 0) return false;
200
+ if (element.getAttribute('aria-hidden') === 'true') return false;
201
+ return true;
202
+ };
203
+
204
+ const extraStates = (element, role) => {
205
+ const parts = [];
206
+ if (element.disabled || element.getAttribute('aria-disabled') === 'true')
207
+ parts.push('disabled');
208
+ if (role === 'checkbox' || role === 'radio' || role === 'switch') {
209
+ const checked =
210
+ 'checked' in element ? Boolean(element.checked) : element.getAttribute('aria-checked');
211
+ if (checked === true || checked === 'true') parts.push('checked');
212
+ }
213
+ if (role === 'tab' || role === 'option') {
214
+ if (element.getAttribute('aria-selected') === 'true') parts.push('selected');
215
+ }
216
+ if (role === 'button' && element.getAttribute('aria-expanded')) {
217
+ parts.push(`expanded=${element.getAttribute('aria-expanded')}`);
218
+ }
219
+ if ((role === 'textbox' || role === 'combobox' || role === 'searchbox') && element.value) {
220
+ if ((element.getAttribute('type') || '').toLowerCase() !== 'password') {
221
+ parts.push(`value="${clip(element.value)}"`);
222
+ }
223
+ }
224
+ if (role === 'heading' && /^h[1-6]$/i.test(element.tagName)) {
225
+ parts.push(`level=${element.tagName.slice(1)}`);
226
+ }
227
+ const testId =
228
+ element.getAttribute('data-testid') ||
229
+ element.getAttribute('data-test-id') ||
230
+ element.getAttribute('data-test');
231
+ if (testId) parts.push(`testid=${testId}`);
232
+ if (element.getAttribute('title')) parts.push(`title="${clip(element.getAttribute('title'))}"`);
233
+ if (includeBoxes) {
234
+ const rect = element.getBoundingClientRect();
235
+ parts.push(
236
+ `box=${Math.round(rect.x)},${Math.round(rect.y)},${Math.round(rect.width)},${Math.round(
237
+ rect.height,
238
+ )}`,
239
+ );
240
+ }
241
+ return parts.length ? ` [${parts.join(' ')}]` : '';
242
+ };
243
+
244
+ const root = targetRef ? refStore.get(targetRef) : document.body;
245
+ if (!root) {
246
+ return { text: '', count: 0, url: location.href, title: document.title, missing: targetRef };
247
+ }
248
+
249
+ const lines = [];
250
+ const items = [];
251
+ let count = 0;
252
+ let textCount = 0;
253
+ let truncated = false;
254
+ const maxTextNodes = 150;
255
+
256
+ // depth 按「语义层级」算(只数被收录的节点),和 Playwright 的 a11y 树一致;
257
+ // domDepth 只用来防病态嵌套,不参与截断,否则真实页面里的输入框会被埋掉。
258
+ const walk = (element, domDepth, level) => {
259
+ if (!element || count >= maxNodes || level > maxDepth || domDepth > 80) {
260
+ if (count >= maxNodes) truncated = true;
261
+ return;
262
+ }
263
+ const tag = element.tagName ? element.tagName.toLowerCase() : '';
264
+ if (tag === 'script' || tag === 'style' || tag === 'noscript' || tag === 'template') return;
265
+
266
+ const role = roleOf(element);
267
+ const interactive = INTERACTIVE.has(role) || element.hasAttribute('onclick');
268
+ const structural = STRUCTURAL.has(role);
269
+ const visible = isVisible(element);
270
+ let nextLevel = level;
271
+
272
+ if (visible && (interactive || structural)) {
273
+ const name = accessibleName(element);
274
+ const indent = ' '.repeat(Math.min(level, 10));
275
+ const ref = interactive ? refOf(element) : '';
276
+ const state = extraStates(element, role);
277
+ const label = name ? ` "${name}"` : '';
278
+ const refPart = ref ? ` [ref=${ref}]` : '';
279
+ lines.push(`- ${indent}${role}${label}${state}${refPart}`);
280
+ count += 1;
281
+ items.push({ ref, role, name, tag, interactive, index: count });
282
+ nextLevel = level + 1;
283
+ } else if (
284
+ visible &&
285
+ !role &&
286
+ element.children.length === 0 &&
287
+ textCount < maxTextNodes &&
288
+ !element.closest('button, a, label, option, h1, h2, h3, h4, h5, h6, script, style')
289
+ ) {
290
+ // 纯文本节点:断言要看的结果(提示语、表格内容、状态文案)都在这里
291
+ const text = clip(element.textContent);
292
+ if (text) {
293
+ const ref = refOf(element);
294
+ lines.push(`- ${' '.repeat(Math.min(level, 10))}text: ${text} [ref=${ref}]`);
295
+ textCount += 1;
296
+ items.push({ ref, role: 'text', name: text, tag, interactive: false, index: count });
297
+ }
298
+ }
299
+
300
+ const children = [];
301
+ for (const child of element.children) children.push(child);
302
+ if (element.shadowRoot) {
303
+ for (const child of element.shadowRoot.children) children.push(child);
304
+ }
305
+ for (const child of children) walk(child, domDepth + 1, nextLevel);
306
+ };
307
+
308
+ walk(root, 0, 0);
309
+ window.__lanternRefSeq = seq;
310
+
311
+ return {
312
+ text: lines.join('\n'),
313
+ count,
314
+ truncated,
315
+ url: location.href,
316
+ title: document.title,
317
+ items,
318
+ };
319
+ }
320
+
321
+ /** 页面内取元素的定位信息(给 locators.mjs 拼候选定位器用) */
322
+ export function describeElementInPage(element) {
323
+ const clean = (value) =>
324
+ String(value == null ? '' : value)
325
+ .replace(/\s+/g, ' ')
326
+ .trim();
327
+ const escapeAttr = (value) => String(value).replace(/"/g, '\\"');
328
+ const stableId = (value) => {
329
+ const id = clean(value);
330
+ if (!id) return '';
331
+ if (/^\d/.test(id)) return '';
332
+ // 纯数字 / uuid / 哈希这类生成 id 换一次渲染就变,不能当契约
333
+ if (/^(?:[0-9a-f]{8,}|[a-z]*-?\d{5,})$/i.test(id)) return '';
334
+ if (/[0-9a-f]{8,}-[0-9a-f]{4}/i.test(id)) return '';
335
+ return id;
336
+ };
337
+ const role = (() => {
338
+ const explicit = element.getAttribute('role');
339
+ if (explicit) return explicit.split(/\s+/)[0];
340
+ const tag = element.tagName.toLowerCase();
341
+ if (tag === 'input') {
342
+ const type = (element.getAttribute('type') || 'text').toLowerCase();
343
+ if (type === 'checkbox') return 'checkbox';
344
+ if (type === 'radio') return 'radio';
345
+ if (type === 'range') return 'slider';
346
+ if (type === 'number') return 'spinbutton';
347
+ if (type === 'search') return 'searchbox';
348
+ if (type === 'submit' || type === 'button' || type === 'reset') return 'button';
349
+ if (type === 'file') return 'button';
350
+ return 'textbox';
351
+ }
352
+ if (tag === 'select') return element.multiple ? 'listbox' : 'combobox';
353
+ if (tag === 'textarea') return 'textbox';
354
+ if (tag === 'a') return element.hasAttribute('href') ? 'link' : '';
355
+ if (tag === 'button') return 'button';
356
+ if (tag === 'summary') return 'button';
357
+ if (/^h[1-6]$/.test(tag)) return 'heading';
358
+ if (tag === 'img') return 'img';
359
+ return '';
360
+ })();
361
+
362
+ const name = (() => {
363
+ const labelledBy = element.getAttribute('aria-labelledby');
364
+ if (labelledBy) {
365
+ const text = labelledBy
366
+ .split(/\s+/)
367
+ .map((id) => document.getElementById(id))
368
+ .filter(Boolean)
369
+ .map((node) => clean(node.textContent))
370
+ .join(' ');
371
+ if (text) return clean(text);
372
+ }
373
+ const ariaLabel = element.getAttribute('aria-label');
374
+ if (ariaLabel) return clean(ariaLabel);
375
+ if (element.id) {
376
+ const label = document.querySelector(`label[for="${escapeAttr(element.id)}"]`);
377
+ if (label) {
378
+ const text = clean(label.textContent);
379
+ if (text) return text;
380
+ }
381
+ }
382
+ const wrapper = element.closest('label');
383
+ if (wrapper && wrapper !== element) {
384
+ const text = clean(wrapper.textContent);
385
+ if (text) return text;
386
+ }
387
+ const placeholder = element.getAttribute('placeholder');
388
+ if (placeholder) return clean(placeholder);
389
+ const alt = element.getAttribute('alt');
390
+ if (alt) return clean(alt);
391
+ if (element.tagName === 'INPUT') {
392
+ const type = (element.getAttribute('type') || 'text').toLowerCase();
393
+ if (type === 'submit' || type === 'button' || type === 'reset') return clean(element.value);
394
+ return '';
395
+ }
396
+ if (element.tagName === 'SELECT' || element.tagName === 'TEXTAREA') return '';
397
+ const text = clean(element.textContent);
398
+ if (text && text.length <= 60) return text;
399
+ const icon = element.querySelector('[aria-label]');
400
+ if (icon) {
401
+ const label = clean(icon.getAttribute('aria-label'));
402
+ if (label) return label;
403
+ }
404
+ const use = element.querySelector('use');
405
+ if (use) {
406
+ const href = use.getAttribute('href') || use.getAttribute('xlink:href') || '';
407
+ if (href) return clean(href.replace(/^#/, '').replace(/^icon-/, ''));
408
+ }
409
+ const svgTitle = element.querySelector('svg title');
410
+ if (svgTitle) {
411
+ const label = clean(svgTitle.textContent);
412
+ if (label) return label;
413
+ }
414
+ return clean(element.getAttribute('title'));
415
+ })();
416
+
417
+ const labelText = (() => {
418
+ if (element.id) {
419
+ const label = document.querySelector(`label[for="${escapeAttr(element.id)}"]`);
420
+ if (label) return clean(label.textContent);
421
+ }
422
+ const wrapper = element.closest('label');
423
+ if (wrapper && wrapper !== element) return clean(wrapper.textContent);
424
+ return '';
425
+ })();
426
+
427
+ const cssPath = (() => {
428
+ const parts = [];
429
+ let node = element;
430
+ for (let i = 0; i < 5 && node && node.nodeType === 1; i += 1) {
431
+ const tag = node.tagName.toLowerCase();
432
+ const testId =
433
+ node.getAttribute('data-testid') ||
434
+ node.getAttribute('data-test-id') ||
435
+ node.getAttribute('data-test');
436
+ if (testId) {
437
+ parts.unshift(`[data-testid="${escapeAttr(testId)}"]`);
438
+ break;
439
+ }
440
+ const nameAttr = node.getAttribute('name');
441
+ if (nameAttr) {
442
+ parts.unshift(`${tag}[name="${escapeAttr(nameAttr)}"]`);
443
+ break;
444
+ }
445
+ const id = stableId(node.id);
446
+ if (id) {
447
+ parts.unshift(`#${id}`);
448
+ break;
449
+ }
450
+ const parent = node.parentElement;
451
+ let part = tag;
452
+ if (parent) {
453
+ const siblings = Array.from(parent.children).filter(
454
+ (child) => child.tagName === node.tagName,
455
+ );
456
+ if (siblings.length > 1) part += `:nth-of-type(${siblings.indexOf(node) + 1})`;
457
+ }
458
+ parts.unshift(part);
459
+ node = node.parentElement;
460
+ }
461
+ return parts.join(' > ');
462
+ })();
463
+
464
+ return {
465
+ tag: element.tagName.toLowerCase(),
466
+ role,
467
+ name,
468
+ labelText,
469
+ placeholder: clean(element.getAttribute('placeholder')),
470
+ ariaLabel: clean(element.getAttribute('aria-label')),
471
+ testId:
472
+ clean(element.getAttribute('data-testid')) ||
473
+ clean(element.getAttribute('data-test-id')) ||
474
+ clean(element.getAttribute('data-test')),
475
+ id: clean(element.id),
476
+ nameAttr: clean(element.getAttribute('name')),
477
+ href: clean(element.getAttribute('href')).slice(0, 120),
478
+ text: clean(element.textContent).slice(0, 80),
479
+ cssPath,
480
+ hasAriaLabel: Boolean(element.getAttribute('aria-label')),
481
+ };
482
+ }
483
+
484
+ /** 取页面快照(Node 侧入口) */
485
+ export async function takeSnapshot(page, options = {}) {
486
+ return page.evaluate(snapshotInPage, options);
487
+ }
488
+
489
+ /** 取元素定位信息(Node 侧入口) */
490
+ export async function describeElement(page, handle) {
491
+ return page.evaluate(describeElementInPage, handle);
492
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * 「自动化测试」的提示词文件:种子正文 + 分段解析。
3
+ *
4
+ * 文件放在当前空间的「提示词管理/Playwright提示词.md」,由插件开关联动:
5
+ * 打开插件时缺就补种子(没人改过时会跟着内置模板升级),插件开启期间不允许删除 / 改名。
6
+ * 生成页每次开始规划 / 探索前重新读一次,改了提示词不用重启服务。
7
+ *
8
+ * 文件用两级标题分三段:
9
+ * - `## 规划阶段`:只拆步骤、不打开浏览器的 system prompt
10
+ * - `## 探索阶段`:驱动浏览器定位元素、提交步骤的 system prompt
11
+ * - `## 生成代码`:出码 / 写入时的工程约定(项目结构、语法、代码规范),拼进探索阶段的 system prompt
12
+ * 占位符:{{project}} 项目名、{{dir}} 项目目录、{{language}} 目标语言、{{plan}} 步骤计划
13
+ */
14
+
15
+ import { UI_AUTO_PROMPT_FILE } from './plugin-prompts.mjs';
16
+
17
+ export { UI_AUTO_PROMPT_FILE };
18
+
19
+ export const PLAN_SECTION = '规划阶段';
20
+ export const AGENT_SECTION = '探索阶段';
21
+ export const CODE_SECTION = '生成代码';
22
+
23
+ export const UI_AUTO_PROMPT_TEMPLATE = `# Playwright提示词
24
+
25
+ > 这份提示词由「自动化测试」插件生成并引用,分「规划阶段」「探索阶段」「生成代码」三段。
26
+ > 插件开启期间不能删除本文件;关闭插件后可以删掉,再次打开插件会重新生成。
27
+ > 可用占位符:\`{{project}}\` 项目名、\`{{dir}}\` 项目目录、\`{{language}}\` 目标语言、\`{{plan}}\` 已确认的步骤计划。
28
+
29
+ ## 规划阶段
30
+
31
+ 你是资深测试设计师。现在只做一件事:把需求拆成可执行、可人工编辑的测试步骤计划。
32
+
33
+ 项目:{{project}}({{dir}}),最终脚本语言:{{language}}。
34
+
35
+ 规则:
36
+ 1. 只调用 ui_plan_steps 一次,提交完整计划;不要打开浏览器、不要写定位器。
37
+ 2. 每步只描述「动作 + 目标」:target 用页面上的可见文案 / 位置描述,例如「用户名输入框」「右上角登录按钮」。
38
+ 3. 账号、密码、手机号等占位数据写成 \${ENV_NAME},不要编造真实凭据。
39
+ 4. 计划要覆盖:打开页面、关键前置(登录 / 导航)、核心操作、至少一条结果断言(用户能看到什么)。
40
+ 5. 步骤 4~12 步为宜,不要为了凑数拆得过细;一个动作一句话。
41
+ 6. 只写测试步骤,不要写「截图」:截屏是探索时给人看的记录,不会进脚本。
42
+
43
+ 提交后简短说明你的测试思路与需要人工确认的点。
44
+
45
+ ## 探索阶段
46
+
47
+ 你是 UI 自动化测试工程师:在真实浏览器里把流程点一遍,确认每个元素都能唯一定位,再提交步骤。
48
+
49
+ 当前挂载项目:{{project}}({{dir}}),生成目标语言:{{language}}。
50
+
51
+ 工作方式(必须遵守):
52
+ 1. 先用 ui_browser_open 打开页面,再按快照里的 ref 操作;每次点击 / 填写后会自动返回新快照。
53
+ 2. ref 是临时的,页面导航或刷新后会失效;失效就重新 ui_browser_snapshot,不要猜。
54
+ 需要等异步渲染时用 ui_browser_wait({ text: "某段文案" }),不要用长时间死等。
55
+ 3. 写步骤之前,对每个要操作或断言的元素调用 ui_locator_probe,用返回的 recommended;
56
+ recommended 带 nth 时表示同名元素取第 N 个(脚本里是 .first() / .nth(n)),照抄即可。
57
+ 没有唯一候选时换更具体的元素,不要自己拼长 CSS / XPath。
58
+ 4. 用 ui_test_set_steps 提交整份步骤(会刷新代码预览)。**探索阶段不要调用 ui_test_verify**:
59
+ 试跑是用户确认代码后手动发起的;只有收到试跑失败日志、让你修步骤时才重跑。
60
+ 5. 如果页面行为本身与需求不符,保留现状并在回答里说明是疑似缺陷,不要为了跑绿改断言。
61
+ 6. 占位数据(账号、手机号)写成 \${ENV_NAME},不要编造真实凭据。
62
+ 7. 至少要有一条 expect 断言,断言用户能看到的结果,而不是断言元素存在就算过。
63
+ 8. 关键节点(登录后、进入目标页、操作完成)调用一次 ui_browser_screenshot,用户会在面板里看到这一步的截图。
64
+ 9. 点击没反应 / 页面报错时,用 ui_browser_console 与 ui_browser_network 看错误,别盲目重试。
65
+
66
+ {{plan}}
67
+
68
+ 步骤 IR 的 action 只有:goto / click / fill / select / press / hover / expect / expectUrl / wait。
69
+ locator.kind 只能是 testid / role / label / placeholder / text / attr / css,优先 role 与 label。
70
+
71
+ ## 生成代码
72
+
73
+ 出码与写入项目时,必须落在项目原有的结构和规范上,不要另起一套:
74
+
75
+ 1. **先看项目再写**:动手前读项目里已有的用例、page object、fixture 与配置(目录结构、文件名、导入别名、是否用 path alias),新文件放到同一套目录里,不要新造一层 tests/api、tests/e2e 之类的平行结构。
76
+ 2. **复用已有部件**:项目已有 page object / helper / fixture(如登录态、baseURL、测试数据工厂)就复用,不要重复实现,也不要改它们的签名去迁就新用例。
77
+ 3. **语法与语言规范**:跟随项目现有语法风格——TypeScript 项目用项目既有的严格度与类型写法(import type / interface 放哪、是否允许 any),Python 项目跟随现有 pytest 风格(fixture 作用域、参数化、mark / conftest 位置)。同一仓库里不混用两种风格。
78
+ 4. **代码规范**:命名跟随项目(文件 kebab-case 还是 PascalCase、类名 / 函数名风格、常量位置),引号、分号、缩进、换行长度按项目 formatter 配置(prettier / eslint / ruff / black)来,提交前能通过项目自己的 lint / format 检查。不要引入新的格式化工具或依赖。
79
+ 5. **改动最小**:已有文件只追加必要内容(用例、方法、import),不重排原有代码、不顺手格式化别人的文件、不删无关代码;新增文件才写完整骨架。
80
+ 6. **测试本身**:用例名用中文描述业务行为;一个用例只验证一件事;选择器用第 3 步确认的定位器;敏感值走环境变量,不写死账号密码;不加与断言无关的 sleep,等待交给框架的自动等待与显式 expect。
81
+ 7. **注释克制**:只写「为什么这么做」(业务前提、时序坑),不复述代码;不要在文件头堆生成说明这类噪音。
82
+
83
+ 最后用中文简短汇报:覆盖了什么流程、每步用的定位策略、改了 / 新增了哪些文件、还有哪些没验证到。
84
+ `;
85
+
86
+ /**
87
+ * 解析提示词文件里的三段正文。
88
+ * 认 `## 规划阶段` / `## 探索阶段` / `## 生成代码`(也接受不带「阶段」的写法);
89
+ * 某段缺失或为空时返回空串,调用方回退到内置提示词 / 不追加对应段落。
90
+ */
91
+ export function parseUiAutoPrompt(text) {
92
+ const raw = String(text ?? '');
93
+ const out = { plan: '', agent: '', code: '' };
94
+ if (!raw.trim()) return out;
95
+ const lines = raw.split('\n');
96
+ let current = '';
97
+ let buffer = [];
98
+ const flush = () => {
99
+ const body = buffer.join('\n').trim();
100
+ if (current === 'plan') out.plan = body;
101
+ else if (current === 'agent') out.agent = body;
102
+ else if (current === 'code') out.code = body;
103
+ buffer = [];
104
+ };
105
+ for (const line of lines) {
106
+ const heading = /^##\s+(.+?)\s*$/.exec(line);
107
+ if (heading) {
108
+ flush();
109
+ const name = heading[1];
110
+ if (/规划/.test(name)) current = 'plan';
111
+ else if (/探索/.test(name)) current = 'agent';
112
+ else if (/代码|出码/.test(name)) current = 'code';
113
+ else current = '';
114
+ continue;
115
+ }
116
+ // 一级标题 / 引用块在段外,忽略即可(段内保留引用)
117
+ if (!current && (/^#\s/.test(line) || /^>\s?/.test(line))) continue;
118
+ if (current) buffer.push(line);
119
+ }
120
+ flush();
121
+ return out;
122
+ }