audit-mobile-secondary-continue.mjs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import fs from 'node:fs';
  2. import { connectAudit, readAudit, sleep, auditCheck } from './lib/mobile-full-audit.mjs';
  3. const a = await connectAudit();
  4. async function buttonByText(id, text, scope = 'button', occurrence = 0) {
  5. const selector = await a.cdp.evaluate(({ text, scope, occurrence }) => {
  6. document.querySelectorAll('[data-secondary-audit-target]').forEach((e) => e.removeAttribute('data-secondary-audit-target'));
  7. const matches = [...document.querySelectorAll(scope)].filter((e) => {
  8. const r = e.getBoundingClientRect();
  9. return r.width > 0 && r.height > 0 && (e.textContent || '').replace(/\s+/g, ' ').trim().includes(text);
  10. });
  11. if (!matches[occurrence]) throw Error(`目标不唯一或不可见:${text} / ${matches.length}`);
  12. matches[occurrence].scrollIntoView({ block: 'center' });
  13. const r = matches[occurrence].getBoundingClientRect();
  14. const hit = document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2);
  15. if (hit !== matches[occurrence] && !matches[occurrence].contains(hit)) throw Error(`目标被其他界面遮挡:${text}`);
  16. matches[occurrence].setAttribute('data-secondary-audit-target', 'current');
  17. return '[data-secondary-audit-target="current"]';
  18. }, { text, scope, occurrence });
  19. return a.tap(id, selector, text);
  20. }
  21. async function settled(id, expectedPath, wait = 1000) {
  22. await a.cdp.waitFor((path) => location.pathname === path, expectedPath, 15000);
  23. await sleep(wait);
  24. const shot = await a.snapshot(id);
  25. const probe = await a.cdp.evaluate(() => ({
  26. path: location.pathname,
  27. heading: document.querySelector('.m-title-main')?.textContent.trim() || document.querySelector('h1')?.textContent.trim() || '',
  28. text: document.body.innerText.slice(0, 5000),
  29. controls: [...document.querySelectorAll('button,a,input,textarea,[role="button"]')].filter((e) => { const r = e.getBoundingClientRect(); return r.width > 0 && r.height > 0 && r.x >= 0 && r.right <= innerWidth + 1 && r.y >= 0 && r.bottom <= innerHeight + 1; }).map((e) => ({ tag: e.tagName, text: e.tagName === 'INPUT' ? '' : e.textContent.trim().replace(/\s+/g, ' ').slice(0, 120), label: e.getAttribute('aria-label'), href: e.getAttribute('href'), class: String(e.className).slice(0, 100), disabled: !!e.disabled }))
  30. }));
  31. fs.writeFileSync(`test-results/mobile-full-audit-20261130/${id}-probe.json`, JSON.stringify(probe, null, 2) + '\n', { flag: 'w' });
  32. auditCheck(`${expectedPath}页面到达`, probe.path === expectedPath, { screenshot: shot.id, heading: probe.heading });
  33. return probe;
  34. }
  35. try {
  36. const current = await a.cdp.evaluate(() => location.pathname);
  37. if (current !== '/mobile/my-containers') throw Error(`当前页面不是待续的我的容器:${current}`);
  38. const containers = readAudit().screenshots.find((x) => x.id === 'secondary-my-containers-settled');
  39. if (!containers) await settled('secondary-my-containers-settled', '/mobile/my-containers', 1200);
  40. await buttonByText('secondary-container-access-open', '查看接入信息', 'button.container-btn', 0);
  41. await settled('secondary-plan-access-settled', '/mobile/plan-access', 1200);
  42. await a.back('secondary-plan-access-back');
  43. await a.cdp.waitFor((path) => location.pathname === path, '/mobile/my-containers', 10000);
  44. await buttonByText('secondary-plans-open-from-containers', '查看套餐与开通', '.quota-btn');
  45. await settled('secondary-plans-settled', '/mobile/plans', 1200);
  46. await a.back('secondary-plans-back');
  47. await a.cdp.waitFor((path) => location.pathname === path, '/mobile/my-containers', 10000);
  48. await a.back('secondary-containers-back');
  49. await a.cdp.waitFor((path) => location.pathname === path, '/mobile/mine', 10000);
  50. await buttonByText('secondary-about-open', '关于与版本', '.m-row');
  51. await sleep(1800);
  52. await a.snapshot('secondary-about-after');
  53. const about = await a.cdp.evaluate(() => ({ path: location.pathname, text: document.body.innerText.slice(0, 3000), dialogs: [...document.querySelectorAll('[role="dialog"]')].map((e) => e.innerText.slice(0, 1000)) }));
  54. fs.writeFileSync('test-results/mobile-full-audit-20261130/secondary-about-probe.json', JSON.stringify(about, null, 2) + '\n', { flag: 'w' });
  55. auditCheck('关于与版本点击后仍在设置页或显示更新反馈', about.path === '/mobile/mine', { screenshot: 'secondary-about-after', hasVersion: about.text.includes('v2.0.0') || about.text.includes('关于与版本') });
  56. await a.checkpoint('已真机只读审计我的容器、首个容器接入信息、套餐页与关于版本;未申请、未充值、未退出、未创建。证据与动作账本位于test-results/mobile-full-audit-20261130');
  57. console.log(JSON.stringify({ stage: 'secondary_account_pages_complete', screenshots: readAudit().screenshots.length, actions: Object.keys(readAudit().actions).length }));
  58. } catch (error) {
  59. console.error(error);
  60. process.exitCode = 1;
  61. } finally {
  62. a.close();
  63. }