/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as os from 'os'; import * as cp from 'child_process'; import { IElement, ILocalizedStrings, ILocaleInfo } from './driver'; import { launch as launchPlaywrightBrowser } from './playwrightBrowser'; import { launch as launchPlaywrightElectron } from './playwrightElectron'; import { Logger, measureAndLog } from './logger'; import * as treekill from 'tree-kill'; import { teardown } from './processes'; import { PlaywrightDriver } from './playwrightDriver'; export interface LaunchOptions { codePath?: string; readonly workspacePath: string; userDataDir: string; readonly extensionsPath: string; readonly logger: Logger; logsPath: string; readonly verbose?: boolean; readonly extraArgs?: string[]; readonly remote?: boolean; readonly web?: boolean; readonly tracing?: boolean; readonly headless?: boolean; readonly browser?: 'chromium' | 'webkit' | 'firefox'; } interface ICodeInstance { kill: () => Promise; } const instances = new Set(); function registerInstance(process: cp.ChildProcess, logger: Logger, type: string) { const instance = { kill: () => teardown(process, logger) }; instances.add(instance); process.stdout?.on('data', data => logger.log(`[${type}] stdout: ${data}`)); process.stderr?.on('data', error => logger.log(`[${type}] stderr: ${error}`)); process.once('exit', (code, signal) => { logger.log(`[${type}] Process terminated (pid: ${process.pid}, code: ${code}, signal: ${signal})`); instances.delete(instance); }); } async function teardownAll(signal?: number) { stopped = true; for (const instance of instances) { await instance.kill(); } if (typeof signal === 'number') { process.exit(signal); } } let stopped = false; process.on('exit', () => teardownAll()); process.on('SIGINT', () => teardownAll(128 + 2)); // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events process.on('SIGTERM', () => teardownAll(128 + 15)); // same as above export async function launch(options: LaunchOptions): Promise { if (stopped) { throw new Error('Smoke test process has terminated, refusing to spawn Code'); } // Browser smoke tests if (options.web) { const { serverProcess, driver } = await measureAndLog(launchPlaywrightBrowser(options), 'launch playwright (browser)', options.logger); registerInstance(serverProcess, options.logger, 'server'); return new Code(driver, options.logger, serverProcess); } // Electron smoke tests (playwright) else { const { electronProcess, driver } = await measureAndLog(launchPlaywrightElectron(options), 'launch playwright (electron)', options.logger); registerInstance(electronProcess, options.logger, 'electron'); return new Code(driver, options.logger, electronProcess); } } export class Code { readonly driver: PlaywrightDriver; constructor( driver: PlaywrightDriver, readonly logger: Logger, private readonly mainProcess: cp.ChildProcess ) { this.driver = new Proxy(driver, { get(target, prop) { if (typeof prop === 'symbol') { throw new Error('Invalid usage'); } const targetProp = (target as any)[prop]; if (typeof targetProp !== 'function') { return targetProp; } return function (this: any, ...args: any[]) { logger.log(`${prop}`, ...args.filter(a => typeof a === 'string')); return targetProp.apply(this, args); }; } }); } async startTracing(name: string): Promise { return await this.driver.startTracing(name); } async stopTracing(name: string, persist: boolean): Promise { return await this.driver.stopTracing(name, persist); } async dispatchKeybinding(keybinding: string): Promise { await this.driver.dispatchKeybinding(keybinding); } async exit(): Promise { return measureAndLog(new Promise((resolve, reject) => { const pid = this.mainProcess.pid!; let done = false; // Start the exit flow via driver this.driver.exitApplication(); // Await the exit of the application (async () => { let retries = 0; while (!done) { retries++; if (retries === 20) { this.logger.log('Smoke test exit call did not terminate process after 10s, forcefully exiting the application...'); // no need to await since we're polling for the process to die anyways treekill(pid, err => { try { process.kill(pid, 0); // throws an exception if the process doesn't exist anymore this.logger.log('Failed to kill Electron process tree:', err?.message); } catch (error) { // Expected when process is gone } }); } if (retries === 40) { done = true; reject(new Error('Smoke test exit call did not terminate process after 20s, giving up')); } try { process.kill(pid, 0); // throws an exception if the process doesn't exist anymore. await new Promise(resolve => setTimeout(resolve, 500)); } catch (error) { done = true; resolve(); } } })(); }), 'Code#exit()', this.logger); } async waitForTextContent(selector: string, textContent?: string, accept?: (result: string) => boolean, retryCount?: number): Promise { accept = accept || (result => textContent !== undefined ? textContent === result : !!result); // {{SQL CARBON EDIT}} Print out found element return await poll( () => this.driver.getElements(windowId, selector).then(els => els.length > 0 ? Promise.resolve(els[0].textContent) : Promise.reject(new Error('Element not found for textContent'))), s => accept!(typeof s.textContent === 'string' ? s.textContent : ''), `get text content '${selector}'`, retryCount ); this.logger.log(`got text content element ${JSON.stringify(element)}`); return element.textContent; } async waitAndClick(selector: string, xoffset?: number, yoffset?: number, retryCount: number = 200): Promise { await this.poll(() => this.driver.click(selector, xoffset, yoffset), () => true, `click '${selector}'`, retryCount); } async waitForSetValue(selector: string, value: string): Promise { await this.poll(() => this.driver.setValue(selector, value), () => true, `set value '${selector}'`); } async waitForElements(selector: string, recursive: boolean, accept: (result: IElement[]) => boolean = result => result.length > 0): Promise { // {{SQL CARBON EDIT}} Print out found element return await poll(() => this.driver.getElements(windowId, selector, recursive), accept, this.logger, `get elements '${selector}'`); this.logger.log(`got elements ${elements.map(element => JSON.stringify(element)).join('\n')}`); return elements; } async waitForElement(selector: string, accept: (result: IElement | undefined) => boolean = result => !!result, retryCount: number = 200): Promise { // {{SQL CARBON EDIT}} Print out found element const element = await this.poll(() => this.driver.getElements(selector).then(els => els[0]), accept, `get element '${selector}'`, retryCount); this.logger.log(`got element ${JSON.stringify(element)}`); return element; } async waitForActiveElement(selector: string, retryCount: number = 200): Promise { await this.poll(() => this.driver.isActiveElement(selector), r => r, `is active element '${selector}'`, retryCount); } async waitForTitle(accept: (title: string) => boolean): Promise { await this.poll(() => this.driver.getTitle(), accept, `get title`); } async waitForTypeInEditor(selector: string, text: string): Promise { await this.poll(() => this.driver.typeInEditor(selector, text), () => true, `type in editor '${selector}'`); } async waitForTerminalBuffer(selector: string, accept: (result: string[]) => boolean): Promise { await this.poll(() => this.driver.getTerminalBuffer(selector), accept, `get terminal buffer '${selector}'`); } async writeInTerminal(selector: string, value: string): Promise { await this.poll(() => this.driver.writeInTerminal(selector, value), () => true, `writeInTerminal '${selector}'`); } async getLocaleInfo(): Promise { return this.driver.getLocaleInfo(); } async getLocalizedStrings(): Promise { return this.driver.getLocalizedStrings(); } private async poll( fn: () => Promise, acceptFn: (result: T) => boolean, timeoutMessage: string, retryCount = 200, retryInterval = 100 // millis ): Promise { let trial = 1; let lastError: string = ''; while (true) { if (trial > retryCount) { this.logger.log('Timeout!'); this.logger.log(lastError); this.logger.log(`Timeout: ${timeoutMessage} after ${(retryCount * retryInterval) / 1000} seconds.`); throw new Error(`Timeout: ${timeoutMessage} after ${(retryCount * retryInterval) / 1000} seconds.`); } let result; try { result = await fn(); if (acceptFn(result)) { return result; } else { lastError = 'Did not pass accept function'; } } catch (e: any) { lastError = Array.isArray(e.stack) ? e.stack.join(os.EOL) : e.stack; } await new Promise(resolve => setTimeout(resolve, retryInterval)); trial++; } } } export function findElement(element: IElement, fn: (element: IElement) => boolean): IElement | null { const queue = [element]; while (queue.length > 0) { const element = queue.shift()!; if (fn(element)) { return element; } queue.push(...element.children); } return null; } export function findElements(element: IElement, fn: (element: IElement) => boolean): IElement[] { const result: IElement[] = []; const queue = [element]; while (queue.length > 0) { const element = queue.shift()!; if (fn(element)) { result.push(element); } queue.push(...element.children); } return result; }