Skip to content

Commit

Permalink
test: standardise multiremote and standalone e2es across examples
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Apr 9, 2024
1 parent f2af201 commit ced35e6
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 6 deletions.
22 changes: 22 additions & 0 deletions example-cjs/e2e-multiremote/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { browser } from 'wdio-electron-service';
import { multiremotebrowser, expect } from '@wdio/globals';

const { name, version } = globalThis.packageJson;

describe('Electron APIs using Multiremote', () => {
it('should retrieve app metadata through the electron API', async () => {
const appName = await browser.electron.execute((electron) => electron.app.getName());
expect(appName).toStrictEqual([name, name]);
const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
expect(appVersion).toStrictEqual([version, version]);
});

it('should retrieve instance-specific values from a single instance', async () => {
const browserA = multiremotebrowser.getInstance('browserA');
expect(await browserA.electron.execute(() => process.argv.includes('--browser=A'))).toBe(true);
expect(await browserA.electron.execute(() => process.argv.includes('--browser=B'))).toBe(false);
const browserB = multiremotebrowser.getInstance('browserB');
expect(await browserB.electron.execute(() => process.argv.includes('--browser=A'))).toBe(false);
expect(await browserB.electron.execute(() => process.argv.includes('--browser=B'))).toBe(true);
});
});
44 changes: 44 additions & 0 deletions example-cjs/e2e-standalone/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import path from 'node:path';
import fs from 'node:fs';
import process from 'node:process';

import { startSession } from 'wdio-electron-service';
import type { PackageJson } from 'read-package-up';

const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' }),
) as PackageJson;

const getBinaryExtension = () => {
if (process.platform === 'darwin') {
return '.app/Contents/MacOS/wdio-electron-service-example';
} else if (process.platform === 'win32') {
return '.exe';
}

return '';
};

const getBinaryPath = (packageName: string) =>
`./out/${packageName}-${process.platform}-${process.arch}/${packageName}${getBinaryExtension()}`;

async function init() {
const browser = await startSession({
appBinaryPath: getBinaryPath('wdio-electron-service-example'),
appArgs: ['foo', 'bar=baz'],
});

const appName = await browser.electron.execute((electron) => electron.app.getName());
if (appName !== packageJson.name) {
throw new Error(`appName test failed: ${appName} !== ${packageJson.name}`);
}

const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
if (appVersion !== packageJson.version) {
throw new Error(`appVersion test failed: ${appVersion} !== ${packageJson.version}`);
}

process.exit();
}

init();
4 changes: 3 additions & 1 deletion example-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"ci": "pnpm i && pnpm build && pnpm test",
"clean": "pnpm clean:dist && rm -rf ./node_modules pnpm-lock.yaml ./wdio-logs ./out",
"clean:dist": "pnpx rimraf ./dist && mkdir -p ./dist",
"test": "wdio run ./wdio.conf.ts"
"test": "wdio run ./wdio.conf.ts && pnpm test:multiremote && pnpm test:standalone",
"test:multiremote": "wdio run ./wdio.multiremote.conf.ts",
"test:standalone": "ts-node ./e2e-standalone/api.spec.ts"
},
"dependencies": {
"wdio-electron-service": "file:../"
Expand Down
52 changes: 52 additions & 0 deletions example-cjs/wdio.multiremote.conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from 'node:path';
import fs from 'node:fs';
import type { PackageJson } from 'read-package-up';
import type { Options } from '@wdio/types';

const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }),
) as PackageJson;

globalThis.packageJson = packageJson;
process.env.TEST = 'true';

export const config: Options.Testrunner = {
services: ['electron'],
waitforTimeout: 5000,
connectionRetryCount: 10,
connectionRetryTimeout: 30000,
logLevel: 'debug',
runner: 'local',
autoCompileOpts: {
autoCompile: true,
tsNodeOpts: {
transpileOnly: true,
project: path.join(__dirname, 'tsconfig.json'),
},
},
framework: 'mocha',
mochaOpts: {
ui: 'bdd',
timeout: 30000,
},
outputDir: 'wdio-multiremote-logs',
specs: ['./e2e-multiremote/*.ts'],
capabilities: {
browserA: {
capabilities: {
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=A'],
},
} as WebdriverIO.Capabilities,
},
browserB: {
capabilities: {
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=B'],
},
} as WebdriverIO.Capabilities,
},
},
};
22 changes: 22 additions & 0 deletions example-electron-builder/e2e-multiremote/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { browser } from 'wdio-electron-service';
import { multiremotebrowser, expect } from '@wdio/globals';

const { name, version } = globalThis.packageJson;

