Skip to content

Commit 3f8dfed

Browse files
authored
feat(electron): add app.firstWindow convenience method (#2195)
1 parent fdc9ce8 commit 3f8dfed

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

packages/playwright-electron/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('Sanity checks', function () {
8282

8383
it('sanity checks', async () => {
8484
// Wait for the first window to appear.
85-
const window = await this.app.waitForEvent('window');
85+
const window = await this.app.firstWindow();
8686

8787
// Assert window title.
8888
assert.equal(await window.title(), 'Hello World!');

src/server/electron.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,20 @@ export class ElectronApplication extends ExtendedEventEmitter {
8383
this._windows.delete(page);
8484
});
8585
this._windows.add(page);
86+
await page.waitForLoadState('domcontentloaded');
8687
this.emit(ElectronEvents.ElectronApplication.Window, page);
8788
}
8889

8990
windows(): Page[] {
9091
return [...this._windows];
9192
}
9293

94+
async firstWindow(): Promise<Page> {
95+
if (this._windows.size)
96+
return this._windows.values().next().value;
97+
return this.waitForEvent('window');
98+
}
99+
93100
async newBrowserWindow(options: any): Promise<Page> {
94101
const windowId = await this.evaluate(async ({ BrowserWindow }, options) => {
95102
const win = new BrowserWindow(options);

test/electron/electron.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Electron', function() {
2424
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
2525
state.application = await playwright.electron.launch(electronPath, {
2626
args: [path.join(__dirname, 'testApp.js')],
27+
// This is for our own extensive protocol logging, customers don't need it.
2728
logger: {
2829
isEnabled: (name, severity) => {
2930
return name === 'browser' ||
@@ -44,6 +45,7 @@ describe('Electron', function() {
4445
});
4546
afterEach(async (state, testRun) => {
4647
await state.application.close();
48+
// This is for our own extensive protocol logging, customers don't need it.
4749
if (config.dumpProtocolOnFailure) {
4850
if (testRun.ok())
4951
testRun.output().splice(0);
@@ -115,9 +117,17 @@ describe('Electron', function() {
115117
await page.goto('data:text/html,<script>window.result = add(20, 22);</script>');
116118
expect(await page.evaluate(() => result)).toBe(42);
117119
});
120+
it('should wait for first window', async ({ application }) => {
121+
application.evaluate(({ BrowserWindow }) => {
122+
const window = new BrowserWindow({ width: 800, height: 600 });
123+
window.loadURL('data:text/html,<title>Hello World!</title>');
124+
});
125+
const window = await application.firstWindow();
126+
expect(await window.title()).toBe('Hello World!');
127+
});
118128
});
119129

120-
describe('Electron window', function() {
130+
describe('Electron per window', function() {
121131
beforeAll(async state => {
122132
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
123133
state.application = await playwright.electron.launch(electronPath, {

0 commit comments

Comments
 (0)