Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass browserInstance through to execute #329

Merged
merged 9 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions example-cjs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions example/e2e-multiremote/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ describe('Electron APIs using Multiremote', () => {
expect(appVersion).toStrictEqual([version, version]);
});

it('should allow to retrieve API values from single instance', async () => {
it('should retrieve instance-specific values from a single instance', async () => {
const browserA = multiremotebrowser.getInstance('browserA');
expect(await browserA.electron.execute((electron) => electron.app.getName())).toStrictEqual([name, name]);
expect(await browserA.electron.execute((electron) => electron.app.getVersion())).toStrictEqual([version, version]);
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((electron) => electron.app.getName())).toStrictEqual([name, name]);
expect(await browserB.electron.execute((electron) => electron.app.getVersion())).toStrictEqual([version, version]);
expect(await browserB.electron.execute(() => process.argv.includes('--browser=A'))).toBe(false);
expect(await browserB.electron.execute(() => process.argv.includes('--browser=B'))).toBe(true);
});
});
11 changes: 9 additions & 2 deletions example/wdio.multiremote.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ 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',
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=A'],
},
},
},
browserB: {
capabilities: {
browserName: 'electron',
'browserName': 'electron',
'wdio:electronServiceOptions': {
appArgs: ['browser=B'],
},
},
},
},
Expand Down
5 changes: 1 addition & 4 deletions src/commands/execute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type ElectronWorkerService from '../service.js';

export async function execute<ReturnValue, InnerArguments extends unknown[]>(
this: ElectronWorkerService,
browser: WebdriverIO.Browser,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then do:

Suggested change
browser: WebdriverIO.Browser,
this: WebdriverIO.Browser,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried these two - the instance test fails, producing an array of values again

script: string | ((...innerArgs: InnerArguments) => ReturnValue),
...args: InnerArguments
): Promise<ReturnValue> {
Expand All @@ -12,7 +10,6 @@ export async function execute<ReturnValue, InnerArguments extends unknown[]>(
throw new Error('Expecting script to be type of "string" or "function"');
}

const browser = this.browser as WebdriverIO.Browser;
if (!browser) {
throw new Error('WDIO browser is not yet initialised');
}
Expand Down
9 changes: 6 additions & 3 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export default class ElectronWorkerService implements Services.ServiceInstance {
this.#browser = browser;
}

#getElectronAPI() {
#getElectronAPI(browserInstance?: WebdriverIO.Browser) {
const browser = (browserInstance || this.browser) as WebdriverIO.Browser;
const api = {
_mocks: {} as Record<string, ElectronServiceMock>,
execute: execute.bind(this),
execute: (script: string | ((...innerArgs: unknown[]) => unknown), ...args: unknown[]) =>
execute.apply(this, [browser, script, ...args]),
goosewobbler marked this conversation as resolved.
Show resolved Hide resolved
mock: mock.bind(this),
mockAll: mockAll.bind(this),
removeMocks: removeMocks.bind(this),
Expand Down Expand Up @@ -56,7 +58,8 @@ export default class ElectronWorkerService implements Services.ServiceInstance {
continue;
}
log.debug('Adding Electron API to browser object instance named: ', instance);
mrInstance.electron = this.#getElectronAPI();

mrInstance.electron = this.#getElectronAPI(mrInstance);
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions test/commands/execute.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import { vi, describe, beforeEach, it, expect } from 'vitest';

import { execute } from '../../src/commands/execute';
import ElectronWorkerService from '../../src';

describe('execute', () => {
let workerService;
beforeEach(async () => {
globalThis.browser = {
execute: vi.fn(),
} as unknown as WebdriverIO.Browser;
workerService = new ElectronWorkerService({});
workerService.browser = globalThis.browser;
});

it('should throw an error when called with a parameter of the wrong type', async () => {
await expect(() => execute.call(workerService, {})).rejects.toThrowError(
it('should throw an error when called with a script argument of the wrong type', async () => {
await expect(() => execute(globalThis.browser, {} as string)).rejects.toThrowError(
new Error('Expecting script to be type of "string" or "function"'),
);
});

it('should throw an error when called without a parameter', async () => {
await expect(() => execute.call(workerService)).rejects.toThrowError(
it('should throw an error when called without a script argument', async () => {
// @ts-expect-error no script argument
await expect(() => execute(globalThis.browser)).rejects.toThrowError(
new Error('Expecting script to be type of "string" or "function"'),
);
});

it('should throw an error when the browser is not initialised', async () => {
workerService.browser = undefined;
await expect(() => execute.call(workerService, 'return 1 + 2 + 3')).rejects.toThrowError(
// @ts-expect-error undefined browser argument
await expect(() => execute(undefined, 'return 1 + 2 + 3')).rejects.toThrowError(
new Error('WDIO browser is not yet initialised'),
);
});

it('should execute a stringified function', async () => {
await execute.call(workerService, 'return 1 + 2 + 3');
await execute(globalThis.browser, 'return 1 + 2 + 3');
expect(globalThis.browser.execute).toHaveBeenCalledWith('return 1 + 2 + 3');
});

it('should execute a function', async () => {
await execute.call(workerService, () => 1 + 2 + 3);
await execute(globalThis.browser, () => 1 + 2 + 3);
expect(globalThis.browser.execute).toHaveBeenCalledWith(expect.any(Function), '() => 1 + 2 + 3');
});
});
13 changes: 7 additions & 6 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { configDefaults, defineConfig } from 'vitest/config';

const symlinkedCJSFiles = ['src/cjs/constants.ts', 'src/cjs/main.ts', 'src/cjs/preload.ts', 'src/cjs/types.ts'];

export default defineConfig({
test: {
include: ['test/**/*.spec.ts'],
Expand All @@ -8,13 +10,12 @@ export default defineConfig({
coverage: {
enabled: true,
include: ['src/**/*'],
// exclude symlinked CJS files from coverage
exclude: ['src/cjs/constants.ts', 'src/cjs/main.ts', 'src/cjs/preload.ts', 'src/cjs/types.ts'],
exclude: [...symlinkedCJSFiles, 'src/types.ts'],
thresholds: {
lines: 75,
functions: 75,
branches: 75,
statements: 75,
lines: 70,
functions: 70,
branches: 70,
statements: 70,
},
},
},
Expand Down