describe('Electron APIs using Multiremote', () => {
it('should retrieve app metadata through the electron API', async () => {
const appName = await browser.electron.execute((electron) => electron.app.getName());
expect(appName).toStrictEqual([name, name]);
const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
expect(appVersion).toStrictEqual([version, version]);
});

it('should retrieve instance-specific values from a single instance', async () => {
const browserA = multiremotebrowser.getInstance('browserA');
expect(await browserA.electron.execute(() => process.argv.includes('--browser=A'))).toBe(true);
expect(await browserA.electron.execute(() => process.argv.includes('--browser=B'))).toBe(false);
const browserB = multiremotebrowser.getInstance('browserB');
expect(await browserB.electron.execute(() => process.argv.includes('--browser=A'))).toBe(false);
expect(await browserB.electron.execute(() => process.argv.includes('--browser=B'))).toBe(true);
});
});
42 changes: 42 additions & 0 deletions example-electron-builder/e2e-standalone/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import url from 'node:url';
import path from 'node:path';
import fs from 'node:fs';
import process from 'node:process';

import { startSession } from 'wdio-electron-service';
import type { PackageJson } from 'read-package-up';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', 'package.json'), { encoding: 'utf-8' }),
) as PackageJson;

const getBinaryExtension = () => {
if (process.platform === 'darwin') {
return '.app/Contents/MacOS/wdio-electron-service-example';
} else if (process.platform === 'win32') {
return '.exe';
}

return '';
};

const getBinaryPath = (packageName: string) =>
`./out/${packageName}-${process.platform}-${process.arch}/${packageName}${getBinaryExtension()}`;

const browser = await startSession({
appBinaryPath: getBinaryPath('wdio-electron-service-example'),
appArgs: ['foo', 'bar=baz'],
});

const appName = await browser.electron.execute((electron) => electron.app.getName());
if (appName !== packageJson.name) {
throw new Error(`appName test failed: ${appName} !== ${packageJson.name}`);
}

const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
if (appVersion !== packageJson.version) {
throw new Error(`appVersion test failed: ${appVersion} !== ${packageJson.version}`);
}

process.exit();
4 changes: 3 additions & 1 deletion example-electron-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"ci": "pnpm i && pnpm build && pnpm test",
"clean": "pnpm clean:dist && rm -rf ./node_modules pnpm-lock.yaml ./wdio-logs",
"clean:dist": "pnpx rimraf ./dist && mkdir -p ./dist",
"test": "wdio run ./wdio.conf.ts"
"test": "wdio run ./wdio.conf.ts && pnpm test:multiremote && pnpm test:standalone",
"test:multiremote": "wdio run ./wdio.multiremote.conf.ts",
"test:standalone": "ts-node ./e2e-standalone/api.spec.ts"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.4",
Expand Down
26 changes: 26 additions & 0 deletions example-electron-builder/wdio.multiremote.conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Options } from '@wdio/types';
import { config as baseConfig } from './wdio.conf.js';

export const config: Options.Testrunner = {
...baseConfig,
outputDir: 'wdio-multiremote-logs',
specs: ['./e2e-multiremote/*.ts'],
capabilities: {
browserA: {
capabilities: {
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=A'],
},
} as WebdriverIO.Capabilities,
},
browserB: {
capabilities: {
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=B'],
},
} as WebdriverIO.Capabilities,
},
},
};
2 changes: 1 addition & 1 deletion example/e2e-standalone/api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import url from 'node:url';
import path from 'node:path';
import url from 'node:url';
import fs from 'node:fs';
import process from 'node:process';

Expand Down
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"ci": "pnpm i && pnpm build && pnpm test",
"clean": "pnpm clean:dist && rm -rf ./node_modules pnpm-lock.yaml ./wdio-logs ./out",
"clean:dist": "pnpx rimraf ./dist && mkdir -p ./dist",
"test": "wdio run ./wdio.conf.ts && wdio run ./wdio.multiremote.conf.ts && pnpm test:standalone",
"test": "wdio run ./wdio.conf.ts && pnpm test:multiremote && pnpm test:standalone",
"test:multiremote": "wdio run ./wdio.multiremote.conf.ts",
"test:standalone": "ts-node ./e2e-standalone/api.spec.ts",
"test:standalone:local": "cd .. && pnpm build && cd - && rm -rf ./node_modules && pnpm i && pnpm test:standalone"
},
Expand Down
8 changes: 6 additions & 2 deletions src/cjs/classes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Capabilities, Options, Services } from '@wdio/types';

// Workaround for ts-node converting dynamic imports to requires
// see https://github.com/TypeStrong/ts-node/discussions/1290
const dynamicImport = new Function('specifier', 'return import(specifier)');

export class CJSElectronLauncher {
private instance?: Promise<Services.ServiceInstance>;

constructor(options: unknown, caps: unknown, config: Options.Testrunner) {
this.instance = (async () => {
const importPath = '../service.js';
const { default: ElectronService } = await import(importPath);
const { default: ElectronService } = await dynamicImport(importPath);
return new ElectronService(options, caps, config);
})();
}
Expand All @@ -23,7 +27,7 @@ export class CJSElectronService {
constructor(globalOptions: unknown) {
this.instance = (async () => {
const importPath = '../service.js';
const { default: ElectronService } = await import(importPath);
const { default: ElectronService } = await dynamicImport(importPath);
return new ElectronService(globalOptions);
})();
}
Expand Down

0 comments on commit ced35e6

Please sign in to comment.