Path: blob/main/test/automation/src/playwrightElectron.ts
3520 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as playwright from '@playwright/test';6import type { LaunchOptions } from './code';7import { PlaywrightDriver } from './playwrightDriver';8import { IElectronConfiguration, resolveElectronConfiguration } from './electron';9import { measureAndLog } from './logger';10import { ChildProcess } from 'child_process';1112export async function launch(options: LaunchOptions): Promise<{ electronProcess: ChildProcess; driver: PlaywrightDriver }> {1314// Resolve electron config and update15const { electronPath, args, env } = await resolveElectronConfiguration(options);16args.push('--enable-smoke-test-driver');1718// Launch electron via playwright19const { electron, context, page } = await launchElectron({ electronPath, args, env }, options);20const electronProcess = electron.process();2122return {23electronProcess,24driver: new PlaywrightDriver(electron, context, page, undefined /* no server process */, Promise.resolve() /* Window is open already */, options)25};26}2728async function launchElectron(configuration: IElectronConfiguration, options: LaunchOptions) {29const { logger, tracing, snapshots } = options;3031const playwrightImpl = options.playwright ?? playwright;32const electron = await measureAndLog(() => playwrightImpl._electron.launch({33executablePath: configuration.electronPath,34args: configuration.args,35env: configuration.env as { [key: string]: string },36timeout: 037}), 'playwright-electron#launch', logger);3839let window = electron.windows()[0];40if (!window) {41window = await measureAndLog(() => electron.waitForEvent('window', { timeout: 0 }), 'playwright-electron#firstWindow', logger);42}4344const context = window.context();4546if (tracing) {47try {48await measureAndLog(() => context.tracing.start({ screenshots: true, snapshots }), 'context.tracing.start()', logger);49} catch (error) {50logger.log(`Playwright (Electron): Failed to start playwright tracing (${error})`); // do not fail the build when this fails51}52}5354if (options.verbose) {55electron.on('window', () => logger.log(`Playwright (Electron): electron.on('window')`));56electron.on('close', () => logger.log(`Playwright (Electron): electron.on('close')`));5758context.on('page', () => logger.log(`Playwright (Electron): context.on('page')`));59context.on('requestfailed', e => logger.log(`Playwright (Electron): context.on('requestfailed') [${e.failure()?.errorText} for ${e.url()}]`));6061window.on('dialog', () => logger.log(`Playwright (Electron): window.on('dialog')`));62window.on('domcontentloaded', () => logger.log(`Playwright (Electron): window.on('domcontentloaded')`));63window.on('load', () => logger.log(`Playwright (Electron): window.on('load')`));64window.on('popup', () => logger.log(`Playwright (Electron): window.on('popup')`));65window.on('framenavigated', () => logger.log(`Playwright (Electron): window.on('framenavigated')`));66window.on('requestfailed', e => logger.log(`Playwright (Electron): window.on('requestfailed') [${e.failure()?.errorText} for ${e.url()}]`));67}6869window.on('console', e => logger.log(`Playwright (Electron): window.on('console') [${e.text()}]`));70window.on('pageerror', async (error) => logger.log(`Playwright (Electron) ERROR: page error: ${error}`));71window.on('crash', () => logger.log('Playwright (Electron) ERROR: page crash'));72window.on('close', () => logger.log('Playwright (Electron): page close'));73window.on('response', async (response) => {74if (response.status() >= 400) {75logger.log(`Playwright (Electron) ERROR: HTTP status ${response.status()} for ${response.url()}`);76}77});7879return { electron, context, page: window };80}818283