2023-10-13 16:32:10 +02:00
|
|
|
|
|
|
|
import type { ElectronApplication } from '@playwright/test';
|
|
|
|
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.
|
|
|
|
const activateMainMenuItem = (
|
|
|
|
electronApp: ElectronApplication,
|
|
|
|
targetItemLabel: string,
|
|
|
|
parentMenuLabel?: string,
|
|
|
|
) => {
|
|
|
|
return electronApp.evaluate(async ({ Menu }, [targetItemLabel, parentMenuLabel]) => {
|
|
|
|
const activateItemInSubmenu = (submenu: MenuItem[], parentLabel: string) => {
|
2023-10-13 16:32:10 +02:00
|
|
|
for (const item of submenu) {
|
2024-04-25 16:31:18 +02:00
|
|
|
const matchesParent = !parentMenuLabel || parentLabel === parentMenuLabel;
|
|
|
|
if (item.label === targetItemLabel && matchesParent && item.visible) {
|
2023-10-13 16:32:10 +02:00
|
|
|
// Found!
|
|
|
|
item.click();
|
|
|
|
return true;
|
|
|
|
} else if (item.submenu) {
|
2024-04-25 16:31:18 +02:00
|
|
|
const foundItem = activateItemInSubmenu(item.submenu.items, item.label);
|
2023-10-13 16:32:10 +02:00
|
|
|
|
|
|
|
if (foundItem) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No item found
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const appMenu = Menu.getApplicationMenu();
|
2024-04-25 16:31:18 +02:00
|
|
|
return activateItemInSubmenu(appMenu.items, '');
|
|
|
|
}, [targetItemLabel, parentMenuLabel]);
|
2023-10-13 16:32:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default activateMainMenuItem;
|