Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(electron): add app.firstWindow convenience method (#2195)

Pavel Feldman3f8dfed67f3a
diff --git a/packages/playwright-electron/README.md b/packages/playwright-electron/README.md
index c6bcace113..155a783c25 100644
--- a/packages/playwright-electron/README.md
+++ b/packages/playwright-electron/README.md
@@ -82,7 +82,7 @@ describe('Sanity checks', function () {
it('sanity checks', async () => {
// Wait for the first window to appear.
- const window = await this.app.waitForEvent('window');
+ const window = await this.app.firstWindow();
// Assert window title.
assert.equal(await window.title(), 'Hello World!');
diff --git a/src/server/electron.ts b/src/server/electron.ts
index 4ae0281cce..ff64f71235 100644
--- a/src/server/electron.ts
+++ b/src/server/electron.ts
@@ -83,6 +83,7 @@ export class ElectronApplication extends ExtendedEventEmitter {
this._windows.delete(page);
});
this._windows.add(page);
+ await page.waitForLoadState('domcontentloaded');
this.emit(ElectronEvents.ElectronApplication.Window, page);
}
@@ -90,6 +91,12 @@ export class ElectronApplication extends ExtendedEventEmitter {
return [...this._windows];
}
+ async firstWindow(): Promise<Page> {
+ if (this._windows.size)
+ return this._windows.values().next().value;
+ return this.waitForEvent('window');
+ }
+
async newBrowserWindow(options: any): Promise<Page> {
const windowId = await this.evaluate(async ({ BrowserWindow }, options) => {
const win = new BrowserWindow(options);
diff --git a/test/electron/electron.spec.js b/test/electron/electron.spec.js
index b1acc3c8a4..fc8257aeb1 100644
--- a/test/electron/electron.spec.js
+++ b/test/electron/electron.spec.js
@@ -24,6 +24,7 @@ describe('Electron', function() {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
state.application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
+ // This is for our own extensive protocol logging, customers don't need it.
logger: {
isEnabled: (name, severity) => {
return name === 'browser' ||
@@ -44,6 +45,7 @@ describe('Electron', function() {
});
afterEach(async (state, testRun) => {
await state.application.close();
+ // This is for our own extensive protocol logging, customers don't need it.
if (config.dumpProtocolOnFailure) {
if (testRun.ok())
testRun.output().splice(0);
@@ -115,9 +117,17 @@ describe('Electron', function() {
await page.goto('data:text/html,<script>window.result = add(20, 22);</script>');
expect(await page.evaluate(() => result)).toBe(42);
});
+ it('should wait for first window', async ({ application }) => {
+ application.evaluate(({ BrowserWindow }) => {
+ const window = new BrowserWindow({ width: 800, height: 600 });
+ window.loadURL('data:text/html,<title>Hello World!</title>');
+ });
+ const window = await application.firstWindow();
+ expect(await window.title()).toBe('Hello World!');
+ });
});
-describe('Electron window', function() {
+describe('Electron per window', function() {
beforeAll(async state => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
state.application = await playwright.electron.launch(electronPath, {