2020-08-05 01:57:25 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-08-20 07:32:12 +03:00
|
|
|
import { options } from './playwright.fixtures';
|
2020-08-21 02:04:27 +03:00
|
|
|
import { registerFixture } from '../test-runner';
|
2020-08-05 01:57:25 +03:00
|
|
|
|
2020-08-07 21:19:15 +03:00
|
|
|
import path from 'path';
|
|
|
|
import {spawn, execSync} from 'child_process';
|
|
|
|
import { BrowserType, Browser, LaunchOptions } from '..';
|
2020-08-05 01:57:25 +03:00
|
|
|
|
|
|
|
const playwrightPath = path.join(__dirname, '..');
|
|
|
|
|
|
|
|
class Wrapper {
|
2020-08-07 21:19:15 +03:00
|
|
|
_output: Map<any, any>;
|
|
|
|
_outputCallback: Map<any, any>;
|
|
|
|
_browserType: BrowserType<Browser>;
|
|
|
|
_child: import("child_process").ChildProcess;
|
|
|
|
_exitPromise: Promise<unknown>;
|
|
|
|
_exitAndDisconnectPromise: Promise<any>;
|
|
|
|
constructor(browserType: BrowserType<Browser>, defaultBrowserOptions: LaunchOptions, extraOptions?: { stallOnClose: boolean; }) {
|
2020-08-05 01:57:25 +03:00
|
|
|
this._output = new Map();
|
|
|
|
this._outputCallback = new Map();
|
|
|
|
|
|
|
|
this._browserType = browserType;
|
|
|
|
const launchOptions = {...defaultBrowserOptions,
|
|
|
|
handleSIGINT: true,
|
|
|
|
handleSIGTERM: true,
|
|
|
|
handleSIGHUP: true,
|
2020-08-20 21:10:57 +03:00
|
|
|
executablePath: defaultBrowserOptions.executablePath || browserType.executablePath(),
|
2020-08-05 01:57:25 +03:00
|
|
|
logger: undefined,
|
|
|
|
};
|
|
|
|
const options = {
|
|
|
|
playwrightPath,
|
|
|
|
browserTypeName: browserType.name(),
|
|
|
|
launchOptions,
|
|
|
|
...extraOptions,
|
|
|
|
};
|
|
|
|
this._child = spawn('node', [path.join(__dirname, 'fixtures', 'closeme.js'), JSON.stringify(options)]);
|
|
|
|
this._child.on('error', (...args) => console.log("ERROR", ...args));
|
|
|
|
this._exitPromise = new Promise(resolve => this._child.on('exit', resolve));
|
|
|
|
|
|
|
|
let outputString = '';
|
|
|
|
this._child.stdout.on('data', data => {
|
|
|
|
outputString += data.toString();
|
|
|
|
// Uncomment to debug.
|
|
|
|
// console.log(data.toString());
|
|
|
|
let match;
|
|
|
|
while (match = outputString.match(/\(([^()]+)=>([^()]+)\)/)) {
|
|
|
|
const key = match[1];
|
|
|
|
const value = match[2];
|
|
|
|
this._addOutput(key, value);
|
|
|
|
outputString = outputString.substring(match.index + match[0].length);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_addOutput(key, value) {
|
|
|
|
this._output.set(key, value);
|
|
|
|
const cb = this._outputCallback.get(key);
|
|
|
|
this._outputCallback.delete(key);
|
|
|
|
if (cb)
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
async out(key) {
|
|
|
|
if (!this._output.has(key))
|
|
|
|
await new Promise(f => this._outputCallback.set(key, f));
|
|
|
|
return this._output.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
const wsEndpoint = await this.out('wsEndpoint');
|
|
|
|
const browser = await this._browserType.connect({ wsEndpoint });
|
|
|
|
this._exitAndDisconnectPromise = Promise.all([
|
|
|
|
this._exitPromise,
|
|
|
|
new Promise(resolve => browser.once('disconnected', resolve)),
|
|
|
|
]).then(([exitCode]) => exitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
child() {
|
|
|
|
return this._child;
|
|
|
|
}
|
|
|
|
|
|
|
|
async childExitCode() {
|
|
|
|
return await this._exitAndDisconnectPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 21:19:15 +03:00
|
|
|
declare global {
|
2020-08-20 22:51:05 +03:00
|
|
|
interface TestState {
|
2020-08-07 21:19:15 +03:00
|
|
|
wrapper: Wrapper;
|
|
|
|
stallingWrapper: Wrapper;
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 01:57:25 +03:00
|
|
|
registerFixture('wrapper', async ({browserType, defaultBrowserOptions}, test) => {
|
|
|
|
const wrapper = new Wrapper(browserType, defaultBrowserOptions);
|
|
|
|
await wrapper.connect();
|
|
|
|
await test(wrapper);
|
|
|
|
});
|
|
|
|
|
|
|
|
registerFixture('stallingWrapper', async ({browserType, defaultBrowserOptions}, test) => {
|
|
|
|
const wrapper = new Wrapper(browserType, defaultBrowserOptions, { stallOnClose: true });
|
|
|
|
await wrapper.connect();
|
|
|
|
await test(wrapper);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.slow()('should close the browser when the node process closes', async ({wrapper}) => {
|
|
|
|
if (WIN)
|
|
|
|
execSync(`taskkill /pid ${wrapper.child().pid} /T /F`);
|
|
|
|
else
|
|
|
|
process.kill(wrapper.child().pid);
|
|
|
|
expect(await wrapper.childExitCode()).toBe(WIN ? 1 : 0);
|
|
|
|
// We might not get browser exitCode in time when killing the parent node process,
|
|
|
|
// so we don't check it here.
|
|
|
|
});
|
|
|
|
|
|
|
|
// Cannot reliably send signals on Windows.
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should report browser close signal', async ({wrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
const pid = await wrapper.out('pid');
|
|
|
|
process.kill(-pid, 'SIGTERM');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('null');
|
|
|
|
expect(await wrapper.out('signal')).toBe('SIGTERM');
|
|
|
|
process.kill(wrapper.child().pid);
|
|
|
|
await wrapper.childExitCode();
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should report browser close signal 2', async ({wrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
const pid = await wrapper.out('pid');
|
|
|
|
process.kill(-pid, 'SIGKILL');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('null');
|
|
|
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
|
|
|
process.kill(wrapper.child().pid);
|
|
|
|
await wrapper.childExitCode();
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should close the browser on SIGINT', async ({wrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
process.kill(wrapper.child().pid, 'SIGINT');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('0');
|
|
|
|
expect(await wrapper.out('signal')).toBe('null');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(130);
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should close the browser on SIGTERM', async ({wrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('0');
|
|
|
|
expect(await wrapper.out('signal')).toBe('null');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(0);
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should close the browser on SIGHUP', async ({wrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
process.kill(wrapper.child().pid, 'SIGHUP');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('0');
|
|
|
|
expect(await wrapper.out('signal')).toBe('null');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(0);
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should kill the browser on double SIGINT', async ({stallingWrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
const wrapper = stallingWrapper;
|
|
|
|
process.kill(wrapper.child().pid, 'SIGINT');
|
|
|
|
await wrapper.out('stalled');
|
|
|
|
process.kill(wrapper.child().pid, 'SIGINT');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('null');
|
|
|
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(130);
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should kill the browser on SIGINT + SIGTERM', async ({stallingWrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
const wrapper = stallingWrapper;
|
|
|
|
process.kill(wrapper.child().pid, 'SIGINT');
|
|
|
|
await wrapper.out('stalled');
|
|
|
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('null');
|
|
|
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(0);
|
|
|
|
});
|
|
|
|
|
2020-08-19 23:30:54 +03:00
|
|
|
it.skip(WIN || !options.HEADLESS).slow()('should kill the browser on SIGTERM + SIGINT', async ({stallingWrapper}) => {
|
2020-08-05 01:57:25 +03:00
|
|
|
const wrapper = stallingWrapper;
|
|
|
|
process.kill(wrapper.child().pid, 'SIGTERM');
|
|
|
|
await wrapper.out('stalled');
|
|
|
|
process.kill(wrapper.child().pid, 'SIGINT');
|
|
|
|
expect(await wrapper.out('exitCode')).toBe('null');
|
|
|
|
expect(await wrapper.out('signal')).toBe('SIGKILL');
|
|
|
|
expect(await wrapper.childExitCode()).toBe(130);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('caller file path', async ({}) => {
|
|
|
|
const stackTrace = require(path.join(playwrightPath, 'lib', 'utils', 'stackTrace'));
|
|
|
|
const callme = require('./fixtures/callback');
|
|
|
|
const filePath = callme(() => {
|
|
|
|
return stackTrace.getCallerFilePath(path.join(__dirname, 'fixtures') + path.sep);
|
|
|
|
});
|
|
|
|
expect(filePath).toBe(__filename);
|
|
|
|
});
|