Skip to content

Commit 5614462

Browse files
committed
Co-authored-by: Karen Cheung <karenc810@users.noreply.github.com>
Co-authored-by: Ryan Huie <8-ryan-8@users.noreply.github.com>
1 parent 75081a1 commit 5614462

File tree

5 files changed

+69
-81
lines changed

5 files changed

+69
-81
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"electron": "^18.0.0",
116116
"electron-installer-dmg": "^4.0.0",
117117
"electron-packager": "^15.5.1",
118+
"electron-playwright-helpers": "^1.2.0",
118119
"electron-reloader": "^1.2.3",
119120
"electron-winstaller": "^5.0.0",
120121
"eslint": "^8.21.0",

prev-tst/main.js

-44
This file was deleted.

prev-tst/prev-tst.js

-30
This file was deleted.

src/index.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ ipcMain.on('openDebugAppWindow', (event, localhostToUse) => {
327327
if(localhostToUse.length === 4 || localhostToUse.length === 5) openBrowserWindow(localhostToUse);
328328
});
329329

330+
330331
/*
331332
* ==================================================
332333
* The injected debugging script uses the ipcRenderer in the browser window to send snapshots when there are state changes
@@ -373,4 +374,31 @@ ipcMain.on('REFRESH', (event, data) => {
373374
};
374375
// Use browser window to send REFRESH message
375376
browser.webContents.send('REFRESH', instance);
376-
})
377+
})
378+
379+
//--------------- FOR TESTING -----------------
380+
ipcMain.on('new-window', () => {
381+
createWindow()
382+
})
383+
384+
// testing main synchronous data passing
385+
function mainSynchronousData() {
386+
return 'Main Synchronous Data'
387+
}
388+
389+
ipcMain.on('main-synchronous-data', (event, arg) => {
390+
return mainSynchronousData()
391+
})
392+
393+
// testing main asynchronous data passing
394+
async function mainAsynchronousData() {
395+
return new Promise(resolve => {
396+
setTimeout(() => {
397+
resolve('Main Asynchronous Data')
398+
}, 1000)
399+
})
400+
}
401+
402+
ipcMain.on('main-asynchronous-data', async (event, arg) => {
403+
return await mainAsynchronousData()
404+
})

test/base.test.ts

+39-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { _electron as electron } from "playwright";
22
import { test, expect } from "@playwright/test";
3+
import {
4+
clickMenuItemById,
5+
findLatestBuild,
6+
ipcMainCallFirstListener,
7+
ipcRendererCallFirstListener,
8+
parseElectronApp,
9+
ipcMainInvokeHandler,
10+
ipcRendererInvoke
11+
} from 'electron-playwright-helpers'
312

413
test("Launch electron app", async () => {
514
// Launch electron app
@@ -51,12 +60,36 @@ test.describe("Check Main Page", async () => {
5160
expect(title).toBe("SvelteStorm");
5261
});
5362

54-
test("Check version number: APP", async () => {
55-
const versionNumberApp = await firstWindow.innerText("data-testid=version-number-app");
56-
expect(versionNumberApp).not.toBe("-");
57-
const isValidNumberApp = semver.valid(semver.coerce(versionNumberApp));
58-
expect(semver.valid(isValidNumberApp)).not.toBeNull();
59-
});
63+
test('Trigger IPC listener via main process', async () => {
64+
electronApp.evaluate(({ ipcMain }) => {
65+
ipcMain.emit('new-window')
66+
})
67+
const newPage = await electronApp.waitForEvent('window')
68+
expect(newPage).toBeTruthy()
69+
firstWindow = newPage
70+
})
71+
72+
test('Send IPC message from Renderer to Main and open new window', async () => {
73+
// evaluate this script in render process
74+
// requires webPreferences.nodeIntegration true and contextIsolation false
75+
await firstWindow.evaluate(() => {
76+
require('electron').ipcRenderer.send('openDebugAppWindow', '8080')
77+
})
78+
79+
const newPage = await electronApp.waitForEvent('window')
80+
expect(newPage).toBeTruthy()
81+
firstWindow = newPage
82+
})
83+
84+
test('receive synchronous data via ipcMainCallFirstListener()', async () => {
85+
const data = await ipcMainCallFirstListener(electronApp, 'main-synchronous-data')
86+
expect(data).toBe('Main Synchronous Data')
87+
})
88+
89+
test('receive asynchronous data via ipcMainCallFirstListener()', async () => {
90+
const data = await ipcMainCallFirstListener(electronApp, 'main-asynchronous-data')
91+
expect(data).toBe('Main Asynchronous Data')
92+
})
6093

6194
test.afterAll(async () => {
6295
await electronApp.close();

0 commit comments

Comments
 (0)