Skip to content

Commit

Permalink
test: make debugp collect IO
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Mar 23, 2020
1 parent 9826fd6 commit b91f5eb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/chromium/crConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class CRConnection extends platform.EventEmitter {
this.rootSession = new CRSession(this, '', 'browser', '');
this._sessions.set('', this.rootSession);
this._debugProtocol = platform.debug('pw:protocol');
(this._debugProtocol as any).color = '34';
}

static fromSession(session: CRSession): CRConnection {
Expand Down
1 change: 1 addition & 0 deletions src/firefox/ffConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class FFConnection extends platform.EventEmitter {
this.off = super.removeListener;
this.removeListener = super.removeListener;
this.once = super.once;
(this._debugProtocol as any).color = '34';
}

async send<T extends keyof Protocol.CommandParameters>(
Expand Down
24 changes: 17 additions & 7 deletions src/server/processLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { TimeoutError } from '../errors';
import * as platform from '../platform';

const debugLauncher = platform.debug('pw:launcher');
const debugStdout = platform.debug('pw:stdio:out');
const debugStderr = platform.debug('pw:stdio:err');
(debugStdout as any).color = '178';
(debugStderr as any).color = '160';
const removeFolderAsync = platform.promisify(removeFolder);

export type LaunchProcessOptions = {
Expand Down Expand Up @@ -73,13 +77,19 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
return result;
}

if (options.dumpio) {
spawnedProcess.stdout.pipe(process.stdout);
spawnedProcess.stderr.pipe(process.stderr);
} else {
spawnedProcess.stderr.on('data', () => {});
spawnedProcess.stdout.on('data', () => {});
}
const stdout = readline.createInterface({ input: spawnedProcess.stdout });
stdout.on('line', (data: string) => {
debugStdout(data);
if (options.dumpio)
console.log(`\x1b[33m[out]\x1b[0m ${data}`);
});

const stderr = readline.createInterface({ input: spawnedProcess.stderr });
stderr.on('line', (data: string) => {
debugStderr(data);
if (options.dumpio)
console.log(`\x1b[31m[err]\x1b[0m ${data}`);
});

let processClosed = false;
const waitForProcessToClose = new Promise((fulfill, reject) => {
Expand Down
1 change: 1 addition & 0 deletions src/webkit/wkConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class WKConnection {
this.browserSession = new WKSession(this, '', 'Browser has been closed.', (message: any) => {
this.rawSend(message);
});
(this._debugProtocol as any).color = '34';
}

nextMessageId(): number {
Expand Down
2 changes: 1 addition & 1 deletion test/browsercontext.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
await context.close();
});
// flaky: https://github.com/microsoft/playwright/pull/1301/checks?check_run_id=496478707
it.fail(FFOX && LINUX)('should fail if wrong credentials', async({browser, server}) => {
it('should fail if wrong credentials', async({browser, server}) => {
server.setAuth('/empty.html', 'user', 'pass');
const context = await browser.newContext({
httpCredentials: { username: 'foo', password: 'bar' }
Expand Down
27 changes: 12 additions & 15 deletions test/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const os = require('os');
const rm = require('rimraf').sync;
const GoldenUtils = require('./golden-utils');
const {Matchers} = require('../utils/testrunner/');
const readline = require('readline');

const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';
Expand Down Expand Up @@ -103,6 +104,8 @@ module.exports.describe = ({testRunner, product, playwrightPath}) => {
beforeAll(async state => {
state.browser = await browserType.launch(defaultBrowserOptions);
state.browserServer = state.browser.__server__;
state._stdout = readline.createInterface({ input: state.browserServer.process().stdout });
state._stderr = readline.createInterface({ input: state.browserServer.process().stderr });
});

afterAll(async state => {
Expand All @@ -112,23 +115,17 @@ module.exports.describe = ({testRunner, product, playwrightPath}) => {
});

beforeEach(async(state, test) => {
const onLine = (line) => test.output += line + '\n';
test.output = [];
const dumpout = data => test.output.push(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
const dumperr = data => test.output.push(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
state._stdout.on('line', dumpout);
state._stderr.on('line', dumperr);
if (dumpProtocolOnFailure)
state.browser._setDebugFunction(onLine);

let rl;
if (state.browserServer.process().stderr) {
rl = require('readline').createInterface({ input: state.browserServer.process().stderr });
test.output = '';
rl.on('line', onLine);
}

state.browser._setDebugFunction(data => test.output.push(`\x1b[32m[pw:protocol]\x1b[0m ${data}`));
state.tearDown = async () => {
if (rl) {
rl.removeListener('line', onLine);
rl.close();
}
if (dumpProtocolOnFailure)
state._stdout.off('line', dumpout);
state._stderr.off('line', dumperr);
if (dumpProtocolOnFailure)
state.browser._setDebugFunction(() => void 0);
};
});
Expand Down
6 changes: 4 additions & 2 deletions utils/testrunner/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ class Reporter {
console.log(`${prefix} ${colors.red(`[TIMEOUT ${test.timeout}ms]`)} ${test.fullName} (${formatLocation(test.location)})`);
if (test.output) {
console.log(' Output:');
console.log(padLines(test.output, 4));
for (const line of test.output)
console.log(' ' + line);
}
} else if (test.result === 'failed') {
console.log(`${prefix} ${colors.red('[FAIL]')} ${test.fullName} (${formatLocation(test.location)})`);
Expand Down Expand Up @@ -253,7 +254,8 @@ class Reporter {
}
if (test.output) {
console.log(' Output:');
console.log(padLines(test.output, 4));
for (const line of test.output)
console.log(' ' + line);
}
}
}
Expand Down

0 comments on commit b91f5eb

Please sign in to comment.