1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

Chore: Tests: Improve Playwright test reliability (#10981)

This commit is contained in:
Henry Heino
2024-09-04 04:14:12 -07:00
committed by GitHub
parent 0b13dbddd8
commit e41394b57f
10 changed files with 66 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
import type { ElectronApplication } from '@playwright/test';
import { expect, type ElectronApplication } from '@playwright/test';
import type { MenuItem } from 'electron';
@@ -7,35 +7,45 @@ import type { MenuItem } from 'electron';
// https://github.com/spaceagetv/electron-playwright-helpers/blob/main/src/menu_helpers.ts
// If given, `parentMenuLabel` should be the label of the menu containing the target item.
const activateMainMenuItem = (
const activateMainMenuItem = async (
electronApp: ElectronApplication,
targetItemLabel: string,
targetItemLabel: string|RegExp,
parentMenuLabel?: string,
) => {
return electronApp.evaluate(async ({ Menu }, [targetItemLabel, parentMenuLabel]) => {
const activateItemInSubmenu = (submenu: MenuItem[], parentLabel: string) => {
for (const item of submenu) {
const matchesParent = !parentMenuLabel || parentLabel === parentMenuLabel;
if (item.label === targetItemLabel && matchesParent && item.visible) {
// Found!
item.click();
return true;
} else if (item.submenu) {
const foundItem = activateItemInSubmenu(item.submenu.items, item.label);
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 (foundItem) {
if (matchesLabel && matchesParent && item.visible) {
// Found!
item.click();
return true;
} else if (item.submenu) {
const foundItem = activateItemInSubmenu(item.submenu.items, item.label);
if (foundItem) {
return true;
}
}
}
}
// No item found
return false;
};
// No item found
return false;
};
const appMenu = Menu.getApplicationMenu();
return activateItemInSubmenu(appMenu.items, '');
}, [targetItemLabel, parentMenuLabel]);
const appMenu = Menu.getApplicationMenu();
return activateItemInSubmenu(appMenu.items, '');
}, [targetItemLabel, parentMenuLabel]);
}, {
message: `should find and activate menu item with label ${JSON.stringify(targetItemLabel)}`,
}).toBe(true);
};
export default activateMainMenuItem;