diff --git a/example-cjs/e2e/interaction.spec.ts b/example-cjs/e2e/interaction.spec.ts index a2d74558..1d6e7663 100644 --- a/example-cjs/e2e/interaction.spec.ts +++ b/example-cjs/e2e/interaction.spec.ts @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/example/e2e/interaction.spec.ts b/example/e2e/interaction.spec.ts index a2d74558..1d6e7663 100644 --- a/example/e2e/interaction.spec.ts +++ b/example/e2e/interaction.spec.ts @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/src/service.ts b/src/service.ts index 52ecfbaa..3bf5fbb8 100644 --- a/src/service.ts +++ b/src/service.ts @@ -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 { const browser = instance as WebdriverIO.Browser; const mrBrowser = instance as WebdriverIO.MultiRemoteBrowser; this.#browser = browser; @@ -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; + }); } } } diff --git a/test/service.spec.ts b/test/service.spec.ts index 019b1ec4..b671e4fd 100644 --- a/test/service.spec.ts +++ b/test/service.spec.ts @@ -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'; @@ -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)); @@ -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'], @@ -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));