Files

90 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2019-11-09 14:12:46 -08:00
/**
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
* @license MIT
*/
2023-08-27 08:08:42 -07:00
import * as playwright from '@playwright/test';
2019-11-09 17:29:32 -08:00
import deepEqual = require('deep-equal');
2023-11-01 06:50:08 -07:00
import { ITerminalInitOnlyOptions, ITerminalOptions } from '@xterm/xterm';
2023-08-27 08:08:42 -07:00
import { deepStrictEqual } from 'assert';
2019-11-09 14:12:46 -08:00
export async function pollFor<T>(page: playwright.Page, evalOrFn: string | (() => Promise<T>), val: T, preFn?: () => Promise<void>, maxDuration?: number): Promise<void> {
2019-11-09 14:12:46 -08:00
if (preFn) {
await preFn();
}
const result = typeof evalOrFn === 'string' ? await page.evaluate(evalOrFn) : await evalOrFn();
if (process.env.DEBUG) {
console.log('pollFor result: ', result);
}
2019-11-09 17:29:32 -08:00
if (!deepEqual(result, val)) {
if (maxDuration === undefined) {
maxDuration = 2000;
}
if (maxDuration <= 0) {
deepStrictEqual(result, val, 'pollFor max duration exceeded');
}
2019-11-09 14:12:46 -08:00
return new Promise<void>(r => {
setTimeout(() => r(pollFor(page, evalOrFn, val, preFn, maxDuration! - 10)), 10);
2019-11-09 14:12:46 -08:00
});
}
}
2019-11-09 17:29:32 -08:00
2020-02-10 16:56:37 -06:00
export async function writeSync(page: playwright.Page, data: string): Promise<void> {
2019-11-09 17:29:32 -08:00
await page.evaluate(`
window.ready = false;
window.term.write('${data}', () => window.ready = true);
`);
await pollFor(page, 'window.ready', true);
}
2019-11-09 18:28:24 -08:00
export async function timeout(ms: number): Promise<void> {
return new Promise<void>(r => setTimeout(r, ms));
}
2020-02-10 16:56:37 -06:00
2023-09-12 12:45:29 -07:00
export async function openTerminal(page: playwright.Page, options: ITerminalOptions & ITerminalInitOnlyOptions = {}, testOptions: { loadUnicodeGraphemesAddon: boolean } = { loadUnicodeGraphemesAddon: true }): Promise<void> {
2022-07-27 08:20:54 -07:00
await page.evaluate(`window.term = new Terminal(${JSON.stringify({ allowProposedApi: true, ...options })})`);
2020-02-10 11:18:23 -06:00
await page.evaluate(`window.term.open(document.querySelector('#terminal-container'))`);
2023-09-12 12:45:29 -07:00
// HACK: This is a soft layer breaker that's temporarily included until unicode graphemes have
// more complete integration tests. See https://github.com/xtermjs/xterm.js/pull/4519#discussion_r1285234453
if (testOptions.loadUnicodeGraphemesAddon) {
await page.evaluate(`
window.unicode = new UnicodeGraphemesAddon();
window.term.loadAddon(window.unicode);
window.term.unicode.activeVersion = '15-graphemes';
`);
}
2022-07-27 09:22:05 -07:00
await page.waitForSelector('.xterm-rows');
2020-02-10 11:18:23 -06:00
}
2020-09-10 06:31:59 -07:00
export function getBrowserType(): playwright.BrowserType<playwright.WebKitBrowser> | playwright.BrowserType<playwright.ChromiumBrowser> | playwright.BrowserType<playwright.FirefoxBrowser> {
2020-02-10 16:56:37 -06:00
// Default to chromium
2020-09-10 06:31:59 -07:00
let browserType: playwright.BrowserType<playwright.WebKitBrowser> | playwright.BrowserType<playwright.ChromiumBrowser> | playwright.BrowserType<playwright.FirefoxBrowser> = playwright['chromium'];
2020-02-10 16:56:37 -06:00
const index = process.argv.indexOf('--browser');
2020-02-15 05:47:42 -08:00
if (index !== -1 && process.argv.length > index + 1 && typeof process.argv[index + 1] === 'string') {
2020-02-10 16:56:37 -06:00
const string = process.argv[index + 1];
if (string === 'firefox' || string === 'webkit') {
browserType = playwright[string];
}
}
return browserType;
}
2022-07-27 10:33:10 -07:00
export function launchBrowser(): Promise<playwright.Browser> {
const browserType = getBrowserType();
const options: Record<string, unknown> = {
2021-11-05 21:34:37 +02:00
headless: process.argv.includes('--headless')
};
const index = process.argv.indexOf('--executablePath');
2021-11-05 21:34:37 +02:00
if (index > 0 && process.argv.length > index + 1 && typeof process.argv[index + 1] === 'string') {
options.executablePath = process.argv[index + 1];
}
return browserType.launch(options);
}