๐Ÿ“ฆ microsoft / playwright

๐Ÿ“„ electron-app.spec.ts ยท 342 lines
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/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { BrowserWindow } from 'electron';
import path from 'path';
import fs from 'fs';
import { electronTest as test, expect } from './electronTest';
import type { ConsoleMessage } from 'playwright';

test('should fire close event via ElectronApplication.close();', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const events = [];
  electronApp.on('close', () => events.push('application(close)'));
  electronApp.context().on('close', () => events.push('context(close)'));
  electronApp.process().on('exit', () => events.push('process(exit)'));
  await electronApp.close();
  // Close one more time - this should be a noop.
  await electronApp.close();
  events.sort(); // we don't care about the order
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
  // Give it some time to fire more events - there should not be any.
  await new Promise(f => setTimeout(f, 1000));
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
});

test('should fire close event via BrowserContext.close()', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const events = [];
  electronApp.on('close', () => events.push('application(close)'));
  electronApp.context().on('close', () => events.push('context(close)'));
  electronApp.process().on('exit', () => events.push('process(exit)'));
  await electronApp.context().close();
  // Close one more time - this should be a noop.
  await electronApp.context().close();
  events.sort(); // we don't care about the order
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
  // Give it some time to fire more events - there should not be any.
  await new Promise(f => setTimeout(f, 1000));
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
});

test('should fire close event when the app quits itself', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const events = [];
  electronApp.on('close', () => events.push('application(close)'));
  electronApp.context().on('close', () => events.push('context(close)'));
  electronApp.process().on('exit', () => events.push('process(exit)'));
  {
    const waitForAppClose = new Promise<void>(f => electronApp.on('close', f));
    await electronApp.evaluate(({ app }) => app.quit());
    await waitForAppClose;
  }
  events.sort(); // we don't care about the order
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
  // Give it some time to fire more events - there should not be any.
  await new Promise(f => setTimeout(f, 1000));
  expect(events).toEqual(['application(close)', 'context(close)', 'process(exit)']);
});

test('should fire console events', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const messages = [];
  electronApp.on('console', message => messages.push({ type: message.type(), text: message.text() }));
  await electronApp.evaluate(() => {
    console.log('its type log');
    console.debug('its type debug');
    console.info('its type info');
    console.error('its type error');
    console.warn('its type warn');
  });
  await electronApp.close();
  expect(messages).toEqual([
    { type: 'log', text: 'its type log' },
    { type: 'debug', text: 'its type debug' },
    { type: 'info', text: 'its type info' },
    { type: 'error', text: 'its type error' },
    { type: 'warning', text: 'its type warn' },
  ]);
});

test('should fire console events with handles and complex objects', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const messages: ConsoleMessage[] = [];
  electronApp.on('console', message => messages.push(message));
  await electronApp.evaluate(() => {
    globalThis.complexObject = [{ a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, { a: 7, b: 8, c: 9 }];
    console.log(globalThis.complexObject[0], globalThis.complexObject[1], globalThis.complexObject[2]);
  });
  expect(messages.length).toBe(1);
  const message = messages[0];
  expect(message.text()).toBe('{a: 1, b: 2, c: 3} {a: 4, b: 5, c: 6} {a: 7, b: 8, c: 9}');
  expect(message.args().length).toBe(3);
  expect(await message.args()[0].jsonValue()).toEqual({ a: 1, b: 2, c: 3 });
  expect(await message.args()[0].evaluate(part => part === globalThis.complexObject[0])).toBeTruthy();
  expect(await message.args()[1].jsonValue()).toEqual({ a: 4, b: 5, c: 6 });
  expect(await message.args()[1].evaluate(part => part === globalThis.complexObject[1])).toBeTruthy();
  expect(await message.args()[2].jsonValue()).toEqual({ a: 7, b: 8, c: 9 });
  expect(await message.args()[2].evaluate(part => part === globalThis.complexObject[2])).toBeTruthy();
  await electronApp.close();
});

test('should dispatch ready event', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app-ready-event.js');
  const events = await electronApp.evaluate(() => globalThis.__playwrightLog);
  expect(events).toEqual([
    'isReady == false',
    'will-finish-launching fired',
    'ready fired',
    'whenReady resolved',
    'isReady == true',
  ]);
});

