-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
e2e.spec.ts
165 lines (138 loc) · 5.51 KB
/
e2e.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type {ElectronApplication, JSHandle} from 'playwright';
import {_electron as electron} from 'playwright';
import {expect, test as base} from '@playwright/test';
import type {BrowserWindow} from 'electron';
import {globSync} from 'glob';
import {platform} from 'node:process';
import {createHash} from 'node:crypto';
process.env.PLAYWRIGHT_TEST = 'true';
// Declare the types of your fixtures.
type TestFixtures = {
electronApp: ElectronApplication;
electronVersions: NodeJS.ProcessVersions;
};
const test = base.extend<TestFixtures>({
electronApp: [async ({}, use) => {
/**
* Executable path depends on root package name!
*/
let executablePattern = 'dist/*/root{,.*}';
if (platform === 'darwin') {
executablePattern += '/Contents/*/root';
}
const [executablePath] = globSync(executablePattern);
if (!executablePath) {
throw new Error('App Executable path not found');
}
const electronApp = await electron.launch({
executablePath: executablePath,
});
electronApp.on('console', (msg) => {
if (msg.type() === 'error') {
console.error(`[electron][${msg.type()}] ${msg.text()}`);
}
});
await use(electronApp);
// This code runs after all the tests in the worker process.
await electronApp.close();
}, {scope: 'worker', auto: true} as any],
page: async ({electronApp}, use) => {
const page = await electronApp.firstWindow();
// capture errors
page.on('pageerror', (error) => {
console.error(error);
});
// capture console messages
page.on('console', (msg) => {
console.log(msg.text());
});
await page.waitForLoadState('load');
await use(page);
},
electronVersions: async ({electronApp}, use) => {
await use(await electronApp.evaluate(() => process.versions));
},
});
test('Main window state', async ({electronApp, page}) => {
const window: JSHandle<BrowserWindow> = await electronApp.browserWindow(page);
const windowState = await window.evaluate(
(mainWindow): Promise<{isVisible: boolean; isDevToolsOpened: boolean; isCrashed: boolean}> => {
const getState = () => ({
isVisible: mainWindow.isVisible(),
isDevToolsOpened: mainWindow.webContents.isDevToolsOpened(),
isCrashed: mainWindow.webContents.isCrashed(),
});
return new Promise(resolve => {
/**
* The main window is created hidden, and is shown only when it is ready.
* See {@link ../packages/main/src/mainWindow.ts} function
*/
if (mainWindow.isVisible()) {
resolve(getState());
} else {
mainWindow.once('ready-to-show', () => resolve(getState()));
}
});
},
);
expect(windowState.isCrashed, 'The app has crashed').toEqual(false);
expect(windowState.isVisible, 'The main window was not visible').toEqual(true);
expect(windowState.isDevToolsOpened, 'The DevTools panel was open').toEqual(false);
});
test.describe('Main window web content', async () => {
test('The main window has an interactive button', async ({page}) => {
const element = page.getByRole('button');
await expect(element).toBeVisible();
await expect(element).toHaveText('count is 0');
await element.click();
await expect(element).toHaveText('count is 1');
});
test('The main window has a vite logo', async ({page}) => {
const element = page.getByAltText('Vite logo');
await expect(element).toBeVisible();
await expect(element).toHaveRole('img');
const imgState = await element.evaluate((img: HTMLImageElement) => img.complete);
const imgNaturalWidth = await element.evaluate((img: HTMLImageElement) => img.naturalWidth);
expect(imgState).toEqual(true);
expect(imgNaturalWidth).toBeGreaterThan(0);
});
});
test.describe('Preload context should be exposed', async () => {
test.describe(`versions should be exposed`, async () => {
test('with same type`', async ({page}) => {
const type = await page.evaluate(() => typeof globalThis[btoa('versions')]);
expect(type).toEqual('object');
});
test('with same value', async ({page, electronVersions}) => {
const value = await page.evaluate(() => globalThis[btoa('versions')]);
expect(value).toEqual(electronVersions);
});
});
test.describe(`sha256sum should be exposed`, async () => {
test('with same type`', async ({page}) => {
const type = await page.evaluate(() => typeof globalThis[btoa('sha256sum')]);
expect(type).toEqual('function');
});
test('with same behavior', async ({page}) => {
const testString = btoa(`${Date.now() * Math.random()}`);
const expectedValue = createHash('sha256').update(testString).digest('hex');
const value = await page.evaluate((str) => globalThis[btoa('sha256sum')](str), testString);
expect(value).toEqual(expectedValue);
});
});
test.describe(`send should be exposed`, async () => {
test('with same type`', async ({page}) => {
const type = await page.evaluate(() => typeof globalThis[btoa('send')]);
expect(type).toEqual('function');
});
test('with same behavior', async ({page, electronApp}) => {
await electronApp.evaluate(async ({ipcMain}) => {
ipcMain.handle('test', (event, message) => btoa(message));
});
const testString = btoa(`${Date.now() * Math.random()}`);
const expectedValue = btoa(testString);
const value = await page.evaluate(async (str) => await globalThis[btoa('send')]('test', str), testString);
expect(value).toEqual(expectedValue);
});
});
});