Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
261 lines (255 loc) · 20.2 KB
/
Copy pathindex.html
File metadata and controls
261 lines (255 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>当 AI Agent 跑 UI 测试:你怎么知道一个 FAIL 是真的?</title>
<meta name="description" content="When your AI agent reports a UI test FAIL, is it real? Three real misattributions, a nine-class failure taxonomy, and the principle that evidence must never decide the verdict.">
<style>
:root{
--bg:#fbfaf7; --fg:#1e1e21; --muted:#6b6b73; --line:#e3e0d8;
--code-bg:#1d2026; --code-fg:#e8e6e3; --accent:#b4441f; --card:#fff;
--quote-bg:#f4f1ea;
}
@media (prefers-color-scheme: dark){
:root{
--bg:#16171a; --fg:#e6e5e2; --muted:#9a9aa2; --line:#2c2e33;
--code-bg:#0f1115; --code-fg:#dcdcd8; --accent:#e07a52; --card:#1c1e22;
--quote-bg:#1f2126;
}
}
*{box-sizing:border-box}
body{
margin:0; background:var(--bg); color:var(--fg);
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans SC","PingFang SC","Microsoft YaHei",sans-serif;
font-size:17px; line-height:1.78; letter-spacing:.01em;
-webkit-font-smoothing:antialiased;
}
.wrap{max-width:760px; margin:0 auto; padding:72px 24px 120px}
h1,h2,h3,h4{line-height:1.35; font-weight:700; margin:2.4em 0 .7em}
h1{font-size:2.05em; margin-top:0; letter-spacing:-.01em}
h2{font-size:1.42em; padding-bottom:.32em; border-bottom:1px solid var(--line)}
h3{font-size:1.16em}
h1+h2{margin-top:1.2em; font-size:1.05em; color:var(--muted); font-weight:500; border:0}
p{margin:1.05em 0}
strong{font-weight:700}
a{color:var(--accent)}
hr{border:0; border-top:1px solid var(--line); margin:3em 0}
blockquote{
margin:1.6em 0; padding:1.05em 1.3em; background:var(--quote-bg);
border-left:3px solid var(--accent); border-radius:0 6px 6px 0;
color:var(--fg); font-size:.97em;
}
blockquote strong{color:var(--accent)}
code{
font-family:"SF Mono",ui-monospace,"Cascadia Code",Consolas,monospace;
font-size:.885em; background:rgba(128,128,128,.14);
padding:.14em .38em; border-radius:4px;
}
pre{
background:var(--code-bg); color:var(--code-fg); padding:1.15em 1.25em;
border-radius:9px; overflow-x:auto; margin:1.5em 0; line-height:1.6;
border:1px solid rgba(255,255,255,.06);
}
pre code{background:none; padding:0; color:inherit; font-size:.855em}
.tw{overflow-x:auto; margin:1.6em 0}
table{border-collapse:collapse; width:100%; font-size:.93em}
th,td{border:1px solid var(--line); padding:.6em .8em; text-align:left; vertical-align:top}
th{background:rgba(128,128,128,.09); font-weight:700; white-space:nowrap}
tbody tr:nth-child(even){background:rgba(128,128,128,.035)}
ul{padding-left:1.35em; margin:1.05em 0}
li{margin:.42em 0}
ul.checklist{list-style:none; padding-left:0}
ul.checklist li{display:flex; gap:.55em; align-items:flex-start; margin:.5em 0}
ul.checklist .box{color:var(--accent); font-size:.95em; line-height:1.7}
ul.checklist li.done{color:var(--muted)}
.meta{
margin-top:64px; padding-top:22px; border-top:1px solid var(--line);
color:var(--muted); font-size:.86em; line-height:1.7;
}
@media (max-width:640px){
body{font-size:16px}
.wrap{padding:44px 18px 90px}
h1{font-size:1.62em}
h2{font-size:1.26em}
}
</style>
</head>
<body>
<div class="wrap">
<article>
<p class="meta" style="margin-top:0;border:0;padding-top:0">When Your Agent Reports a FAIL, Is It Real?</p>
<blockquote>A test that fails for the wrong reason is worse than no test at all.<br>It teaches the team to ignore red.<br>因为错误的原因失败的测试,比没有测试更糟——它会训练整个团队忽略红灯。</blockquote>
<hr>
<h2>0. 先讲一个我自己的事故</h2>
<p>在一次 17 个用例的回归跑批里,我的 agent 报了 <strong>7 个 FAIL</strong>。</p>
<p>这 7 个 FAIL,<strong>一条都不成立</strong>。</p>
<p>每一个用例的断言都通过了,产品行为完全正确。它们被判 FAIL 的唯一原因是:<code>page.screenshot()</code> 抛了一个错——跑批进行到第 11 条时,浏览器窗口被最小化了。</p>
<pre><code class="language-js">// 断言行:全部 pass
assert(els.length === 3); // ✅
assert(state.activeTab === 'info'); // ✅
// 证据行:抛错 → 整个用例被判 FAIL
await page.screenshot({ path: 'step11.png' });
// Error: Cannot capture a screenshot while the window is minimized.</code></pre>
<p>我没有犹豫就把 7 个 FAIL 归给了"产品"。是 review 的时候才发现,产品根本没问题。</p>
<p>这就是本文要解决的一件事:</p>
<blockquote><strong>Agent 跑测试,最大的风险不是跑不通,而是它会自信地骗你。</strong><br>The dangerous failure mode is not a crash. It's <strong>false confidence</strong>.</blockquote>
<hr>
<h2>1. 问题的形状 / The Shape of the Problem</h2>
<p>人工测试时代,"这个 FAIL 是不是产品的问题"由人的经验兜底。测试同学点一下就知道:"这不是 bug,是环境没起来。"</p>
<p>换成 agent 之后,这个判断<strong>必须被显式地写进流程</strong>,否则 agent 会默认做一个最糟的选择:</p>
<blockquote>我没有观察到预期结果 → 产品有 bug。</blockquote>
<p>这个推理链条里缺了一环:<strong>"我没有观察到"和"它不存在"是两件事</strong>。</p>
<p>同一天里我连踩三次,加起来 15 条假 FAIL:</p>
<div class="tw"><table><thead><tr><th>#</th><th>事故</th><th>假 FAIL 数</th><th>真正的根因</th></tr></thead><tbody><tr><td>1</td><td>取证通道失败,被当成断言失败</td><td><strong>7</strong></td><td><code>screenshot()</code> 抛错未隔离</td></tr><tr><td>2</td><td>DOM 断言假阳性</td><td><strong>8</strong></td><td><code>v-show</code> 节点仍在、选择器跨区域污染</td></tr><tr><td>3</td><td>把构建默认行为当缺陷</td><td>连续错判 <strong>2 次</strong></td><td>webpack 自动注入 <code><link rel=prefetch></code></td></tr></tbody></table></div>
<p>这三件事看起来是三码事,其实是同一个病:<strong>判定(verdict)和观测(observation)没有分离</strong>。</p>
<hr>
<h2>2. 事故一:证据通道失败,把 15 条结论污染了 7 条</h2>
<h3>症状 / Symptom</h3>
<p>跑批到一半,窗口被最小化。<code>screenshot()</code> 开始抛错。<strong>后续每个用例都被整条判 FAIL</strong>,尽管断言全通过。</p>
<p>这个设计错误极其常见,因为它写起来最自然:</p>
<pre><code class="language-js">// ❌ 错误:证据是流程的一部分
async function runCase(page, c) {
const asserts = await check(page, c);
await page.screenshot({ path: `shots/${c.id}.png` }); // 抛错 → 整个 case 挂掉
return asserts.every(Boolean) ? 'PASS' : 'FAIL';
}</code></pre>
<h3>本质 / Root cause</h3>
<blockquote><strong>断言是主体,证据是辅助。</strong><br>The assertion is the verdict. The screenshot is a witness, not a judge.</blockquote>
<p>取证是<strong>旁路动作(side channel)</strong>。旁路出问题,不应该反过来改写主路的结论。</p>
<h3>解法 / The fix</h3>
<p>把取证封装成<strong>永不抛错</strong>的函数,双通道降级,都失败就认账并放行判定:</p>
<pre><code class="language-js">// ✅ 正确:证据永不参与判定
async function capture(page, base) {
try {
await page.screenshot({ path: `${base}.png` });
return { ref: `${base}.png`, mode: 'png' };
} catch (e1) {
try {
// printToPDF 不依赖合成器帧(compositor frame),
// 窗口最小化时仍能出图 —— 这是唯一可靠的降级通道
await page.pdf({ path: `${base}.pdf`, printBackground: true });
return { ref: `${base}.pdf`, mode: 'pdf' };
} catch (e2) {
// 两条通道都失败:登记缺口,判定照旧
return { ref: null, mode: 'none', gap: [e1.message, e2.message] };
}
}
}
async function runCase(page, c) {
const result = await check(page, c); // 判定只来自这里
const shot = await capture(page, `shots/${c.id}`);
return {
...result,
evidence_refs: [shot.ref].filter(Boolean), // null 被过滤掉
shotMode: shot.mode, // png | pdf | none
evidenceGaps: shot.gap || [],
};
}</code></pre>
<p>改完之后同一场景的结果:</p>
<div class="tw"><table><thead><tr><th></th><th>修复前</th><th>修复后</th></tr></thead><tbody><tr><td>用例结果</td><td><strong>10/17</strong> + 7 条假 FAIL</td><td><strong>17/17</strong></td></tr><tr><td>证据</td><td>7 条用例无图</td><td>25 张图(中途自动切 PDF)</td></tr><tr><td>团队反应</td><td>以为产品大面积故障</td><td>正常,只是窗口被最小化了</td></tr></tbody></table></div>
<h3>一个反直觉的注意点</h3>
<p><strong>不要用"视口尺寸"去判断"能不能截图"。</strong></p>
<p>窗口最小化时,视口<strong>仍然是非零的</strong>——我实测是 <code>995x913</code>。也就是说:</p>
<pre><code class="language-js">const { viewport } = page.info();
// viewport.width > 0 ✅ ... 但截图照样失败</code></pre>
<p>尺寸探针会<strong>静默通过</strong>,然后截图炸掉。正确的做法是<strong>直接看错误文案</strong>:</p>
<pre><code class="language-js">if (/while the window is minimized/i.test(err.message)) {
// 明确是窗口状态,可自动恢复后重试
}</code></pre>
<blockquote>Probe the error message, not the proxy metric.<br>探错误文案,不要探代理指标。</blockquote>
<hr>
<h2>3. 事故二:DOM 断言的假阳性(8 条)</h2>
<p>这批 FAIL 更好笑——<strong>产品确实是对的,是我的选择器在说谎</strong>。</p>
<h3>陷阱 1:<code>v-show</code> 的节点永远在</h3>
<p>Vue 的 <code>v-show</code> 只是切换 <code>display:none</code>,<strong>DOM 节点不会移除</strong>。</p>
<pre><code class="language-js">// ❌ 关闭弹窗后,节点还在,断言恒为 true → 永远 PASS(假阴性)
!!document.querySelector('.overlay')
// ✅ 必须读计算样式
getComputedStyle(document.querySelector('.overlay')).display !== 'none'</code></pre>
<p>我第一次写成了反向判断("遮罩残留"),结果弹窗<strong>已经正常关闭</strong>了,却一直报残留。</p>
<h3>陷阱 2:同名 class 跨区域污染</h3>
<p>一个弹窗里的 tab 和设置页侧栏的 tab,用了<strong>完全相同的 class</strong>。全局查询把两处一起数了:</p>
<pre><code class="language-js">// ❌ 数出 8 个 tab,误判"渲染异常"
document.querySelectorAll('.header-tab .buttons')
// ✅ 必须限定作用域
document.querySelectorAll('.modal .header-tab .buttons')</code></pre>
<blockquote>Every selector needs a scope. Unscoped <code>querySelectorAll</code> is a false-positive generator.<br>选择器必须限定作用域,否则它就是个假阳性发生器。</blockquote>
<h3>陷阱 3:<code>goto()</code> 会毁掉 SPA 上下文</h3>
<p>在单页应用里用 <code>page.goto()</code> 切页,会<strong>重建整个文档</strong>。之后 <code>page.evaluate()</code> 直接报:</p>
<pre><code>'Runtime.evaluate' wasn't found</code></pre>
<p>正确做法是走路由:</p>
<pre><code class="language-js">// ❌ 整页重载,Vue 实例被销毁
await page.goto('https://app/#/settings');
// ✅ 只改 hash,保留应用上下文
await page.evaluate(() => { location.hash = '#/settings'; });</code></pre>
<h3>陷阱 4:恒真断言</h3>
<pre><code class="language-js">// ❌ 永远不会失败 —— 等于没验
assert(result.text || true);
assert(!!(await page.$('body')));</code></pre>
<blockquote><strong>自检方法</strong>:把断言逐条读一遍,问"它有可能为 false 吗?"<br>如果答不上来,这条断言就是装饰品。</blockquote>
<hr>
<h2>4. 事故三:我把构建机制的默认行为当成了缺陷</h2>
<p>这条最值得说,因为它不是技术问题,是<strong>思维问题</strong>。</p>
<p><strong>场景</strong>:一个异步加载的对话框模块被改成了懒加载。我写的断言是:</p>
<blockquote>冷启动时,<strong>不应该</strong>发起对这个模块的请求。</blockquote>
<p>它一直 FAIL。我连续归因错了两次:</p>
<p>1. 第一次猜:<strong>会话复用导致缓存是热的</strong> → 加了隔离,还是 FAIL 2. 第二次猜:<strong>CDN/浏览器缓存</strong> → 清缓存,还是 FAIL</p>
<p>第三次我去拉真实数据,才发现真相:</p>
<blockquote>打包工具为<strong>每一个</strong>异步 chunk 自动注入了 <code><link rel="prefetch"></code>,这个应用一共 51 条,其中就包含那个模块。所以冷启动必然预取。</blockquote>
<p><strong>这是构建工具的默认行为,跟本次改动毫无关系。</strong> 我的断言从一开始就是错的。</p>
<p>正确的判据应该打在<strong>改动真正改变了什么</strong>上:</p>
<div class="tw"><table><thead><tr><th></th><th>判据</th><th>结论</th></tr></thead><tbody><tr><td>❌ 错</td><td>冷启动是否预取该 chunk</td><td>受构建机制支配,改动前后完全一样</td></tr><tr><td>✅ 对</td><td>代码是否<strong>已从主包拆出</strong></td><td>主包 1.14MB 不含模板 / chunk 92KB 含模板 / 主包以动态 <code>import()</code> 引用</td></tr></tbody></table></div>
<h3>元教训 / Meta-lesson</h3>
<blockquote><strong>连续两次归因错误,都是因为用推测代替了数据。</strong><br>Twice in a row, I explained a FAIL with a hypothesis instead of an observation.</blockquote>
<p>正确动作不是"再想想还有别的原因吗",而是<strong>把观测数据拉出来</strong>:</p>
<pre><code class="language-js">// 别猜,直接看
performance.getEntriesByType('resource').map(r => r.name)
document.querySelectorAll('link[rel=prefetch]').length</code></pre>
<hr>
<h2>5. 核心解法:把归因显式化(failure_class)</h2>
<p>上面三个事故有个共同点:<strong>每次都是"我以为"而不是"我验证"</strong>。</p>
<p>所以我给每个用例强制加一个字段:<code>failure_class</code>。它必须在报告里出现,不能留空。</p>
<h3>九类 / Nine classes</h3>
<div class="tw"><table><thead><tr><th>class</th><th>含义</th><th>计作"产品未通过"</th></tr></thead><tbody><tr><td><code>PRODUCT_BUG</code></td><td>产品行为与规格不符</td><td>✅ <strong>是</strong></td></tr><tr><td><code>HARNESS</code></td><td>脚本/选择器/驱动没打通</td><td>❌</td></tr><tr><td><code>TEST_DATA</code></td><td>前置数据不对</td><td>❌</td></tr><tr><td><code>SPEC_GAP</code></td><td>需求没写清,无法判断对错</td><td>❌</td></tr><tr><td><code>ENV</code></td><td>环境、部署、依赖问题</td><td>❌</td></tr><tr><td><code>TOOLING</code></td><td>工具链或浏览器能力缺陷</td><td>❌</td></tr><tr><td><code>TIMEOUT</code></td><td>超时(须进一步细分)</td><td>❌</td></tr><tr><td><code>FLAKY</code></td><td>不稳定,需复现确认</td><td>⚠️ 部分</td></tr><tr><td><code>TEST_CASE</code></td><td>用例本身写错</td><td>❌</td></tr></tbody></table></div>
<h3>两条硬规则 / Two hard rules</h3>
<p><strong>规则一:只有 <code>PRODUCT_BUG</code> 才计入"产品未通过"。</strong></p>
<p>其余全部进"阻塞分桶(blocked bucket)"。这个区分让报告从"17 个挂了 7 个"变成:</p>
<pre><code>PASS 10 · PRODUCT_BUG 0 · HARNESS 7</code></pre>
<p>一眼就知道:<strong>产品是好的,是我的 harness 有问题。</strong></p>
<p><strong>规则二:元素找不到时,先标 <code>HARNESS</code>,不要直接判产品 bug。</strong></p>
<pre><code class="language-js">// ❌ 危险的默认值
if (!(await page.$('.submit-btn'))) return fail('PRODUCT_BUG');
// ✅ 先怀疑自己,兜底跑一次
if (!(await page.$('.submit-btn'))) {
const retry = await retryOnce(page, c); // agent 兜底,恰好 1 次
if (retry.ok) return pass();
// 两次都失败,且现象像产品问题,才升级
return fail(candidateLooksLikeProductBug ? 'PRODUCT_BUG' : 'HARNESS');
}</code></pre>
<blockquote><strong>这条规则的价值观是:默认可信的是产品,不是你自己的脚本。</strong><br>Default to trusting the product, not your selector.</blockquote>
<h3>为什么这套东西值得单独拿出来讲</h3>
<p>因为<strong>它和具体工具无关</strong>。不管我用的是 Playwright、Puppeteer、Playwright MCP、还是自研的浏览器驱动,<code>failure_class</code> 这套语义都不用改。</p>
<p>它是测试报告的<strong>信任模型(trust model)</strong>,不是某个 API 的用法。</p>
<hr>
<h2>6. 可以直接抄走的自检清单</h2>
<h3>断言层</h3>
<ul class="checklist"><li class="task"><span class="box">☐</span>每条断言都问过一遍:<strong>它有可能为 false 吗?</strong>(排除恒真断言)</li><li class="task"><span class="box">☐</span><code>v-show</code> 控制的元素,读的是 <code>getComputedStyle().display</code>,不是节点存在性</li><li class="task"><span class="box">☐</span>所有 <code>querySelectorAll</code> 都限定了父级作用域</li><li class="task"><span class="box">☐</span>SPA 切页用 <code>location.hash</code>,不用 <code>goto()</code></li><li class="task"><span class="box">☐</span>断言基准用<strong>实测值</strong>(如 <code>window.innerHeight</code>),不用设计常量</li></ul>
<h3>证据层</h3>
<ul class="checklist"><li class="task"><span class="box">☐</span><code>capture()</code> <strong>永不抛错</strong>,双通道降级(PNG → PDF → 记录缺口)</li><li class="task"><span class="box">☐</span>取证失败<strong>不改写</strong> result</li><li class="task"><span class="box">☐</span>证据缺失登记在独立字段(<code>evidenceGaps</code>),与其他判定字段分离</li><li class="task"><span class="box">☐</span>用<strong>错误文案</strong>判断能力可用性,不用视口尺寸这类代理指标</li></ul>
<h3>归因层</h3>
<ul class="checklist"><li class="task"><span class="box">☐</span>每个 FAIL 都有 <code>failure_class</code>,不允许为空</li><li class="task"><span class="box">☐</span>元素找不到时默认 <code>HARNESS</code>,兜底一次后才考虑升级</li><li class="task"><span class="box">☐</span>报告区分"产品未通过"和"阻塞",不合并成一个失败数</li><li class="task"><span class="box">☐</span>归因前先拉观测数据(resource timing、DOM 快照),不靠推测</li></ul>
<h3>元层(最重要)</h3>
<ul class="checklist"><li class="task"><span class="box">☐</span>同一现象<strong>连续两次归因失败</strong>时,停下来拉数据,不要继续猜</li><li class="task"><span class="box">☐</span>设计断言前先问:<strong>这个判据受不受构建/框架/环境机制影响?</strong></li><li class="task"><span class="box">☐</span>断言打在"本次改动真正改变了什么"上,不是"我以为应该怎样"</li></ul>
<hr>
<h2>7. 一句话收尾 / One Line</h2>
<blockquote>自动化测试的可信度,不取决于你跑通了多少条,而取决于<strong>你能否说清每一条为什么挂</strong>。<br>The credibility of a test suite is not how many cases pass.<br>It's whether you can explain <strong>why each failure failed</strong>.</blockquote>
<p>我最初的问题是"怎么让 agent 跑得更多"。真正的问题其实是:</p>
<p><strong>怎么让 agent 的结论可以被相信。</strong></p>
<p>这两件事之间,隔着一整套归因机制。</p>
<hr>
<p><em>这些数字(7 条、8 条、连续 2 次)都是真实事故记录,不是构造的例子。</em> <em>写出来是因为它们比"最佳实践"更有用——最佳实践告诉你怎么做对,事故告诉你错在哪。</em></p>
</article>
</div>
</body>
</html>