Skip to content

Commit 5965e27

Browse files
committed
test: make debugp collect IO
1 parent 9826fd6 commit 5965e27

File tree

8 files changed

+38
-26
lines changed

8 files changed

+38
-26
lines changed

src/chromium/crConnection.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class CRConnection extends platform.EventEmitter {
4444
this.rootSession = new CRSession(this, '', 'browser', '');
4545
this._sessions.set('', this.rootSession);
4646
this._debugProtocol = platform.debug('pw:protocol');
47+
(this._debugProtocol as any).color = '34';
4748
}
4849

4950
static fromSession(session: CRSession): CRConnection {

src/firefox/ffConnection.ts

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class FFConnection extends platform.EventEmitter {
5858
this.off = super.removeListener;
5959
this.removeListener = super.removeListener;
6060
this.once = super.once;
61+
(this._debugProtocol as any).color = '34';
6162
}
6263

6364
async send<T extends keyof Protocol.CommandParameters>(

src/server/processLauncher.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import { TimeoutError } from '../errors';
2424
import * as platform from '../platform';
2525

2626
const debugLauncher = platform.debug('pw:launcher');
27+
const debugStdout = platform.debug('pw:stdio:out');
28+
const debugStderr = platform.debug('pw:stdio:err');
29+
(debugStdout as any).color = '178';
30+
(debugStderr as any).color = '160';
2731
const removeFolderAsync = platform.promisify(removeFolder);
2832

2933
export type LaunchProcessOptions = {
@@ -73,13 +77,19 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
7377
return result;
7478
}
7579

76-
if (options.dumpio) {
77-
spawnedProcess.stdout.pipe(process.stdout);
78-
spawnedProcess.stderr.pipe(process.stderr);
79-
} else {
80-
spawnedProcess.stderr.on('data', () => {});
81-
spawnedProcess.stdout.on('data', () => {});
82-
}
80+
const stdout = readline.createInterface({ input: spawnedProcess.stdout });
81+
stdout.on('line', (data: string) => {
82+
debugStdout(data);
83+
if (options.dumpio)
84+
console.log(`\x1b[33m[out]\x1b[0m ${data}`); // eslint-disable-line no-console
85+
});
86+
87+
const stderr = readline.createInterface({ input: spawnedProcess.stderr });
88+
stderr.on('line', (data: string) => {
89+
debugStderr(data);
90+
if (options.dumpio)
91+
console.log(`\x1b[31m[err]\x1b[0m ${data}`); // eslint-disable-line no-console
92+
});
8393

8494
let processClosed = false;
8595
const waitForProcessToClose = new Promise((fulfill, reject) => {

src/webkit/wkConnection.ts

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class WKConnection {
4646
this.browserSession = new WKSession(this, '', 'Browser has been closed.', (message: any) => {
4747
this.rawSend(message);
4848
});
49+
(this._debugFunction as any).color = '34';
4950
}
5051

5152
nextMessageId(): number {

test/browsercontext.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
422422
await context.close();
423423
});
424424
// flaky: https://github.com/microsoft/playwright/pull/1301/checks?check_run_id=496478707
425-
it.fail(FFOX && LINUX)('should fail if wrong credentials', async({browser, server}) => {
425+
it('should fail if wrong credentials', async({browser, server}) => {
426426
server.setAuth('/empty.html', 'user', 'pass');
427427
const context = await browser.newContext({
428428
httpCredentials: { username: 'foo', password: 'bar' }

test/fixtures.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ module.exports.describe = function({testRunner, expect, product, browserType, pl
7272
it.slow()('should dump browser process stderr', async({server}) => {
7373
let dumpioData = '';
7474
const res = spawn('node', [path.join(__dirname, 'fixtures', 'dumpio.js'), playwrightPath, product, 'usewebsocket']);
75-
res.stderr.on('data', data => dumpioData += data.toString('utf8'));
75+
res.stdout.on('data', data => dumpioData += data.toString('utf8'));
7676
await new Promise(resolve => res.on('close', resolve));
7777
expect(dumpioData).toContain('message from dumpio');
7878
});
7979
it.slow()('should dump browser process stderr', async({server}) => {
8080
let dumpioData = '';
8181
const res = spawn('node', [path.join(__dirname, 'fixtures', 'dumpio.js'), playwrightPath, product]);
82-
res.stderr.on('data', data => dumpioData += data.toString('utf8'));
82+
res.stdout.on('data', data => dumpioData += data.toString('utf8'));
8383
await new Promise(resolve => res.on('close', resolve));
8484
expect(dumpioData).toContain('message from dumpio');
8585
});

test/playwright.spec.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const os = require('os');
2020
const rm = require('rimraf').sync;
2121
const GoldenUtils = require('./golden-utils');
2222
const {Matchers} = require('../utils/testrunner/');
23+
const readline = require('readline');
2324

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

108111
afterAll(async state => {
@@ -112,22 +115,16 @@ module.exports.describe = ({testRunner, product, playwrightPath}) => {
112115
});
113116

114117
beforeEach(async(state, test) => {
115-
const onLine = (line) => test.output += line + '\n';
118+
test.output = [];
119+
const dumpout = data => test.output.push(`\x1b[33m[pw:stdio:out]\x1b[0m ${data}`);
120+
const dumperr = data => test.output.push(`\x1b[31m[pw:stdio:err]\x1b[0m ${data}`);
121+
state._stdout.on('line', dumpout);
122+
state._stderr.on('line', dumperr);
116123
if (dumpProtocolOnFailure)
117-
state.browser._setDebugFunction(onLine);
118-
119-
let rl;
120-
if (state.browserServer.process().stderr) {
121-
rl = require('readline').createInterface({ input: state.browserServer.process().stderr });
122-
test.output = '';
123-
rl.on('line', onLine);
124-
}
125-
124+
state.browser._setDebugFunction(data => test.output.push(`\x1b[32m[pw:protocol]\x1b[0m ${data}`));
126125
state.tearDown = async () => {
127-
if (rl) {
128-
rl.removeListener('line', onLine);
129-
rl.close();
130-
}
126+
state._stdout.off('line', dumpout);
127+
state._stderr.off('line', dumperr);
131128
if (dumpProtocolOnFailure)
132129
state.browser._setDebugFunction(() => void 0);
133130
};

utils/testrunner/Reporter.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ class Reporter {
207207
console.log(`${prefix} ${colors.red(`[TIMEOUT ${test.timeout}ms]`)} ${test.fullName} (${formatLocation(test.location)})`);
208208
if (test.output) {
209209
console.log(' Output:');
210-
console.log(padLines(test.output, 4));
210+
for (const line of test.output)
211+
console.log(' ' + line);
211212
}
212213
} else if (test.result === 'failed') {
213214
console.log(`${prefix} ${colors.red('[FAIL]')} ${test.fullName} (${formatLocation(test.location)})`);
@@ -253,7 +254,8 @@ class Reporter {
253254
}
254255
if (test.output) {
255256
console.log(' Output:');
256-
console.log(padLines(test.output, 4));
257+
for (const line of test.output)
258+
console.log(' ' + line);
257259
}
258260
}
259261
}

0 commit comments

Comments
 (0)