Skip to content

Commit

Permalink
fix: wait until a BrowserWindow is available
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler committed Dec 12, 2023
1 parent d73e507 commit 2aff921
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
8 changes: 4 additions & 4 deletions example-cjs/e2e/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('application loading', () => {
describe('when the make larger button is clicked', () => {
it('should increase the window height and width by 10 pixels', async () => {
let bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -36,7 +36,7 @@ describe('application loading', () => {
biggerClickCount = await browser.$('.click-count .bigger').getText();
expect(biggerClickCount).toEqual('1');
bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -50,7 +50,7 @@ describe('application loading', () => {
describe('when the make smaller button is clicked', () => {
it('should decrease the window height and width by 10 pixels', async () => {
let bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -61,7 +61,7 @@ describe('application loading', () => {
const elem = await browser.$('.make-smaller');
await elem.click();
bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand Down
8 changes: 4 additions & 4 deletions example/e2e/interaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('application loading', () => {
describe('when the make larger button is clicked', () => {
it('should increase the window height and width by 10 pixels', async () => {
let bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -36,7 +36,7 @@ describe('application loading', () => {
biggerClickCount = await browser.$('.click-count .bigger').getText();
expect(biggerClickCount).toEqual('1');
bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -50,7 +50,7 @@ describe('application loading', () => {
describe('when the make smaller button is clicked', () => {
it('should decrease the window height and width by 10 pixels', async () => {
let bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand All @@ -61,7 +61,7 @@ describe('application loading', () => {
const elem = await browser.$('.make-smaller');
await elem.click();
bounds = (await browser.electron.execute((electron) => {
const browserWindow = electron.BrowserWindow.getFocusedWindow() as BrowserWindow;
const browserWindow = electron.BrowserWindow.getAllWindows()[0] as BrowserWindow;
return browserWindow.getBounds();
})) as {
width: number;
Expand Down
20 changes: 17 additions & 3 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export default class ElectronWorkerService implements Services.ServiceInstance {
return Object.assign({}, api) as BrowserExtension['electron'];
}

before(
async before(
_capabilities: Capabilities.RemoteCapability,
_specs: string[],
instance: WebdriverIO.Browser | WebdriverIO.MultiRemoteBrowser,
): void {
): Promise<void> {
const browser = instance as WebdriverIO.Browser;
const mrBrowser = instance as WebdriverIO.MultiRemoteBrowser;
this.#browser = browser;
Expand All @@ -57,10 +57,24 @@ export default class ElectronWorkerService implements Services.ServiceInstance {
if (!caps[CUSTOM_CAPABILITY_NAME]) {
continue;
}
log.debug('Adding Electron API to browser object instance named: ', instance);

log.debug('Adding Electron API to browser object instance named: ', instance);
mrInstance.electron = this.#getElectronAPI(mrInstance);

// wait until an Electron BrowserWindow is available
await mrInstance.waitUntil(async () => {
const numWindows = await mrInstance.electron.execute(
(electron) => electron.BrowserWindow.getAllWindows().length,
);
return numWindows > 0;
});
}
} else {
// wait until an Electron BrowserWindow is available
await browser.waitUntil(async () => {
const numWindows = await browser.electron.execute((electron) => electron.BrowserWindow.getAllWindows().length);
return numWindows > 0;
});
}
}
}
14 changes: 9 additions & 5 deletions test/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, beforeEach, it, expect } from 'vitest';
import { vi, describe, beforeEach, it, expect } from 'vitest';

import { mockProcessProperty } from './helpers';
import type { BrowserExtension } from '../src';
Expand All @@ -15,7 +15,9 @@ describe('before', () => {

it('should add commands to the browser object', () => {
instance = new WorkerService();
const browser = {} as unknown as WebdriverIO.Browser;
const browser = {
waitUntil: vi.fn().mockResolvedValue(true),
} as unknown as WebdriverIO.Browser;
instance.before({}, [], browser);
const serviceApi = browser.electron as BrowserExtension['electron'];
expect(serviceApi.execute).toEqual(expect.any(Function));
Expand All @@ -29,8 +31,11 @@ describe('before', () => {
instance = new WorkerService();
const browser = {
instanceMap: {
electron: { requestedCapabilities: { 'wdio:electronServiceOptions': {} } },
firefox: { requestedCapabilities: {} },
electron: {
requestedCapabilities: { 'wdio:electronServiceOptions': {} },
waitUntil: vi.fn().mockResolvedValue(true),
},
firefox: { requestedCapabilities: {}, waitUntil: vi.fn().mockResolvedValue(true) },
},
isMultiremote: true,
instances: ['electron', 'firefox'],
Expand All @@ -40,7 +45,6 @@ describe('before', () => {
instance.before({}, [], instance.browser);

const electronInstance = instance.browser.getInstance('electron');
console.log('ZOMG', electronInstance, browser);
let serviceApi = electronInstance.electron;
expect(serviceApi.execute).toEqual(expect.any(Function));
expect(serviceApi.mock).toEqual(expect.any(Function));
Expand Down

0 comments on commit 2aff921

Please sign in to comment.