Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/playwright-core/src/client/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class Electron extends ChannelOwner<channels.ElectronChannel> implements
};
const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
this._playwright.selectors._contextsForSelectors.add(app._context);
app.once(Events.ElectronApplication.Close, () => this._playwright.selectors._contextsForSelectors.delete(app._context));
this._playwright._electronApps.add(app);
app.once(Events.ElectronApplication.Close, () => {
this._playwright.selectors._contextsForSelectors.delete(app._context);
this._playwright._electronApps.delete(app);
});
await app._context._initializeHarFromOptions(options.recordHar);
app._context.tracing._tracesDir = options.tracesDir;
return app;
Expand Down
6 changes: 5 additions & 1 deletion packages/playwright-core/src/client/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { TimeoutError } from './errors';
import { APIRequest } from './fetch';
import { Selectors } from './selectors';

import type { ElectronApplication } from './electron';
import type * as channels from '@protocol/channels';
import type { LaunchOptions } from 'playwright-core';

Expand All @@ -41,6 +42,7 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
_defaultLaunchOptions?: LaunchOptions;
_defaultContextTimeout?: number;
_defaultContextNavigationTimeout?: number;
readonly _electronApps = new Set<ElectronApplication>();

constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) {
super(parent, type, guid, initializer);
Expand Down Expand Up @@ -75,7 +77,9 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
}

_allContexts() {
return this._browserTypes().flatMap(type => [...type._contexts]);
const browserContexts = this._browserTypes().flatMap(type => [...type._contexts]);
const electronContexts = [...this._electronApps].map(app => app._context);
return [...browserContexts, ...electronContexts];
}

_allPages() {
Expand Down
27 changes: 27 additions & 0 deletions tests/electron/electron-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,30 @@ test('should save downloads to artifactsDir', async ({ launchElectronApp, electr
// User-provided artifactsDir should not be cleaned up.
expect(fs.existsSync(artifactsDir)).toBeTruthy();
});

test('should include electron pages in playwright._allPages()', async ({ playwright, electronApp, newWindow }) => {
const window = await newWindow();
await window.setContent('<h1>Hello Electron</h1>');
const allPages = (playwright as any)._allPages();
expect(allPages).toContain(window);
});

test('should include electron context in playwright._allContexts()', async ({ playwright, electronApp }) => {
const allContexts = (playwright as any)._allContexts();
expect(allContexts).toContain(electronApp.context());
});

test('should remove electron context from tracking on close', async ({ playwright, launchElectronApp }) => {
const app = await launchElectronApp('electron-app.js');
const context = app.context();
expect((playwright as any)._allContexts()).toContain(context);
await app.close();
expect((playwright as any)._allContexts()).not.toContain(context);
});

test('should take screenshot of electron window', async ({ electronApp, newWindow }) => {
const window = await newWindow();
await window.setContent('<h1 style="color: red;">Screenshot Test</h1>');
const screenshot = await window.screenshot();
expect(screenshot.byteLength).toBeGreaterThan(0);
});
27 changes: 27 additions & 0 deletions tests/playwright-test/playwright.artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,30 @@ test('should take screenshot when page is closed in afterEach', async ({ runInli
expect(result.failed).toBe(1);
expect(fs.existsSync(testInfo.outputPath('test-results', 'a-fails', 'test-failed-1.png'))).toBeTruthy();
});

test('should work with screenshot: only-on-failure for electron', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'electron.spec.ts': `
import { test, expect, _electron } from '@playwright/test';

test('should fail and capture electron screenshot', async () => {
const electronApp = await _electron.launch({ args: [${JSON.stringify(path.join(__dirname, '..', 'electron', 'electron-window-app.js'))}] });
const window = await electronApp.firstWindow();
await window.setContent('<h1>Electron Screenshot Test</h1>');
expect(1).toBe(2);
});
`,
'playwright.config.ts': `
module.exports = { use: { screenshot: 'only-on-failure' } };
`,
}, { workers: 1 }, { PLAYWRIGHT_NO_COPY_PROMPT: 'true' });

expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(listFiles(testInfo.outputPath('test-results'))).toEqual([
'.last-run.json',
'electron-should-fail-and-capture-electron-screenshot',
' error-context.md',
' test-failed-1.png',
]);
});