Skip to content

Commit

Permalink
refactor: rework CJS for session
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Apr 9, 2024
1 parent c86ef82 commit f2af201
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 44 deletions.
45 changes: 45 additions & 0 deletions src/cjs/classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Capabilities, Options, Services } from '@wdio/types';

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);
return new ElectronService(options, caps, config);
})();
}

async onPrepare(config: Options.Testrunner, capabilities: Capabilities.RemoteCapabilities) {
const instance = await this.instance;
return instance?.onPrepare?.(config, capabilities);
}
}

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

constructor(globalOptions: unknown) {
this.instance = (async () => {
const importPath = '../service.js';
const { default: ElectronService } = await import(importPath);
return new ElectronService(globalOptions);
})();
}

async beforeSession(
config: Options.Testrunner,
capabilities: WebdriverIO.Capabilities,
specs: string[],
cid: string,
) {
const instance = await this.instance;
return instance?.beforeSession?.(config, capabilities, specs, cid);
}

async before(capabilities: WebdriverIO.Capabilities, specs: string[], browser: WebdriverIO.Browser) {
const instance = await this.instance;
return instance?.before?.(capabilities, specs, browser);
}
}
49 changes: 5 additions & 44 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { fn as vitestFn } from '@vitest/spy';
import { browser as wdioBrowser } from '@wdio/globals';
import type { Capabilities, Services, Options } from '@wdio/types';
import type { PackageJson } from 'read-package-up';

import { init as initSession } from './session.js';
import { CJSElectronLauncher, CJSElectronService } from './classes.js';
import type {
ElectronInterface,
ElectronServiceAPI,
Expand All @@ -11,49 +12,8 @@ import type {
WdioElectronWindowObj,
} from './types.js';

exports.default = class CJSElectronService {
private instance?: Promise<Services.ServiceInstance>;

constructor(options: unknown, caps: never, config: Options.Testrunner) {
this.instance = (async () => {
const importPath = '../service.js';
const { default: ElectronService } = await import(importPath);
return new ElectronService(options, caps, config);
})();
}

async beforeSession(
config: Options.Testrunner,
capabilities: WebdriverIO.Capabilities,
specs: string[],
cid: string,
) {
const instance = await this.instance;
return instance?.beforeSession?.(config, capabilities, specs, cid);
}

async before(capabilities: WebdriverIO.Capabilities, specs: string[], browser: WebdriverIO.Browser) {
const instance = await this.instance;
return instance?.before?.(capabilities, specs, browser);
}
};

exports.launcher = class CJSElectronLauncher {
private instance?: Promise<Services.ServiceInstance>;

constructor(options: unknown, caps: never, config: Options.Testrunner) {
this.instance = (async () => {
const importPath = '../service.js';
const { default: ElectronService } = await import(importPath);
return new ElectronService(options, caps, config);
})();
}

async onPrepare(config: Options.Testrunner, capabilities: Capabilities.RemoteCapabilities) {
const instance = await this.instance;
return instance?.onPrepare?.(config, capabilities);
}
};
exports.default = CJSElectronService;
exports.launcher = CJSElectronLauncher;

interface BrowserExtension {
/**
Expand Down Expand Up @@ -91,3 +51,4 @@ declare global {
}

export const browser: WebdriverIO.Browser = wdioBrowser;
export const startSession: (opts: ElectronServiceOptions) => Promise<WebdriverIO.Browser> = initSession;
32 changes: 32 additions & 0 deletions src/cjs/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { remote } from 'webdriverio';

import { CJSElectronLauncher, CJSElectronService } from './classes.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 CJSElectronLauncher(opts, capabilities, testRunnerOpts);
const service = new CJSElectronService(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 f2af201

Please sign in to comment.