test('should script application', async ({ electronApp }) => {
  const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
  expect(appPath).toBe(path.resolve(__dirname));
});

test('should preserve args', async ({ launchElectronApp, isMac }) => {
  const electronApp = await launchElectronApp('electron-app-args.js', ['foo', 'bar', '& <>^|\\"']);
  const argv = await electronApp.evaluate(async () => globalThis.argv);
  const electronPath = isMac ? path.join('dist', 'Electron.app') : path.join('dist', 'electron');
  expect(argv).toEqual([expect.stringContaining(electronPath), expect.stringContaining(path.join('electron', 'electron-app-args.js')), 'foo', 'bar', '& <>^|\\"']);
});

test('should return windows', async ({ electronApp, newWindow }) => {
  const window = await newWindow();
  expect(electronApp.windows()).toEqual([window]);
});

test('should evaluate handle', async ({ electronApp }) => {
  const appHandle = await electronApp.evaluateHandle(({ app }) => app);
  expect(await electronApp.evaluate(({ app }, appHandle) => app === appHandle, appHandle)).toBeTruthy();
});

test('should route network', async ({ electronApp, newWindow }) => {
  await electronApp.context().route('**/empty.html', async (route, request) => {
    await route.fulfill({
      status: 200,
      contentType: 'text/html',
      body: '<title>Hello World</title>',
    });
  });
  const window = await newWindow();
  await window.goto('https://localhost:1000/empty.html');
  expect(await window.title()).toBe('Hello World');
});

test('should support init script', async ({ electronApp, newWindow }) => {
  await electronApp.context().addInitScript('window.magic = 42;');
  const window = await newWindow();
  await window.goto('data:text/html,<script>window.copy = magic</script>');
  expect(await window.evaluate(() => window['copy'])).toBe(42);
});

test('should expose function', async ({ electronApp, newWindow }) => {
  await electronApp.context().exposeFunction('add', (a, b) => a + b);
  const window = await newWindow();
  await window.goto('data:text/html,<script>window["result"] = add(20, 22);</script>');
  expect(await window.evaluate(() => window['result'])).toBe(42);
});

test('should wait for first window', async ({ electronApp }) => {
  await electronApp.evaluate(({ BrowserWindow }) => {
    const window = new BrowserWindow({ width: 800, height: 600 });
    void window.loadURL('data:text/html,<title>Hello World!</title>');
  });
  const window = await electronApp.firstWindow();
  expect(await window.title()).toBe('Hello World!');
});

test('should have a clipboard instance', async ({ electronApp }) => {
  const clipboardContentToWrite = 'Hello from Playwright';
  await electronApp.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), clipboardContentToWrite);
  const clipboardContentRead = await electronApp.evaluate(async ({ clipboard }) => clipboard.readText());
  expect(clipboardContentRead).toEqual(clipboardContentToWrite);
});

test('should test app that opens window fast', async ({ launchElectronApp }) => {
  await launchElectronApp('electron-window-app.js');
});

test('should return browser window', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-window-app.js');
  const page = await electronApp.firstWindow();
  const bwHandle = await electronApp.browserWindow(page);
  expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron');
});

test('should bypass csp', async ({ launchElectronApp, server }) => {
  const app = await launchElectronApp('electron-app.js', [], { bypassCSP: true });
  await app.evaluate(electron => {
    const window = new electron.BrowserWindow({
      width: 800,
      height: 600,
    });
    void window.loadURL('about:blank');
  });
  const page = await app.firstWindow();
  await page.goto(server.PREFIX + '/csp.html');
  await page.addScriptTag({ content: 'window["__injected"] = 42;' });
  expect(await page.evaluate('window["__injected"]')).toBe(42);
  expect(await page.evaluate('window["__inlineScriptValue"]')).toBe(42);
});

test('should create page for browser view', async ({ launchElectronApp }) => {
  const app = await launchElectronApp('electron-window-app.js');
  await app.firstWindow();
  await app.evaluate(async electron => {
    const window = electron.BrowserWindow.getAllWindows()[0];
    const view = new electron.BrowserView();
    window.addBrowserView(view);
    await view.webContents.loadURL('about:blank');
    view.setBounds({ x: 0, y: 0, width: 256, height: 256 });
  });
  await expect.poll(() => app.windows().length).toBe(2);
});

