2023-10-13 07:32:10 -07:00
|
|
|
import { resolve, join, dirname } from 'path';
|
2025-03-25 13:07:27 -07:00
|
|
|
import { remove, mkdirp, readFile, pathExists } from 'fs-extra';
|
|
|
|
|
import { _electron as electron, Page, ElectronApplication, test as base, TestInfo } from '@playwright/test';
|
2023-10-13 07:32:10 -07:00
|
|
|
import uuid from '@joplin/lib/uuid';
|
2023-11-12 07:06:32 -08:00
|
|
|
import createStartupArgs from './createStartupArgs';
|
|
|
|
|
import firstNonDevToolsWindow from './firstNonDevToolsWindow';
|
2024-11-09 04:50:06 -08:00
|
|
|
import setDarkMode from './setDarkMode';
|
2023-10-13 07:32:10 -07:00
|
|
|
|
|
|
|
|
|
2024-09-26 03:35:32 -07:00
|
|
|
type StartWithPluginsResult = { app: ElectronApplication; mainWindow: Page };
|
2023-10-13 07:32:10 -07:00
|
|
|
|
|
|
|
|
type JoplinFixtures = {
|
2023-10-31 08:05:28 -07:00
|
|
|
profileDirectory: string;
|
2023-10-13 07:32:10 -07:00
|
|
|
electronApp: ElectronApplication;
|
2024-09-26 03:35:32 -07:00
|
|
|
startAppWithPlugins: (pluginPaths: string[])=> Promise<StartWithPluginsResult>;
|
2023-12-11 05:58:45 -08:00
|
|
|
startupPluginsLoaded: Promise<void>;
|
2023-10-13 07:32:10 -07:00
|
|
|
mainWindow: Page;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A custom fixture that loads an electron app. See
|
|
|
|
|
// https://playwright.dev/docs/test-fixtures
|
|
|
|
|
|
2025-03-25 13:07:27 -07:00
|
|
|
const initializeMainWindow = async (electronApp: ElectronApplication) => {
|
2024-09-26 03:35:32 -07:00
|
|
|
const mainWindow = await firstNonDevToolsWindow(electronApp);
|
|
|
|
|
|
|
|
|
|
// Setting the viewport size helps keep test environments consistent.
|
|
|
|
|
await mainWindow.setViewportSize({
|
2025-02-27 12:32:47 -06:00
|
|
|
width: 1300,
|
2024-09-26 03:35:32 -07:00
|
|
|
height: 800,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return mainWindow;
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-20 03:35:22 -08:00
|
|
|
const waitForMainMessage = (electronApp: ElectronApplication, messageId: string) => {
|
|
|
|
|
return electronApp.evaluate(({ ipcMain }, messageId) => {
|
2024-10-26 12:58:54 -07:00
|
|
|
return new Promise<void>(resolve => {
|
2024-11-20 03:35:22 -08:00
|
|
|
ipcMain.once(messageId, () => resolve());
|
2024-10-26 12:58:54 -07:00
|
|
|
});
|
2024-11-20 03:35:22 -08:00
|
|
|
}, messageId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const waitForAppLoaded = async (electronApp: ElectronApplication) => {
|
|
|
|
|
await waitForMainMessage(electronApp, 'startup-finished');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const waitForStartupPlugins = async (electronApp: ElectronApplication) => {
|
|
|
|
|
await waitForMainMessage(electronApp, 'startup-plugins-loaded');
|
2024-10-26 12:58:54 -07:00
|
|
|
};
|
|
|
|
|
|
2025-03-25 13:07:27 -07:00
|
|
|
const attachJoplinLog = async (profileDirectory: string, testInfo: TestInfo) => {
|
|
|
|
|
const logFile = join(profileDirectory, 'log.txt');
|
|
|
|
|
if (await pathExists(logFile)) {
|
|
|
|
|
await testInfo.attach('log.txt', {
|
|
|
|
|
body: await readFile(logFile, 'utf8'),
|
|
|
|
|
contentType: 'text/plain',
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
console.warn('Missing log file');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-26 03:35:32 -07:00
|
|
|
const testDir = dirname(__dirname);
|
|
|
|
|
|
2023-10-13 07:32:10 -07:00
|
|
|
export const test = base.extend<JoplinFixtures>({
|
|
|
|
|
// Playwright fails if we don't use the object destructuring
|
|
|
|
|
// pattern in the first argument.
|
|
|
|
|
//
|
|
|
|
|
// See https://github.com/microsoft/playwright/issues/8798
|
|
|
|
|
//
|
|
|
|
|
// eslint-disable-next-line no-empty-pattern
|
2023-10-31 08:05:28 -07:00
|
|
|
profileDirectory: async ({ }, use) => {
|
2024-09-26 03:35:32 -07:00
|
|
|
const profilePath = resolve(join(testDir, 'test-profile'));
|
2023-10-13 07:32:10 -07:00
|
|
|
const profileSubdir = join(profilePath, uuid.createNano());
|
|
|
|
|
await mkdirp(profileSubdir);
|
|
|
|
|
|
2023-10-31 08:05:28 -07:00
|
|
|
await use(profileSubdir);
|
|
|
|
|
|
|
|
|
|
await remove(profileSubdir);
|
|
|
|
|
},
|
|
|
|
|
|
2025-03-25 13:07:27 -07:00
|
|
|
electronApp: async ({ profileDirectory }, use, testInfo) => {
|
2023-11-12 07:06:32 -08:00
|
|
|
const startupArgs = createStartupArgs(profileDirectory);
|
2023-10-13 07:32:10 -07:00
|
|
|
const electronApp = await electron.launch({ args: startupArgs });
|
2024-11-20 03:35:22 -08:00
|
|
|
const startupPromise = waitForAppLoaded(electronApp);
|
2024-11-09 04:50:06 -08:00
|
|
|
await setDarkMode(electronApp, false);
|
2024-11-20 03:35:22 -08:00
|
|
|
await startupPromise;
|
2023-10-13 07:32:10 -07:00
|
|
|
|
|
|
|
|
await use(electronApp);
|
|
|
|
|
|
2025-03-25 13:07:27 -07:00
|
|
|
// For debugging purposes, attach the Joplin log file to the test:
|
|
|
|
|
await attachJoplinLog(profileDirectory, testInfo);
|
|
|
|
|
|
2023-10-13 07:32:10 -07:00
|
|
|
await electronApp.firstWindow();
|
|
|
|
|
await electronApp.close();
|
|
|
|
|
},
|
|
|
|
|
|
2024-09-26 03:35:32 -07:00
|
|
|
startAppWithPlugins: async ({ profileDirectory }, use) => {
|
|
|
|
|
const startupArgs = createStartupArgs(profileDirectory);
|
|
|
|
|
let electronApp: ElectronApplication;
|
|
|
|
|
|
|
|
|
|
await use(async (pluginPaths: string[]) => {
|
|
|
|
|
if (electronApp) {
|
|
|
|
|
throw new Error('Electron app already created');
|
|
|
|
|
}
|
|
|
|
|
electronApp = await electron.launch({
|
|
|
|
|
args: [
|
|
|
|
|
...startupArgs,
|
|
|
|
|
'--dev-plugins',
|
|
|
|
|
pluginPaths.map(path => resolve(testDir, path)).join(','),
|
|
|
|
|
],
|
|
|
|
|
});
|
2024-11-20 03:35:22 -08:00
|
|
|
const startupPromise = waitForAppLoaded(electronApp);
|
2025-03-25 13:07:27 -07:00
|
|
|
const mainWindowPromise = initializeMainWindow(electronApp);
|
2024-10-26 12:58:54 -07:00
|
|
|
await waitForStartupPlugins(electronApp);
|
2024-11-20 03:35:22 -08:00
|
|
|
await startupPromise;
|
2024-09-26 03:35:32 -07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
app: electronApp,
|
2024-10-26 12:58:54 -07:00
|
|
|
mainWindow: await mainWindowPromise,
|
2024-09-26 03:35:32 -07:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (electronApp) {
|
|
|
|
|
await electronApp.firstWindow();
|
|
|
|
|
await electronApp.close();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2023-12-11 05:58:45 -08:00
|
|
|
startupPluginsLoaded: async ({ electronApp }, use) => {
|
2024-10-26 12:58:54 -07:00
|
|
|
await use(waitForStartupPlugins(electronApp));
|
2023-12-11 05:58:45 -08:00
|
|
|
},
|
|
|
|
|
|
2023-10-13 07:32:10 -07:00
|
|
|
mainWindow: async ({ electronApp }, use) => {
|
2025-03-25 13:07:27 -07:00
|
|
|
await use(await initializeMainWindow(electronApp));
|
2023-10-13 07:32:10 -07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-08-17 04:22:03 -07:00
|
|
|
export { default as expect } from './extendedExpect';
|