forked from nativefier/nativefier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaywright-test.ts
431 lines (363 loc) · 14.7 KB
/
playwright-test.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { once } from 'events';
import * as fs from 'fs';
import * as path from 'path';
import { Shell } from 'electron';
import {
_electron,
ConsoleMessage,
Dialog,
ElectronApplication,
Page,
} from 'playwright';
import { getTempDir, isLinux } from './helpers/helpers';
import { NativefierOptions } from '../shared/src/options/model';
const INJECT_DIR = path.join(__dirname, '..', 'app', 'inject');
const log = console;
function sleep(milliseconds: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}
/**
* Debugging this? Run your playwright tests in debug mode:
* DEBUG='pw:browser*' npm run test:playwright
*/
describe('Application launch', () => {
jest.setTimeout(60000);
let app: ElectronApplication;
let appClosed = true;
const appMainJSPath = path.join(__dirname, '..', 'app', 'lib', 'main.js');
const DEFAULT_CONFIG: NativefierOptions = {
targetUrl: 'https://npmjs.com',
};
const logFileDir = getTempDir('playwright');
const metaOrAlt = process.platform === 'darwin' ? 'Meta' : 'Alt';
const metaOrCtrl = process.platform === 'darwin' ? 'Meta' : 'Control';
const spawnApp = async (
playwrightConfig: NativefierOptions = { ...DEFAULT_CONFIG },
awaitFirstWindow = true,
preventNavigation = false,
): Promise<Page | undefined> => {
const consoleListener = (consoleMessage: ConsoleMessage): void => {
const consoleMethods: Record<string, (...args: unknown[]) => unknown> = {
debug: log.debug.bind(console),
error: log.error.bind(console),
info: log.info.bind(console),
log: log.log.bind(console),
trace: log.trace.bind(console),
warn: log.warn.bind(console),
};
Promise.all(consoleMessage.args().map((x) => x.jsonValue()))
.then((args) => {
if (consoleMessage.type() in consoleMethods) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
consoleMethods[consoleMessage.type()]('window.console', args);
} else {
log.log('window.console', args);
}
})
.catch(() => log.log('window.console', consoleMessage));
};
app = await _electron.launch({
// Workaround for the following errors in some linux distros:
// pw:browser [pid=24716][err] [24718:0100/000000.660708:ERROR:zygote_linux.cc(650)] write: Broken pipe (32) +16ms
// pw:browser [pid=24719][err] [24719:0725/114519.722060:FATAL:setuid_sandbox_host.cc(157)] The SUID sandbox helper binary was found, but is not configured correctly. Rather than run without sandboxing I'm aborting now. You need to make sure that /home/parallels/Dev/nativefier/node_modules/electron/dist/chrome-sandbox is owned by root and has mode 4755. +61ms
args: isLinux()
? ['--no-sandbox', '--disable-setuid-sandbox', appMainJSPath]
: [appMainJSPath],
env: {
LOG_FILE_DIR: logFileDir,
PLAYWRIGHT_TEST: '1',
PLAYWRIGHT_CONFIG: JSON.stringify({
...playwrightConfig,
// disableGpu and process.env.DISPLAY forwarding solve the following errors on Linux:
// pw:browser [pid=286188][err] [286188:0724/102939.938248:ERROR:ozone_platform_x11.cc(248)] Missing X server or $DISPLAY +77ms
// pw:browser [pid=286188][err] [286188:0724/102939.938299:ERROR:env.cc(225)] The platform failed to initialize. Exiting. +2ms
disableGpu: isLinux() ? true : undefined,
processEnvs:
isLinux() && process.env.DISPLAY
? JSON.stringify({ DISPLAY: process.env.DISPLAY })
: undefined,
} as NativefierOptions),
USE_LOG_FILE: '1',
VERBOSE: '1',
},
timeout: 60000,
});
app.on('window', (page: Page) => {
page.on('console', consoleListener);
if (preventNavigation) {
// Prevent page navigation so we can have a reliable test
page
.route('*', (route): void => {
log.info(`Preventing route: ${route.request().url()}`);
route.abort().catch((error) => {
log.error('ERROR', error);
});
})
.catch((error) => {
log.error('ERROR', error);
});
}
});
app.on('close', () => (appClosed = true));
appClosed = false;
if (!awaitFirstWindow) {
return undefined;
}
const window = await app.firstWindow();
// Wait for our initial page to finish loading, otherwise some tests will break
let waited = 0;
while (
window.url() === 'about:blank' &&
playwrightConfig.targetUrl !== 'about:blank' &&
waited < 2000
) {
waited += 100;
await sleep(100);
}
return window;
};
beforeEach(() => {
nukeInjects();
nukeLogs(logFileDir);
});
afterEach(async () => {
if (app && !appClosed) {
await app.close();
}
if (process.env.DEBUG) {
showLogs(logFileDir);
}
});
test('shows an initial window', async () => {
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
expect(app.windows()).toHaveLength(1);
expect(await mainWindow.title()).toBe('npm');
});
test('can inject some CSS', async () => {
const fuschia = 'rgb(255, 0, 255)';
createInject(
'inject.css',
`* { background-color: ${fuschia} !important; }`,
);
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
const headerStyle = await mainWindow.$eval('header', (el) =>
window.getComputedStyle(el),
);
expect(headerStyle.backgroundColor).toBe(fuschia);
await mainWindow.click('#nav-pricing-link');
await mainWindow.waitForLoadState('domcontentloaded');
const headerStylePostNavigate = await mainWindow.$eval('header', (el) =>
window.getComputedStyle(el),
);
expect(headerStylePostNavigate.backgroundColor).toBe(fuschia);
});
test('can inject some JS', async () => {
const alertMsg = 'hello world from inject';
createInject(
'inject.js',
`setTimeout(() => {alert("${alertMsg}"); }, 5000);`, // Buy ourselves 5 seconds to get the dialog handler setup
);
const mainWindow = (await spawnApp(
{ ...DEFAULT_CONFIG },
true,
true,
)) as Page;
const [dialogPromise] = (await once(
mainWindow,
'dialog',
)) as unknown as Promise<Dialog>[];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const dialog: Dialog = await dialogPromise;
await dialog.dismiss();
expect(dialog.message()).toBe(alertMsg);
});
test('can open internal links', async () => {
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
await mainWindow.click('#nav-pricing-link');
await mainWindow.waitForLoadState('domcontentloaded');
expect(app.windows()).toHaveLength(1);
});
test('tries to open external links', async () => {
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
// Install the mock first
await app.evaluate(({ shell }: { shell: Shell }) => {
// @ts-expect-error injecting into shell so that this promise
// can be accessed outside of this anonymous function's scope
// Not my favorite thing to do, but I could not find another way
process.openExternalPromise = new Promise((resolve) => {
shell.openExternal = async (url: string): Promise<void> => {
resolve(url);
return Promise.resolve();
};
});
});
// Click, but don't await it - Playwright waits for stuff that does not happen when Electron does openExternal.
mainWindow
.click('#footer > div:nth-child(2) > ul > li:nth-child(2) > a')
.catch((err: unknown) => {
expect(err).toBeUndefined();
});
// Go pull out our value returned by our hacky global promise
const openExternalUrl = await app.evaluate('process.openExternalPromise');
expect(openExternalUrl).not.toBe('https://www.npmjs.com/');
expect(openExternalUrl).not.toBe(DEFAULT_CONFIG.targetUrl);
});
// Currently disabled. Playwright doesn't seem to support app keypress events for menu shortcuts.
// Will enable when https://github.com/microsoft/playwright/issues/8004 is resolved.
test.skip('keyboard shortcuts: zoom', async () => {
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
const defaultZoom: number | undefined = await app.evaluate(
({ BrowserWindow }) =>
BrowserWindow.getFocusedWindow()?.webContents?.zoomFactor,
);
expect(defaultZoom).toBeDefined();
await mainWindow.keyboard.press(`${metaOrCtrl}+Equal`);
const postZoomIn = await app.evaluate(
({ BrowserWindow }): number | undefined =>
BrowserWindow.getFocusedWindow()?.webContents?.zoomFactor,
);
expect(postZoomIn).toBeGreaterThan(defaultZoom as number);
await mainWindow.keyboard.press(`${metaOrCtrl}+0`);
const postZoomReset = await app.evaluate(
({ BrowserWindow }): number | undefined =>
BrowserWindow.getFocusedWindow()?.webContents?.zoomFactor,
);
expect(postZoomReset).toEqual(defaultZoom);
await mainWindow.keyboard.press(`${metaOrCtrl}+Minus`);
const postZoomOut: number | undefined = await app.evaluate(
({ BrowserWindow }) =>
BrowserWindow.getFocusedWindow()?.webContents?.zoomFactor,
);
expect(postZoomOut).toBeLessThan(defaultZoom as number);
});
// Currently disabled. Playwright doesn't seem to support app keypress events for menu shortcuts.
// Will enable when https://github.com/microsoft/playwright/issues/8004 is resolved.
test.skip('keyboard shortcuts: back and forward', async () => {
const mainWindow = (await spawnApp()) as Page;
await mainWindow.waitForLoadState('domcontentloaded');
await Promise.all([
mainWindow.click('#nav-pricing-link'),
mainWindow.waitForNavigation({ waitUntil: 'domcontentloaded' }),
]);
// Go back
// console.log(`${metaOrAlt}+ArrowLeft`);
await mainWindow.keyboard.press(`${metaOrAlt}+ArrowLeft`);
await mainWindow.waitForNavigation({ waitUntil: 'domcontentloaded' });
const backUrl = await mainWindow.evaluate(() => window.location.href);
expect(backUrl).toBe(DEFAULT_CONFIG.targetUrl);
// Go forward
// console.log(`${metaOrAlt}+ArrowRight`);
await mainWindow.keyboard.press(`${metaOrAlt}+ArrowRight`);
await mainWindow.waitForNavigation({ waitUntil: 'domcontentloaded' });
const forwardUrl = await mainWindow.evaluate(() => window.location.href);
expect(forwardUrl).not.toBe(DEFAULT_CONFIG.targetUrl);
});
test('no errors thrown in console', async () => {
await spawnApp({ ...DEFAULT_CONFIG }, false);
const mainWindow = await app.firstWindow();
mainWindow.addListener('console', (consoleMessage: ConsoleMessage) => {
try {
expect(consoleMessage.type()).not.toBe('error');
} catch {
// Do it this way so we'll see the whole message, not just
// expect('error').not.toBe('error')
// which isn't particularly useful
expect({
message: 'console.error called unexpectedly with',
consoleMessage: { ...consoleMessage },
}).toBeUndefined();
}
});
// Give the app 5 seconds to spin up and ensure no errors happened
await new Promise((resolve) => setTimeout(resolve, 5000));
});
test('basic auth', async () => {
const mainWindow = (await spawnApp({
targetUrl: 'https://authenticationtest.com/HTTPAuth/',
basicAuthUsername: 'user',
basicAuthPassword: 'pass',
})) as Page;
await mainWindow.waitForLoadState('networkidle');
const documentText = await mainWindow.evaluate<string>(
'document.documentElement.innerText',
);
expect(documentText).toContain('Success');
expect(documentText).not.toContain('Failure');
});
test('basic auth - bad login', async () => {
const mainWindow = (await spawnApp({
targetUrl: 'https://authenticationtest.com/HTTPAuth/',
basicAuthUsername: 'userbad',
basicAuthPassword: 'passbad',
})) as Page;
await mainWindow.waitForLoadState('networkidle');
const documentText = await mainWindow.evaluate<string>(
'document.documentElement.innerText',
);
expect(documentText).not.toContain('Success');
expect(documentText).toContain('Failure');
});
test('basic auth without pre-providing', async () => {
const mainWindow = (await spawnApp({
targetUrl: 'https://authenticationtest.com/HTTPAuth/',
})) as Page;
await mainWindow.waitForLoadState('load');
// Give the app a few seconds to open the login window
await new Promise((resolve) => setTimeout(resolve, 5000));
const appWindows = app.windows();
expect(appWindows).toHaveLength(2);
const loginWindow = appWindows.filter((x) => x !== mainWindow)[0];
await loginWindow.waitForLoadState('domcontentloaded');
await loginWindow.waitForLoadState('load');
const usernameField = await loginWindow.$('#username-input');
expect(usernameField).not.toBeNull();
await usernameField?.fill('user');
const passwordField = await loginWindow.$('#password-input');
expect(passwordField).not.toBeNull();
await passwordField?.fill('pass');
const submitButton = await loginWindow.$('#submit-form-button');
expect(submitButton).not.toBeNull();
// "Why is this here?" you may be asking yourself.
// Because for some reason, on some linux boxes,
// the click function will not work until this is done.
// Why? I do not have access to the dark incantation
// that would allow me to know such information.
log.log({ submitButton });
await submitButton?.click();
await mainWindow.waitForEvent('load');
const documentText = await mainWindow.evaluate<string>(
'document.documentElement.innerText',
);
expect(documentText).toContain('Success');
expect(documentText).not.toContain('Failure');
});
});
function createInject(filename: string, contents: string): void {
fs.writeFileSync(path.join(INJECT_DIR, filename), contents);
}
function nukeInjects(): void {
if (!fs.existsSync(INJECT_DIR)) {
return;
}
const injected = fs
.readdirSync(INJECT_DIR)
.filter((x) => x !== '_placeholder');
injected.forEach((x) => fs.unlinkSync(path.join(INJECT_DIR, x)));
}
function nukeLogs(logFileDir: string): void {
const logs = fs.readdirSync(logFileDir).filter((x) => x.endsWith('.log'));
logs.forEach((x) => fs.unlinkSync(path.join(logFileDir, x)));
}
function showLogs(logFileDir: string): void {
const logs = fs.readdirSync(logFileDir).filter((x) => x.endsWith('.log'));
for (const logFile of logs) {
log.log(fs.readFileSync(path.join(logFileDir, logFile)).toString());
}
}