test('should return same browser window for browser view pages', async ({ launchElectronApp }) => {
  const app = await launchElectronApp('electron-window-app.js');
  await app.firstWindow();
  await app.evaluate(async electron => {
    const window = electron.BrowserWindow.getAllWindows()[0];
    const view = new electron.BrowserView();
    window.addBrowserView(view);
    await view.webContents.loadURL('about:blank');
    view.setBounds({ x: 0, y: 0, width: 256, height: 256 });
  });
  await expect.poll(() => app.windows().length).toBe(2);
  const [firstWindowId, secondWindowId] = await Promise.all(
      app.windows().map(async page => {
        const bwHandle = await app.browserWindow(page);
        const id = await bwHandle.evaluate((bw: BrowserWindow) => bw.id);
        return id;
      })
  );
  expect(firstWindowId).toEqual(secondWindowId);
});

test('should record video', async ({ launchElectronApp }, testInfo) => {
  const app = await launchElectronApp('electron-window-app.js', [], {
    recordVideo: { dir: testInfo.outputPath('video') }
  });
  const page = await app.firstWindow();
  await page.setContent(`<style>body {background:red}</style>`);
  await page.waitForTimeout(1000);
  await app.close();
  const videoPath = await page.video().path();
  expect(fs.statSync(videoPath).size).toBeGreaterThan(0);
});

test('should record har', async ({ launchElectronApp, server }, testInfo) => {
  test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/30747' });
  const app = await launchElectronApp('electron-window-app.js', [], {
    recordHar: { path: testInfo.outputPath('har.zip') }
  });
  const page = await app.firstWindow();
  await page.goto(server.EMPTY_PAGE);
  await app.close();
  expect(fs.existsSync(testInfo.outputPath('har.zip'))).toBeTruthy();
  expect(fs.statSync(testInfo.outputPath('har.zip')).size).toBeGreaterThan(0);
});

test('should be able to get the first window when with a delayed navigation', async ({ launchElectronApp }) => {
  test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/17765' });

  const app = await launchElectronApp('electron-window-app-delayed-loadURL.js');
  const page = await app.firstWindow();
  await expect(page).toHaveURL('data:text/html,<h1>Foobar</h1>');
  await expect(page.locator('h1')).toHaveText('Foobar');
});

test('should detach debugger on app-initiated exit', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('electron-app.js');
  const closePromise = new Promise(f => electronApp.process().on('close', f));
  await electronApp.evaluate(({ app }) => {
    app.quit();
  });
  await closePromise;
});

test('should run pre-ready apis', async ({ launchElectronApp }) => {
  await launchElectronApp('electron-app-pre-ready.js');
});

test('should resolve app path for folder apps', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('.');
  const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath());
  expect(appPath).toBe(path.resolve(__dirname));
});

test('should return app name / version from manifest', async ({ launchElectronApp }) => {
  const electronApp = await launchElectronApp('.');
  const data = await electronApp.evaluate(async ({ app }) => {
    return {
      name: app.getName(),
      version: app.getVersion(),
    };
  });
  expect(data).toEqual({
    name: 'my-electron-app',
    version: '1.0.0'
  });
});

test('should report downloads', async ({ launchElectronApp, electronMajorVersion, server }) => {
  test.skip(electronMajorVersion < 30, 'Depends on https://github.com/electron/electron/pull/41718');

  server.setRoute('/download', (req, res) => {
    res.setHeader('Content-Type', 'application/octet-stream');
    res.setHeader('Content-Disposition', 'attachment');
    res.end(`Hello world`);
  });

  const app = await launchElectronApp('electron-window-app.js', [], {
    acceptDownloads: true,
  });
  const window = await app.firstWindow();
  await window.setContent(`<a href="${server.PREFIX}/download">download</a>`);
  const [download] = await Promise.all([
    window.waitForEvent('download'),
    window.click('a')
  ]);
  const path = await download.path();
  expect(fs.existsSync(path)).toBeTruthy();
  expect(fs.readFileSync(path).toString()).toBe('Hello world');
  await app.close();
});