Files
azuredatastudio/test/automation/src/code.ts

310 lines
9.9 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* 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<void>;
}
const instances = new Set<ICodeInstance>();
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<Code> {
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<void> {
return await this.driver.startTracing(name);
}
async stopTracing(name: string, persist: boolean): Promise<void> {
return await this.driver.stopTracing(name, persist);
}
async dispatchKeybinding(keybinding: string): Promise<void> {
await this.driver.dispatchKeybinding(keybinding);
}
async exit(): Promise<void> {
return measureAndLog(new Promise<void>((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<string> {
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<void> {
await this.poll(() => this.driver.click(selector, xoffset, yoffset), () => true, `click '${selector}'`, retryCount);
}
async waitForSetValue(selector: string, value: string): Promise<void> {
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<IElement[]> {
// {{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<IElement> {
// {{SQL CARBON EDIT}} Print out found element
const element = await this.poll<IElement>(() => 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<void> {
await this.poll(() => this.driver.isActiveElement(selector), r => r, `is active element '${selector}'`, retryCount);
}
async waitForTitle(accept: (title: string) => boolean): Promise<void> {
await this.poll(() => this.driver.getTitle(), accept, `get title`);
}
async waitForTypeInEditor(selector: string, text: string): Promise<void> {
await this.poll(() => this.driver.typeInEditor(selector, text), () => true, `type in editor '${selector}'`);
}
async waitForTerminalBuffer(selector: string, accept: (result: string[]) => boolean): Promise<void> {
await this.poll(() => this.driver.getTerminalBuffer(selector), accept, `get terminal buffer '${selector}'`);
}
async writeInTerminal(selector: string, value: string): Promise<void> {
await this.poll(() => this.driver.writeInTerminal(selector, value), () => true, `writeInTerminal '${selector}'`);
}
async getLocaleInfo(): Promise<ILocaleInfo> {
return this.driver.getLocaleInfo();
}
async getLocalizedStrings(): Promise<ILocalizedStrings> {
return this.driver.getLocalizedStrings();
}
private async poll<T>(
fn: () => Promise<T>,
acceptFn: (result: T) => boolean,
timeoutMessage: string,
retryCount = 200,
retryInterval = 100 // millis
): Promise<T> {
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;
}