2023-10-13 16:32:10 +02:00
|
|
|
|
2024-09-04 13:14:12 +02:00
|
|
|
import { expect, type ElectronApplication } from '@playwright/test';
|
2023-10-13 16:32:10 +02:00
|
|
|
import type { MenuItem } from 'electron';
|
|
|
|
|
|
|
|
|
|
|
|
// Roughly based on
|
|
|
|
// https://github.com/spaceagetv/electron-playwright-helpers/blob/main/src/menu_helpers.ts
|
|
|
|
|
2024-04-25 16:31:18 +02:00
|
|
|
// If given, `parentMenuLabel` should be the label of the menu containing the target item.
|
2024-09-04 13:14:12 +02:00
|
|
|
const activateMainMenuItem = async (
|
2024-04-25 16:31:18 +02:00
|
|
|
electronApp: ElectronApplication,
|
2024-09-04 13:14:12 +02:00
|
|
|
targetItemLabel: string|RegExp,
|
2024-04-25 16:31:18 +02:00
|
|
|
parentMenuLabel?: string,
|
|
|
|
) => {
|
2024-09-04 13:14:12 +02:00
|
|
|
await expect.poll(() => {
|
|
|
|
return electronApp.evaluate(async ({ Menu }, [targetItemLabel, parentMenuLabel]) => {
|
|
|
|
const activateItemInSubmenu = (submenu: MenuItem[], parentLabel: string) => {
|
|
|
|
for (const item of submenu) {
|
|
|
|
const matchesParent = !parentMenuLabel || parentLabel === parentMenuLabel;
|
|
|
|
const matchesLabel = typeof targetItemLabel === 'string' ? (
|
|
|
|
targetItemLabel === item.label
|
|
|
|
) : (
|
|
|
|
item.label.match(targetItemLabel)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (matchesLabel && matchesParent && item.visible) {
|
|
|
|
// Found!
|
|
|
|
item.click();
|
2023-10-13 16:32:10 +02:00
|
|
|
return true;
|
2024-09-04 13:14:12 +02:00
|
|
|
} else if (item.submenu) {
|
|
|
|
const foundItem = activateItemInSubmenu(item.submenu.items, item.label);
|
|
|
|
|
|
|
|
if (foundItem) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-10-13 16:32:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 13:14:12 +02:00
|
|
|
// No item found
|
|
|
|
return false;
|
|
|
|
};
|
2023-10-13 16:32:10 +02:00
|
|
|
|
2024-09-04 13:14:12 +02:00
|
|
|
const appMenu = Menu.getApplicationMenu();
|
2024-11-08 17:32:05 +02:00
|
|
|
return !!appMenu && activateItemInSubmenu(appMenu.items, '');
|
2024-09-04 13:14:12 +02:00
|
|
|
}, [targetItemLabel, parentMenuLabel]);
|
|
|
|
}, {
|
|
|
|
message: `should find and activate menu item with label ${JSON.stringify(targetItemLabel)}`,
|
|
|
|
}).toBe(true);
|
2023-10-13 16:32:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default activateMainMenuItem;
|