settings-scope-menu.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * 设置页 Scope 菜单的 E2E 操作。
  3. *
  4. * 第一层只按设备列出「用户」和「项目」入口,项目在各设备的「项目」子菜单里
  5. * (docs/issues/data-sync/19-scope-menu-project-submenu.md)。用例按稳定的 scope key
  6. * 选择选项,不按显示文案或位置推断归属。
  7. */
  8. const PROJECTS_TRIGGER = '[data-scope-projects-trigger="true"]';
  9. /**
  10. * 在已打开的 Scope 菜单里点选 key。第一层没有该选项时,每轮打开下一个设备的「项目」
  11. * 子菜单再找;子菜单内容在打开后的下一帧才渲染,所以按轮询推进。
  12. */
  13. export async function clickOpenScopeMenuOption(
  14. key: string,
  15. options: { timeout?: number; timeoutMsg?: string } = {},
  16. ): Promise<void> {
  17. let attempt = 0;
  18. await browser.waitUntil(
  19. () =>
  20. browser.execute(
  21. (expected, index, projectsTrigger) => {
  22. const option = Array.from(
  23. document.querySelectorAll<HTMLElement>('[role="menuitemradio"]'),
  24. ).find((item) => item.dataset.pluginScopeKey === expected);
  25. if (option) {
  26. option.click();
  27. return true;
  28. }
  29. const triggers = Array.from(document.querySelectorAll<HTMLElement>(projectsTrigger));
  30. triggers[index % Math.max(triggers.length, 1)]?.click();
  31. return false;
  32. },
  33. key,
  34. attempt++,
  35. PROJECTS_TRIGGER,
  36. ),
  37. {
  38. timeout: options.timeout ?? 15_000,
  39. timeoutMsg: options.timeoutMsg ?? `Scope 菜单没有出现 ${key}`,
  40. },
  41. );
  42. }
  43. /**
  44. * 在已打开的菜单里打开某台设备的「项目」子菜单(本机为 "local",远端为设备 id)。
  45. * 子菜单失去焦点就会关闭,同一时刻只有一个设备的项目可见。
  46. */
  47. export async function openScopeProjectSubmenu(device: string): Promise<void> {
  48. await browser.waitUntil(
  49. () =>
  50. browser.execute(
  51. (projectsTrigger, expectedDevice) => {
  52. const trigger = Array.from(document.querySelectorAll<HTMLElement>(projectsTrigger)).find(
  53. (item) => item.dataset.scopeDevice === expectedDevice,
  54. );
  55. if (!trigger) return false;
  56. if (trigger.dataset.state !== "open") trigger.click();
  57. return trigger.dataset.state === "open";
  58. },
  59. PROJECTS_TRIGGER,
  60. device,
  61. ),
  62. { timeout: 15_000, timeoutMsg: `Scope 菜单没有打开「${device}」的项目子菜单` },
  63. );
  64. }
  65. /** 读取已打开菜单的全部选项:第一层的「用户」加上每台设备「项目」子菜单里的项目。 */
  66. export async function readOpenScopeMenuOptions(): Promise<Array<{ key: string; text: string }>> {
  67. const collect = () =>
  68. browser.execute(() =>
  69. Array.from(document.querySelectorAll<HTMLElement>('[role="menuitemradio"]')).map((item) => ({
  70. key: item.dataset.pluginScopeKey ?? "",
  71. text: item.innerText.trim(),
  72. })),
  73. );
  74. const options = new Map<string, { key: string; text: string }>();
  75. for (const option of await collect()) options.set(option.key, option);
  76. const devices = await browser.execute(
  77. (projectsTrigger) =>
  78. Array.from(document.querySelectorAll<HTMLElement>(projectsTrigger)).map(
  79. (trigger) => trigger.dataset.scopeDevice ?? "",
  80. ),
  81. PROJECTS_TRIGGER,
  82. );
  83. for (const device of devices) {
  84. await openScopeProjectSubmenu(device);
  85. for (const option of await collect()) options.set(option.key, option);
  86. }
  87. return [...options.values()];
  88. }