fix(electron): handle in-event await errors (#2207)
This commit is contained in:
parent
ebceaf4328
commit
414ae0025e
|
|
@ -58,7 +58,6 @@ app.whenReady().then(createWindow);
|
||||||
|
|
||||||
`test/spec.js` - test file
|
`test/spec.js` - test file
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const { electron } = require('playwright-electron');
|
const { electron } = require('playwright-electron');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const electronPath = require('electron');
|
const electronPath = require('electron');
|
||||||
|
|
@ -80,25 +79,80 @@ describe('Sanity checks', function () {
|
||||||
await this.app.close();
|
await this.app.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sanity checks', async () => {
|
it('script application', async () => {
|
||||||
// Wait for the first window to appear.
|
const appPath = await this.app.evaluate(async ({ app }) => {
|
||||||
const window = await this.app.firstWindow();
|
// This runs in the main Electron process, first parameter is
|
||||||
|
// the result of the require('electron') in the main app script.
|
||||||
|
return app.getAppPath();
|
||||||
|
});
|
||||||
|
assert.equal(appPath, path.join(__dirname, '..'));
|
||||||
|
});
|
||||||
|
|
||||||
// Assert window title.
|
it('window title', async () => {
|
||||||
assert.equal(await window.title(), 'Hello World!');
|
// Return value of this.app.firstWindow a Playwright Page.
|
||||||
|
// See https://playwright.dev/#path=docs%2Fapi.md&q=class-page.
|
||||||
|
|
||||||
|
// Get a Playwright page for the first Electron window.
|
||||||
|
// It awaits for the page to be available. Alternatively use
|
||||||
|
// this.app.windows() or this.app.waitForEvent('window').
|
||||||
|
const page = await this.app.firstWindow();
|
||||||
|
assert.equal(await page.title(), 'Hello World!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('capture screenshot', async () => {
|
||||||
|
const page = await this.app.firstWindow();
|
||||||
|
|
||||||
// Capture window screenshot.
|
// Capture window screenshot.
|
||||||
await window.screenshot({ path: 'intro.png' });
|
await page.screenshot({ path: 'intro.png' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sniff console', async () => {
|
||||||
|
const page = await this.app.firstWindow();
|
||||||
|
|
||||||
// Collect console logs.
|
// Collect console logs.
|
||||||
let consoleText;
|
let consoleText;
|
||||||
window.on('console', message => consoleText = message.text());
|
page.on('console', message => consoleText = message.text());
|
||||||
|
|
||||||
// Click button.
|
// Click button.
|
||||||
await window.click('text=Click me');
|
await page.click('text=Click me');
|
||||||
|
|
||||||
// Check that click produced console message.
|
// Check that click produced console message.
|
||||||
assert.equal(consoleText, 'click');
|
assert.equal(consoleText, 'click');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('intercept network', async () => {
|
||||||
|
await this.app.firstWindow();
|
||||||
|
|
||||||
|
// Return value of this.app.context() is a Playwright BrowserContext.
|
||||||
|
// See https://playwright.dev/#path=docs%2Fapi.md&q=class-browsercontext.
|
||||||
|
|
||||||
|
await await this.app.context().route('**/empty.html', (route, request) => {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'text/html',
|
||||||
|
body: '<title>Hello World</title>',
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
// Helper method to create BrowserWindow.
|
||||||
|
const page = await this.app.newBrowserWindow({ width: 800, height: 600 });
|
||||||
|
await page.goto('https://localhost:1000/empty.html');
|
||||||
|
|
||||||
|
assert.equal(await page.title(), 'Hello World');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should maximize window', async () => {
|
||||||
|
await this.app.firstWindow();
|
||||||
|
|
||||||
|
const page = await this.app.newBrowserWindow({ width: 800, height: 600 });
|
||||||
|
// page.browserWindow is a Playwright JSHandle pointing at Electron's
|
||||||
|
// BrowserWindow.
|
||||||
|
// https://playwright.dev/#path=docs%2Fapi.md&q=class-jshandle
|
||||||
|
await page.browserWindow.evaluate(browserWindow => browserWindow.maximize());
|
||||||
|
assert(await page.browserWindow.evaluate(browserWindow => browserWindow.isMaximized()));
|
||||||
|
await page.browserWindow.evaluate(browserWindow => browserWindow.unmaximize());
|
||||||
|
assert(!(await page.browserWindow.evaluate(browserWindow => browserWindow.isMaximized())));
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,9 @@ export class ElectronApplication extends ExtendedEventEmitter {
|
||||||
// Needs to be sync.
|
// Needs to be sync.
|
||||||
const windowId = ++this._lastWindowId;
|
const windowId = ++this._lastWindowId;
|
||||||
// Can be async.
|
// Can be async.
|
||||||
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId);
|
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
|
||||||
|
if (!handle)
|
||||||
|
return;
|
||||||
(page as any).browserWindow = handle;
|
(page as any).browserWindow = handle;
|
||||||
(page as any)._browserWindowId = windowId;
|
(page as any)._browserWindowId = windowId;
|
||||||
page.on(Events.Page.Close, () => {
|
page.on(Events.Page.Close, () => {
|
||||||
|
|
@ -83,7 +85,7 @@ export class ElectronApplication extends ExtendedEventEmitter {
|
||||||
this._windows.delete(page);
|
this._windows.delete(page);
|
||||||
});
|
});
|
||||||
this._windows.add(page);
|
this._windows.add(page);
|
||||||
await page.waitForLoadState('domcontentloaded');
|
await page.waitForLoadState('domcontentloaded').catch(e => {}); // can happen after detach
|
||||||
this.emit(ElectronEvents.ElectronApplication.Window, page);
|
this.emit(ElectronEvents.ElectronApplication.Window, page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue