2020-09-24 19:55:27 +00:00
|
|
|
import { Application } from 'spectron';
|
|
|
|
import path from 'path';
|
|
|
|
import os from 'os';
|
|
|
|
import electronPath from '../../insomnia-app/node_modules/electron';
|
2021-02-25 20:39:59 +00:00
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import fs from 'fs';
|
2020-09-24 19:55:27 +00:00
|
|
|
|
|
|
|
const getAppPlatform = () => process.platform;
|
|
|
|
const isMac = () => getAppPlatform() === 'darwin';
|
|
|
|
const isLinux = () => getAppPlatform() === 'linux';
|
|
|
|
const isWindows = () => getAppPlatform() === 'win32';
|
|
|
|
|
2020-11-25 02:26:00 +00:00
|
|
|
export const isBuild = () => process.env.BUNDLE === 'build';
|
|
|
|
export const isPackage = () => process.env.BUNDLE === 'package';
|
|
|
|
|
2021-02-25 20:39:59 +00:00
|
|
|
const spectronConfig = designerDataPath => {
|
2020-09-24 19:55:27 +00:00
|
|
|
let packagePathSuffix = '';
|
|
|
|
if (isWindows()) {
|
2021-02-03 07:07:11 +00:00
|
|
|
packagePathSuffix = path.join('win-unpacked', 'Insomnia.exe');
|
2020-09-24 19:55:27 +00:00
|
|
|
} else if (isMac()) {
|
2021-02-03 07:07:11 +00:00
|
|
|
packagePathSuffix = path.join('mac', 'Insomnia.app', 'Contents', 'MacOS', 'Insomnia');
|
2020-09-24 19:55:27 +00:00
|
|
|
} else if (isLinux()) {
|
|
|
|
packagePathSuffix = ''; // TODO: find out what this is
|
|
|
|
}
|
|
|
|
|
2021-02-03 07:07:11 +00:00
|
|
|
const buildPath = path.join(__dirname, '../../insomnia-app/build');
|
|
|
|
const packagePath = path.join(__dirname, '../../insomnia-app/dist', packagePathSuffix);
|
2021-02-25 20:39:59 +00:00
|
|
|
const dataPath = path.join(os.tmpdir(), 'insomnia-smoke-test', `${Date.now()}`);
|
|
|
|
const env = { INSOMNIA_DATA_PATH: dataPath };
|
2020-09-24 19:55:27 +00:00
|
|
|
|
2021-02-25 20:39:59 +00:00
|
|
|
if (designerDataPath) {
|
|
|
|
env.DESIGNER_DATA_PATH = designerDataPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { buildPath, packagePath, env };
|
2020-09-24 19:55:27 +00:00
|
|
|
};
|
|
|
|
|
2021-02-25 20:39:59 +00:00
|
|
|
export const launchApp = async designerDataPath => {
|
|
|
|
const config = spectronConfig(designerDataPath);
|
2020-09-24 19:55:27 +00:00
|
|
|
return await launch(config);
|
|
|
|
};
|
|
|
|
|
2020-09-24 20:14:54 +00:00
|
|
|
const getLaunchPath = config =>
|
2020-11-25 02:26:00 +00:00
|
|
|
isPackage()
|
2020-09-24 20:14:54 +00:00
|
|
|
? { path: config.packagePath }
|
|
|
|
: {
|
|
|
|
path: electronPath,
|
|
|
|
args: [config.buildPath],
|
|
|
|
};
|
|
|
|
|
2020-09-24 19:55:27 +00:00
|
|
|
const launch = async config => {
|
|
|
|
if (!config) {
|
|
|
|
throw new Error('Spectron config could not be loaded.');
|
|
|
|
}
|
|
|
|
|
2020-09-24 20:14:54 +00:00
|
|
|
const app = new Application({
|
|
|
|
...getLaunchPath(config),
|
2020-09-24 19:55:27 +00:00
|
|
|
|
2020-09-24 20:14:54 +00:00
|
|
|
// Don't remove chromeDriverArgs
|
2020-09-24 19:55:27 +00:00
|
|
|
// https://github.com/electron-userland/spectron/issues/353#issuecomment-522846725
|
|
|
|
chromeDriverArgs: ['remote-debugging-port=9222'],
|
|
|
|
|
2021-02-25 20:39:59 +00:00
|
|
|
env: config.env,
|
2020-09-24 19:55:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await app.start().then(async () => {
|
|
|
|
// Windows spawns two terminal windows when running spectron, and the only workaround
|
|
|
|
// is to focus the window on start.
|
|
|
|
// https://github.com/electron-userland/spectron/issues/60
|
|
|
|
await app.browserWindow.focus();
|
|
|
|
await app.browserWindow.setAlwaysOnTop(true);
|
2020-12-04 21:47:04 +00:00
|
|
|
|
|
|
|
// Set the implicit wait timeout to 0 (webdriver default)
|
|
|
|
// https://webdriver.io/docs/timeouts.html#session-implicit-wait-timeout
|
|
|
|
// Spectron overrides it to an unreasonable value, as per the issue
|
|
|
|
// https://github.com/electron-userland/spectron/issues/763
|
|
|
|
await app.client.setTimeout({ implicit: 0 });
|
2020-09-24 19:55:27 +00:00
|
|
|
|
2021-02-25 20:39:59 +00:00
|
|
|
// Set bounds to default size
|
|
|
|
await app.browserWindow.setSize(1280, 700);
|
|
|
|
});
|
2020-09-24 19:55:27 +00:00
|
|
|
return app;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stop = async app => {
|
2021-02-25 20:39:59 +00:00
|
|
|
await takeScreenshotOnFailure(app);
|
|
|
|
|
2020-09-24 19:55:27 +00:00
|
|
|
if (app && app.isRunning()) {
|
|
|
|
await app.stop();
|
|
|
|
}
|
|
|
|
};
|
2021-02-25 20:39:59 +00:00
|
|
|
|
|
|
|
const takeScreenshotOnFailure = async app => {
|
|
|
|
if (jasmine.currentTest.failedExpectations.length) {
|
|
|
|
await takeScreenshot(app, jasmine.currentTest.fullName.replace(/ /g, '_'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const takeScreenshot = async (app, name) => {
|
|
|
|
mkdirp.sync('screenshots');
|
|
|
|
const buffer = await app.browserWindow.capturePage();
|
|
|
|
await fs.promises.writeFile(path.join('screenshots', `${name}.png`), buffer);
|
|
|
|
};
|