2020-08-26 05:09:30 +03:00
|
|
|
/**
|
|
|
|
* Copyright Microsoft Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import path from 'path';
|
2021-10-11 17:52:17 +03:00
|
|
|
import type { BrowserType, Browser, LaunchOptions } from 'playwright-core';
|
2022-04-07 00:57:14 +03:00
|
|
|
import type { CommonFixtures, TestChildProcess } from './commonFixtures';
|
2020-08-26 05:09:30 +03:00
|
|
|
|
2021-04-02 05:13:08 +03:00
|
|
|
export type RemoteServerOptions = {
|
|
|
|
stallOnClose?: boolean;
|
2021-05-06 19:34:06 +03:00
|
|
|
disconnectOnSIGHUP?: boolean;
|
2022-04-12 22:28:33 +03:00
|
|
|
exitOnFile?: string;
|
2021-04-02 05:13:08 +03:00
|
|
|
inCluster?: boolean;
|
|
|
|
url?: string;
|
|
|
|
};
|
2020-08-26 05:09:30 +03:00
|
|
|
|
2020-09-11 07:31:46 +03:00
|
|
|
export class RemoteServer {
|
2021-09-22 02:24:48 +03:00
|
|
|
private _process: TestChildProcess;
|
|
|
|
_output: Map<string, string>;
|
|
|
|
_outputCallback: Map<string, () => void>;
|
2021-04-08 20:27:24 +03:00
|
|
|
_browserType: BrowserType;
|
2020-08-26 05:09:30 +03:00
|
|
|
_exitAndDisconnectPromise: Promise<any>;
|
|
|
|
_browser: Browser;
|
|
|
|
_wsEndpoint: string;
|
|
|
|
|
2021-10-28 05:00:06 +03:00
|
|
|
async _start(childProcess: CommonFixtures['childProcess'], browserType: BrowserType, remoteServerOptions: RemoteServerOptions = {}) {
|
2020-08-26 05:09:30 +03:00
|
|
|
this._output = new Map();
|
|
|
|
this._outputCallback = new Map();
|
|
|
|
|
|
|
|
this._browserType = browserType;
|
2021-10-28 05:00:06 +03:00
|
|
|
const browserOptions = (browserType as any)._defaultLaunchOptions;
|
2021-05-15 17:42:35 +03:00
|
|
|
// Copy options to prevent a large JSON string when launching subprocess.
|
|
|
|
// Otherwise, we get `Error: spawn ENAMETOOLONG` on Windows.
|
|
|
|
const launchOptions: LaunchOptions = {
|
|
|
|
args: browserOptions.args,
|
|
|
|
headless: browserOptions.headless,
|
|
|
|
channel: browserOptions.channel,
|
2020-08-26 05:09:30 +03:00
|
|
|
handleSIGINT: true,
|
|
|
|
handleSIGTERM: true,
|
|
|
|
handleSIGHUP: true,
|
2021-03-15 18:07:57 +03:00
|
|
|
executablePath: browserOptions.channel ? undefined : browserOptions.executablePath || browserType.executablePath(),
|
2020-08-26 05:09:30 +03:00
|
|
|
logger: undefined,
|
|
|
|
};
|
|
|
|
const options = {
|
|
|
|
browserTypeName: browserType.name(),
|
|
|
|
launchOptions,
|
2021-04-02 05:13:08 +03:00
|
|
|
...remoteServerOptions,
|
2020-08-26 05:09:30 +03:00
|
|
|
};
|
2021-09-22 02:24:48 +03:00
|
|
|
this._process = childProcess({
|
|
|
|
command: ['node', path.join(__dirname, 'remote-server-impl.js'), JSON.stringify(options)],
|
|
|
|
});
|
2020-08-26 05:09:30 +03:00
|
|
|
|
2021-09-22 02:24:48 +03:00
|
|
|
let index = 0;
|
|
|
|
this._process.onOutput = () => {
|
2020-08-26 05:09:30 +03:00
|
|
|
let match;
|
2021-09-22 02:24:48 +03:00
|
|
|
while ((match = this._process.output.substring(index).match(/\(([^()]+)=>([^()]+)\)/))) {
|
2020-08-26 05:09:30 +03:00
|
|
|
const key = match[1];
|
|
|
|
const value = match[2];
|
|
|
|
this._addOutput(key, value);
|
2021-09-22 02:24:48 +03:00
|
|
|
index += match.index + match[0].length;
|
2020-08-26 05:09:30 +03:00
|
|
|
}
|
2021-09-22 02:24:48 +03:00
|
|
|
};
|
2020-08-26 05:09:30 +03:00
|
|
|
|
|
|
|
this._wsEndpoint = await this.out('wsEndpoint');
|
2021-04-02 05:13:08 +03:00
|
|
|
|
|
|
|
if (remoteServerOptions.url) {
|
|
|
|
this._browser = await this._browserType.connect({ wsEndpoint: this._wsEndpoint });
|
|
|
|
const page = await this._browser.newPage();
|
|
|
|
await page.goto(remoteServerOptions.url);
|
|
|
|
}
|
2020-08-26 05:09:30 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:24:48 +03:00
|
|
|
_addOutput(key: string, value: string) {
|
2020-08-26 05:09:30 +03:00
|
|
|
this._output.set(key, value);
|
|
|
|
const cb = this._outputCallback.get(key);
|
|
|
|
this._outputCallback.delete(key);
|
|
|
|
if (cb)
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:24:48 +03:00
|
|
|
async out(key: string) {
|
2020-08-26 05:09:30 +03:00
|
|
|
if (!this._output.has(key))
|
2021-09-22 02:24:48 +03:00
|
|
|
await new Promise<void>(f => this._outputCallback.set(key, f));
|
2020-08-26 05:09:30 +03:00
|
|
|
return this._output.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
wsEndpoint() {
|
|
|
|
return this._wsEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
child() {
|
2021-09-22 02:24:48 +03:00
|
|
|
return this._process.process;
|
2020-08-26 05:09:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async childExitCode() {
|
2021-09-22 02:24:48 +03:00
|
|
|
return await this._process.exitCode;
|
2020-08-26 05:09:30 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:46:30 +03:00
|
|
|
async close() {
|
2021-04-02 05:13:08 +03:00
|
|
|
if (this._browser) {
|
|
|
|
await this._browser.close();
|
|
|
|
this._browser = undefined;
|
|
|
|
}
|
2021-09-22 02:24:48 +03:00
|
|
|
await this._process.close();
|
2020-08-26 05:09:30 +03:00
|
|
|
return await this.childExitCode();
|
|
|
|
}
|
|
|
|
}
|