Skip to content

Commit

Permalink
feat: session utility
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Apr 9, 2024
1 parent 18d9089 commit 289e6ce
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
22 changes: 22 additions & 0 deletions example/e2e-standalone/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import url from 'node:url';
import path from 'node:path';
import fs from 'node:fs';

import { startSession } from 'wdio-electron-service';
import { expect } from '@wdio/globals';
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 browser = await startSession({
appBinaryPath: './out/wdio-electron-service-example-darwin-arm64/wdio-electron-service-example.app',
appArgs: ['foo', 'bar=baz'],
});

const appName = await browser.electron.execute((electron) => electron.app.getName());
expect(appName).toStrictEqual(packageJson.name);
const appVersion = await browser.electron.execute((electron) => electron.app.getVersion());
expect(appVersion).toStrictEqual(packageJson.version);
4 changes: 3 additions & 1 deletion example/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 ./out",
"clean:dist": "pnpx rimraf ./dist && mkdir -p ./dist",
"test": "wdio run ./wdio.conf.ts && wdio run ./wdio.multiremote.conf.ts"
"test": "wdio run ./wdio.conf.ts && wdio run ./wdio.multiremote.conf.ts && pnpm test:standalone",
"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"
},
"dependencies": {
"wdio-electron-service": "file:../"
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { browser as wdioBrowser } from '@wdio/globals';
import { fn as vitestFn } from '@vitest/spy';
import type { PackageJson } from 'read-package-up';

import { init as initSession } from './session.js';
import ElectronLaunchService from './launcher.js';
import ElectronWorkerService from './service.js';
import type {
Expand Down Expand Up @@ -52,4 +53,5 @@ declare global {
}

export const browser: WebdriverIO.Browser = wdioBrowser;
export const startSession: (opts: ElectronServiceOptions) => Promise<WebdriverIO.Browser> = initSession;
export * from './types.js';
8 changes: 6 additions & 2 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ async function fileExists(path: PathLike) {
}
}

export type ElectronServiceCapabilities = Capabilities.RemoteCapabilities & {
[CUSTOM_CAPABILITY_NAME]?: ElectronServiceOptions;
};

export default class ElectronLaunchService implements Services.ServiceInstance {
#globalOptions: ElectronServiceOptions;
#projectRoot: string;

constructor(globalOptions: ElectronServiceOptions, _caps: never, config: Options.Testrunner) {
constructor(globalOptions: ElectronServiceOptions, _caps: unknown, config: Options.Testrunner) {
this.#globalOptions = globalOptions;
this.#projectRoot = config.rootDir || process.cwd();
}

async onPrepare(_: never, capabilities: Capabilities.RemoteCapabilities) {
async onPrepare(_config: Options.Testrunner, capabilities: ElectronServiceCapabilities) {
const capsList = Array.isArray(capabilities)
? capabilities
: Object.values(capabilities).map((multiremoteOption) => multiremoteOption.capabilities);
Expand Down
33 changes: 33 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { remote } from 'webdriverio';

import ElectronWorkerService from './service.js';
import ElectronLaunchService from './launcher.js';
import { CUSTOM_CAPABILITY_NAME } from './constants.js';
import { ElectronServiceOptions } from './types.js';
import { Options } from '@wdio/types';

export async function init(opts: ElectronServiceOptions) {
const testRunnerOpts = { ...opts } as Options.Testrunner;
let capabilities = {
browserName: 'electron',
[CUSTOM_CAPABILITY_NAME]: {
...opts,
},
};

const launcher = new ElectronLaunchService(opts, capabilities, testRunnerOpts);
const service = new ElectronWorkerService(opts);

await launcher.onPrepare(testRunnerOpts, [capabilities]);

console.log('setting caps', capabilities);

// initialise session
const browser = await remote({
capabilities,
});

await service.before(capabilities, [], browser);

return browser;
}

0 comments on commit 289e6ce

Please sign in to comment.