三件事同批,共同点是「门禁/断言与实现脱钩,而脱钩处恰好没人跑」。
1. GA4 consent 断言(新发现,上一轮遗留)
DEF-09 把 analytics_storage 默认从 granted 改成 denied(opt-in),
实现与单测都改了,e2e/ga4-event-tracking.spec.ts:132 仍断言 granted。
它能藏住是因为那 4 个 @critical 用例**只在 test:e2e:prod 下真正执行**
(dev 下 NEXT_PUBLIC_GA_MEASUREMENT_ID 不存在 ⇒ test.skip)⇒ 本地全绿看不见。
改为同时钉两件事:default 必须是 denied;已存偏好由内联片段在 config
之前以 consent update 放行。只改前者会让「偏好回放坏了」变成新盲区。
判据:E2E_TARGET=production 跑 ga4 spec ⇒ 4 passed。
2. UJ-10 假红(TA-11)
原断言「分类后恰 1 篇 / 全部恰 2 篇」,并行写库用例插入的 E2E 新闻会让它假红。
改为钉两篇种子 slug 的可见/缺席 —— 比数条数更具体:数条数只证明「有 1 条」,
钉 slug 才证明「筛掉的是另一类」。桌面 + 移动两份同步改。
3. 覆盖分母 + 取样稳定性
- headings PAGES 11 → 34 页(与 check:axe:routes 的 35 条对齐,去掉按 404
契约的 /_not-found)。实测 34/34 通过、0 问题。此前只扫 11 页,
新增路由失踪时门禁仍然绿 —— 「分母静默变小」的第三种形态。
- axe settle() 改逐屏 max(400, 0.8×viewport) 步进 + 双 rAF,回顶后等
main * 内 opacity:0 元素清零(8s 超时抛出,不吞)。原「一次跳到底」
会跳过 IntersectionObserver 目标,半透明文字被读成低对比度,失败集合漂移。
- website-acceptance 的 pollBoundingBox 由「非空即返回」改为连续 5 次取样
一致再返回(TA-12):页面入场 transform 未落定时会抓到 29.936px 中途值。
超时仍 throw,判据强度不降。
判据:type-check / type-check:e2e EXIT=0;test:e2e:prod 135 passed / 0 failed
/ 9 skipped / EXIT=0;check:axe 35 路由 × 4 组合 140 rows 全 0、规则级 105/105。
268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
import { test, expect, type Page } from './fixtures';
|
||
|
||
/**
|
||
* GA4 事件追踪验证
|
||
*
|
||
* 验收 A-6 重写:原实现先注入 mock window.gtag,再由测试自己调用 gtag 并断言
|
||
* 刚写入的 __gtagCalls —— 断言的是 fixture,不是应用。且 .env.production 配置了
|
||
* 真实 MEASUREMENT_ID 后,应用注入的 gtag.js 会覆盖 mock,导致全部用例失败。
|
||
*
|
||
* 现在只拦截 Google 的远端脚本(CI 不出网),gtag 函数与 dataLayer 队列由应用
|
||
* 自己的内联片段创建(src/components/analytics/GoogleAnalytics.tsx),
|
||
* 因此 window.dataLayer 中的每一条调用都必然由应用发出。
|
||
*
|
||
* 测试范围:
|
||
* 1. TC-GA4-001: 首屏内联片段发出 js / consent default / 唯一一次 config
|
||
* 2. TC-GA4-002: 联系表单提交触发 form_submit + 单次 conversion
|
||
* 3. TC-GA4-003: 点击「接受所有」触发 button_click + consent update
|
||
* 4. TC-GA4-004: 客户端路由跳转触发带 page_path 的 config
|
||
*/
|
||
|
||
test.setTimeout(60000);
|
||
|
||
type GtagCall = unknown[];
|
||
type GtagParams = Record<string, unknown>;
|
||
|
||
/** 内联片段把参数按 arguments 对象压入队列,这里统一展开成数组便于断言。 */
|
||
async function gtagCalls(page: Page): Promise<GtagCall[]> {
|
||
return page.evaluate(() => {
|
||
const queue = (window as unknown as { dataLayer?: ArrayLike<unknown>[] }).dataLayer;
|
||
if (!queue) {return [];}
|
||
return Array.from(queue).map((entry) => Array.from(entry as ArrayLike<unknown>));
|
||
});
|
||
}
|
||
|
||
function callsWith(calls: GtagCall[], ...prefix: unknown[]): GtagCall[] {
|
||
return calls.filter((call) => prefix.every((value, index) => call[index] === value));
|
||
}
|
||
|
||
function params(call: GtagCall | undefined): GtagParams {
|
||
return (call?.[2] ?? {}) as GtagParams;
|
||
}
|
||
|
||
/** 拦截 Google 远端脚本,返回应用请求携带的 MEASUREMENT_ID;未配置 GA 时返回 null。 */
|
||
async function gotoWithGtag(page: Page, path: string): Promise<string | null> {
|
||
await page.route(/https?:\/\/www\.googletagmanager\.com\//, (route) =>
|
||
route.fulfill({
|
||
status: 200,
|
||
contentType: 'application/javascript',
|
||
body: '/* GA4 E2E stub: 保留应用自身的 dataLayer 队列 */',
|
||
})
|
||
);
|
||
|
||
const gtagRequest = page
|
||
.waitForRequest(/www\.googletagmanager\.com\/gtag\/js/, { timeout: 15000 })
|
||
.catch(() => null);
|
||
|
||
await page.goto(path, { timeout: 30000, waitUntil: 'domcontentloaded' });
|
||
|
||
const request = await gtagRequest;
|
||
if (!request) {return null;}
|
||
return new URL(request.url()).searchParams.get('id');
|
||
}
|
||
|
||
/** 预置同意偏好:抑制横幅遮挡,同时让应用发出 consent update。 */
|
||
async function seedConsent(page: Page): Promise<void> {
|
||
await page.addInitScript(() => {
|
||
window.localStorage.setItem(
|
||
'novalon-cookie-preferences',
|
||
JSON.stringify({
|
||
necessary: true,
|
||
analytics: true,
|
||
marketing: false,
|
||
functionality: true,
|
||
timestamp: Date.now(),
|
||
})
|
||
);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* e2e/playwright.config.ts:29 的 storageState 全站预置了同意偏好(analytics:false),
|
||
* 因此横幅默认不出现;需要真实横幅的用例必须先清除该键。
|
||
*/
|
||
async function clearSeededConsent(page: Page): Promise<void> {
|
||
await page.addInitScript(() => {
|
||
window.localStorage.removeItem('novalon-cookie-preferences');
|
||
});
|
||
}
|
||
|
||
async function expectGtagReady(page: Page): Promise<void> {
|
||
await expect
|
||
.poll(async () => (await gtagCalls(page)).length, { timeout: 15000 })
|
||
.toBeGreaterThan(0);
|
||
}
|
||
|
||
/** 等应用内联片段把 js / consent default / config 三条都写入队列后再断言,避免读到半成品。 */
|
||
async function expectSnippetCalls(page: Page): Promise<GtagCall[]> {
|
||
await expect
|
||
.poll(async () => {
|
||
const calls = await gtagCalls(page);
|
||
const present = [
|
||
callsWith(calls, 'js').length,
|
||
callsWith(calls, 'consent', 'default').length,
|
||
callsWith(calls, 'config').length,
|
||
];
|
||
return present.filter((count) => count > 0).length;
|
||
}, { timeout: 15000 })
|
||
.toBe(3);
|
||
|
||
return gtagCalls(page);
|
||
}
|
||
|
||
/** GoogleAnalytics 只在客户端路由变化时补发带 page_path 的 config。 */
|
||
async function routedPageViews(page: Page, measurementId: string | null): Promise<GtagCall[]> {
|
||
return callsWith(await gtagCalls(page), 'config', measurementId ?? '');
|
||
}
|
||
|
||
test.describe('GA4 事件追踪验证', { tag: ['@analytics', '@regression', '@critical'] }, () => {
|
||
test.describe.configure({ mode: 'parallel' });
|
||
|
||
test('TC-GA4-001: 页面浏览追踪 - 首屏由应用发出唯一一次 config', async ({ page }) => {
|
||
await seedConsent(page);
|
||
const measurementId = await gotoWithGtag(page, '/');
|
||
test.skip(measurementId === null, '构建未注入 GA 脚本(NEXT_PUBLIC_GA_MEASUREMENT_ID 为空)');
|
||
|
||
const calls = await expectSnippetCalls(page);
|
||
|
||
expect(callsWith(calls, 'js')).toHaveLength(1);
|
||
|
||
const consentDefault = callsWith(calls, 'consent', 'default');
|
||
expect(consentDefault).toHaveLength(1);
|
||
// DEF-09(2026-11-25 落地):分析 Cookie 默认改为 opt-in,default 必须是 denied。
|
||
// 已存偏好(seedConsent 写 analytics:true)由内联片段在 config 之前以 consent update 放行,
|
||
// 因此这里同时钉住「默认拒绝」与「偏好回放」两件事,缺任一件都是采集口径错了。
|
||
expect(params(consentDefault[0]).analytics_storage).toBe('denied');
|
||
expect(params(consentDefault[0]).ad_storage).toBe('denied');
|
||
|
||
const consentUpdates = callsWith(calls, 'consent', 'update');
|
||
expect(consentUpdates.length).toBeGreaterThanOrEqual(1);
|
||
expect(params(consentUpdates[consentUpdates.length - 1]).analytics_storage).toBe('granted');
|
||
|
||
const configs = callsWith(calls, 'config');
|
||
expect(configs).toHaveLength(1);
|
||
expect(configs[0]?.[1]).toBe(measurementId);
|
||
expect(params(configs[0]).anonymize_ip).toBe(true);
|
||
});
|
||
|
||
test('TC-GA4-002: 联系表单提交触发 form_submit 和 conversion 事件', async ({ page }) => {
|
||
await seedConsent(page);
|
||
await page.route('**/api/contact', (route) =>
|
||
route.fulfill({
|
||
status: 200,
|
||
contentType: 'application/json',
|
||
body: JSON.stringify({ success: 'true' }),
|
||
})
|
||
);
|
||
|
||
const measurementId = await gotoWithGtag(page, '/contact');
|
||
test.skip(measurementId === null, '构建未注入 GA 脚本(NEXT_PUBLIC_GA_MEASUREMENT_ID 为空)');
|
||
await expectGtagReady(page);
|
||
|
||
await page.locator('[data-testid="name-input"]').first().fill('测试用户');
|
||
await page.locator('[data-testid="phone-input"]').first().fill('13800138000');
|
||
await page.locator('[data-testid="email-input"]').first().fill('test@example.com');
|
||
await page.locator('[data-testid="subject-input"]').first().fill('咨询产品方案');
|
||
await page
|
||
.locator('[data-testid="message-input"]')
|
||
.first()
|
||
.fill('您好,我想了解贵公司的ERP产品和CRM产品,请提供详细方案和报价。');
|
||
|
||
const submitButton = page.locator('[data-testid="submit-button"]').first();
|
||
await expect(submitButton).toBeEnabled({ timeout: 10000 });
|
||
await submitButton.click();
|
||
|
||
await expect
|
||
.poll(
|
||
async () => {
|
||
const calls = await gtagCalls(page);
|
||
return (
|
||
callsWith(calls, 'event', 'form_submit').length +
|
||
callsWith(calls, 'event', 'conversion').length
|
||
);
|
||
},
|
||
{ timeout: 15000 }
|
||
)
|
||
.toBeGreaterThanOrEqual(2);
|
||
|
||
const calls = await gtagCalls(page);
|
||
const formSubmit = callsWith(calls, 'event', 'form_submit');
|
||
expect(formSubmit).toHaveLength(1);
|
||
expect(params(formSubmit[0]).event_category).toBe('contact');
|
||
expect(params(formSubmit[0]).event_label).toBe('咨询产品方案');
|
||
expect(params(formSubmit[0]).event_value).toBe(1);
|
||
expect(params(formSubmit[0]).send_to).toBe(measurementId);
|
||
|
||
// contact-content-v3 只调用 trackContactForm(内部已发 conversion),
|
||
// 计数为 1 是「一次提交 = 一次转化」的回归护栏。
|
||
const conversions = callsWith(calls, 'event', 'conversion');
|
||
expect(conversions).toHaveLength(1);
|
||
expect(params(conversions[0]).conversion_label).toBe('contact_form_submit');
|
||
expect(params(conversions[0]).value).toBe(1);
|
||
expect(params(conversions[0]).currency).toBe('CNY');
|
||
expect(typeof params(conversions[0]).transaction_id).toBe('string');
|
||
});
|
||
|
||
test('TC-GA4-003: 同意横幅「接受所有」点击触发 button_click 事件', async ({ page }) => {
|
||
await clearSeededConsent(page);
|
||
const measurementId = await gotoWithGtag(page, '/');
|
||
test.skip(measurementId === null, '构建未注入 GA 脚本(NEXT_PUBLIC_GA_MEASUREMENT_ID 为空)');
|
||
|
||
const acceptAll = page.locator('button', { hasText: '接受所有' });
|
||
await expect(acceptAll).toBeVisible({ timeout: 15000 });
|
||
await acceptAll.click();
|
||
|
||
await expect
|
||
.poll(
|
||
async () => callsWith(await gtagCalls(page), 'event', 'button_click').length,
|
||
{ timeout: 10000 }
|
||
)
|
||
.toBe(2);
|
||
|
||
const calls = await gtagCalls(page);
|
||
const buttonClick = callsWith(calls, 'event', 'button_click');
|
||
// handleAcceptAll 先复用 handleSavePreferences(save_cookie_preferences),再补发自身事件。
|
||
expect(buttonClick.map((call) => params(call).event_label)).toEqual([
|
||
'save_cookie_preferences',
|
||
'accept_all_cookies',
|
||
]);
|
||
for (const call of buttonClick) {
|
||
expect(params(call).event_category).toBe('engagement');
|
||
expect(params(call).send_to).toBe(measurementId);
|
||
}
|
||
|
||
const consentUpdate = callsWith(calls, 'consent', 'update');
|
||
expect(consentUpdate.length).toBeGreaterThan(0);
|
||
expect(params(consentUpdate[consentUpdate.length - 1]).analytics_storage).toBe('granted');
|
||
|
||
await expect(acceptAll).toBeHidden({ timeout: 10000 });
|
||
});
|
||
|
||
test('TC-GA4-004: 页面浏览追踪 - 客户端路由跳转触发带 page_path 的 config', async ({ page }) => {
|
||
await seedConsent(page);
|
||
const measurementId = await gotoWithGtag(page, '/');
|
||
test.skip(measurementId === null, '构建未注入 GA 脚本(NEXT_PUBLIC_GA_MEASUREMENT_ID 为空)');
|
||
await expectGtagReady(page);
|
||
|
||
const privacyLink = page.locator('footer a[href="/privacy"]').first();
|
||
await expect(privacyLink).toBeVisible({ timeout: 15000 });
|
||
await privacyLink.click();
|
||
await expect(page).toHaveURL(/\/privacy$/);
|
||
|
||
await expect
|
||
.poll(async () => {
|
||
const views = await routedPageViews(page, measurementId);
|
||
return views.filter((call) => params(call).page_path === '/privacy').length;
|
||
}, { timeout: 15000 })
|
||
.toBe(1);
|
||
|
||
const routeConfig = (await routedPageViews(page, measurementId)).filter(
|
||
(call) => params(call).page_path === '/privacy'
|
||
);
|
||
// A-17:SPA pageview 必须在下一帧读取标题,否则 page_title 会是空串
|
||
// (由 GoogleAnalytics.test.tsx「read document.title after metadata paints」钉住)。
|
||
expect(String(params(routeConfig[0]).page_title)).toContain('隐私政策');
|
||
expect(params(routeConfig[0]).page_location).toContain('/privacy');
|
||
});
|
||
});
|