| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 22:45:06 -07:00
										 |  |  | import type { BrowserWindow } from 'electron'; | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | import path from 'path'; | 
					
						
							| 
									
										
										
										
											2021-12-08 17:34:50 -08:00
										 |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2021-05-08 17:45:04 -07:00
										 |  |  | import { electronTest as test, expect } from './electronTest'; | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should fire close event', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   const electronApp = await launchElectronApp('electron-app.js'); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  |   const events = []; | 
					
						
							|  |  |  |   electronApp.on('close', () => events.push('application')); | 
					
						
							|  |  |  |   electronApp.context().on('close', () => events.push('context')); | 
					
						
							|  |  |  |   await electronApp.close(); | 
					
						
							|  |  |  |   expect(events.join('|')).toBe('context|application'); | 
					
						
							|  |  |  |   // Give it some time to fire more events - there should not be any.
 | 
					
						
							|  |  |  |   await new Promise(f => setTimeout(f, 1000)); | 
					
						
							|  |  |  |   expect(events.join('|')).toBe('context|application'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | 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', | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2022-11-21 15:13:53 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | test('should script application', async ({ electronApp }) => { | 
					
						
							|  |  |  |   const appPath = await electronApp.evaluate(async ({ app }) => app.getAppPath()); | 
					
						
							| 
									
										
										
										
											2021-05-08 17:45:04 -07:00
										 |  |  |   expect(appPath).toBe(path.resolve(__dirname)); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 10:48:24 -08:00
										 |  |  | test('should preserve args', async ({ electronApp }) => { | 
					
						
							| 
									
										
										
										
											2022-11-18 13:44:42 -10:00
										 |  |  |   const argv = await electronApp.evaluate(async () => process.argv); | 
					
						
							|  |  |  |   expect(argv.slice(1)).toEqual([expect.stringContaining(path.join('electron', 'electron-app.js'))]); | 
					
						
							| 
									
										
										
										
											2022-11-15 10:48:24 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  | 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', (route, request) => { | 
					
						
							|  |  |  |     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 }); | 
					
						
							|  |  |  |     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'; | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await electronApp.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), clipboardContentToWrite); | 
					
						
							|  |  |  |   const clipboardContentRead = await electronApp.evaluate(async ({ clipboard }) => clipboard.readText()); | 
					
						
							| 
									
										
										
										
											2021-04-02 09:00:49 -07:00
										 |  |  |   expect(clipboardContentRead).toEqual(clipboardContentToWrite); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-02 22:45:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should test app that opens window fast', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   await launchElectronApp('electron-window-app.js'); | 
					
						
							| 
									
										
										
										
											2021-05-02 22:45:06 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should return browser window', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   const electronApp = await launchElectronApp('electron-window-app.js'); | 
					
						
							| 
									
										
										
										
											2021-05-13 14:10:52 -07:00
										 |  |  |   const page = await electronApp.firstWindow(); | 
					
						
							| 
									
										
										
										
											2021-05-02 22:45:06 -07:00
										 |  |  |   const bwHandle = await electronApp.browserWindow(page); | 
					
						
							|  |  |  |   expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-05-19 06:56:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should bypass csp', async ({ launchElectronApp, server }) => { | 
					
						
							|  |  |  |   const app = await launchElectronApp('electron-app.js', { bypassCSP: true }); | 
					
						
							| 
									
										
										
										
											2021-05-19 06:56:29 -07:00
										 |  |  |   await app.evaluate(electron => { | 
					
						
							|  |  |  |     const window = new electron.BrowserWindow({ | 
					
						
							|  |  |  |       width: 800, | 
					
						
							|  |  |  |       height: 600, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     window.loadURL('about:blank'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const page = await app.firstWindow(); | 
					
						
							|  |  |  |   await page.goto(server.PREFIX + '/csp.html'); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await page.addScriptTag({ content: 'window["__injected"] = 42;' }); | 
					
						
							| 
									
										
										
										
											2021-05-19 06:56:29 -07:00
										 |  |  |   expect(await page.evaluate('window["__injected"]')).toBe(42); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-10-04 16:19:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should create page for browser view', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   const app = await launchElectronApp('electron-window-app.js'); | 
					
						
							| 
									
										
										
										
											2022-11-22 11:50:35 -08:00
										 |  |  |   await app.firstWindow(); | 
					
						
							| 
									
										
										
										
											2021-10-04 16:19:57 -04:00
										 |  |  |   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 }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-11-14 22:01:54 -08:00
										 |  |  |   await expect.poll(() => app.windows().length).toBe(2); | 
					
						
							| 
									
										
										
										
											2021-10-04 16:19:57 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should return same browser window for browser view pages', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   const app = await launchElectronApp('electron-window-app.js'); | 
					
						
							| 
									
										
										
										
											2022-11-22 11:50:35 -08:00
										 |  |  |   await app.firstWindow(); | 
					
						
							| 
									
										
										
										
											2021-10-04 16:19:57 -04:00
										 |  |  |   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 }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-11-14 22:01:54 -08:00
										 |  |  |   await expect.poll(() => app.windows().length).toBe(2); | 
					
						
							| 
									
										
										
										
											2021-10-04 16:19:57 -04:00
										 |  |  |   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); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-12-08 17:34:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should record video', async ({ launchElectronApp }, testInfo) => { | 
					
						
							|  |  |  |   const app = await launchElectronApp('electron-window-app.js', { | 
					
						
							| 
									
										
										
										
											2021-12-08 17:34:50 -08:00
										 |  |  |     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); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-04-04 10:50:46 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should be able to get the first window when with a delayed navigation', async ({ launchElectronApp }) => { | 
					
						
							| 
									
										
										
										
											2022-10-14 17:36:37 +02:00
										 |  |  |   test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/17765' }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  |   const app = await launchElectronApp('electron-window-app-delayed-loadURL.js'); | 
					
						
							| 
									
										
										
										
											2022-10-14 17:36:37 +02:00
										 |  |  |   const page = await app.firstWindow(); | 
					
						
							|  |  |  |   await expect(page).toHaveURL('data:text/html,<h1>Foobar</h1>'); | 
					
						
							|  |  |  |   await expect(page.locator('h1')).toHaveText('Foobar'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | test('should detach debugger on app-initiated exit', async ({ launchElectronApp }) => { | 
					
						
							|  |  |  |   const electronApp = await launchElectronApp('electron-app.js'); | 
					
						
							| 
									
										
										
										
											2022-04-04 10:50:46 -08:00
										 |  |  |   const closePromise = new Promise(f => electronApp.process().on('close', f)); | 
					
						
							|  |  |  |   await electronApp.evaluate(({ app }) => { | 
					
						
							|  |  |  |     app.quit(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await closePromise; | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('should run pre-ready apis', async ({ launchElectronApp }) => { | 
					
						
							| 
									
										
										
										
											2022-12-22 17:28:08 -08:00
										 |  |  |   await launchElectronApp('electron-app-pre-ready.js'); | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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)); | 
					
						
							| 
									
										
										
										
											2022-12-22 17:28:08 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-12-20 12:50:53 -08:00
										 |  |  | }); |