chore: render testInfo errors in the Errors tab (#28179)

Fixes https://github.com/microsoft/playwright/issues/28056
This commit is contained in:
Pavel Feldman 2023-11-16 11:37:57 -08:00 committed by GitHub
parent ee1e6cd72f
commit 2bd7d67adc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 34 deletions

View file

@ -21,7 +21,8 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import { ManualPromise, calculateSha1, monotonicTime } from 'playwright-core/lib/utils'; import { ManualPromise, calculateSha1, monotonicTime } from 'playwright-core/lib/utils';
import { yauzl, yazl } from 'playwright-core/lib/zipBundle'; import { yauzl, yazl } from 'playwright-core/lib/zipBundle';
import type { TestInfo } from '../../types/test'; import type { TestInfo, TestInfoError } from '../../types/test';
import { filteredStackTrace } from '../util';
export type Attachment = TestInfo['attachments'][0]; export type Attachment = TestInfo['attachments'][0];
@ -101,6 +102,16 @@ export class TestTracing {
}); });
} }
appendForError(error: TestInfoError) {
const rawStack = error.stack?.split('\n') || [];
const stack = rawStack ? filteredStackTrace(rawStack) : [];
this._appendTraceEvent({
type: 'error',
message: error.message || String(error.value),
stack,
});
}
appendStdioToTrace(type: 'stdout' | 'stderr', chunk: string | Buffer) { appendStdioToTrace(type: 'stdout' | 'stderr', chunk: string | Buffer) {
this._appendTraceEvent({ this._appendTraceEvent({
type, type,

View file

@ -22,7 +22,7 @@ import { ConfigLoader } from '../common/configLoader';
import type { Suite, TestCase } from '../common/test'; import type { Suite, TestCase } from '../common/test';
import type { Annotation, FullConfigInternal, FullProjectInternal } from '../common/config'; import type { Annotation, FullConfigInternal, FullProjectInternal } from '../common/config';
import { FixtureRunner } from './fixtureRunner'; import { FixtureRunner } from './fixtureRunner';
import { ManualPromise, captureLibraryStackTrace, gracefullyCloseAll, removeFolders } from 'playwright-core/lib/utils'; import { ManualPromise, gracefullyCloseAll, removeFolders } from 'playwright-core/lib/utils';
import { TestInfoImpl } from './testInfo'; import { TestInfoImpl } from './testInfo';
import { TimeoutManager, type TimeSlot } from './timeoutManager'; import { TimeoutManager, type TimeSlot } from './timeoutManager';
import { ProcessRunner } from '../common/process'; import { ProcessRunner } from '../common/process';
@ -372,7 +372,7 @@ export class WorkerMain extends ProcessRunner {
return; return;
} }
const error = await testInfo._runAndFailOnError(async () => { await testInfo._runAndFailOnError(async () => {
// Now run the test itself. // Now run the test itself.
debugTest(`test function started`); debugTest(`test function started`);
const fn = test.fn; // Extract a variable to get a better stack trace ("myTest" vs "TestCase.myTest [as fn]"). const fn = test.fn; // Extract a variable to get a better stack trace ("myTest" vs "TestCase.myTest [as fn]").
@ -380,17 +380,8 @@ export class WorkerMain extends ProcessRunner {
debugTest(`test function finished`); debugTest(`test function finished`);
}, 'allowSkips'); }, 'allowSkips');
// If there are no steps with errors in the test, but the test has an error - append artificial trace entry. for (const error of testInfo.errors)
if (error && !testInfo._steps.some(s => !!s.error)) { testInfo._tracing.appendForError(error);
const frames = error.stack ? captureLibraryStackTrace(error.stack.split('\n')).frames : [];
const step = testInfo._addStep({
wallTime: Date.now(),
title: error.message || 'error',
category: 'hook',
location: frames[0],
});
step.complete({ error });
}
}); });
if (didFailBeforeAllForSuite) { if (didFailBeforeAllForSuite) {

View file

@ -36,6 +36,7 @@ export type ContextEntry = {
actions: ActionEntry[]; actions: ActionEntry[];
events: (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[]; events: (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[];
stdio: trace.StdioTraceEvent[]; stdio: trace.StdioTraceEvent[];
errors: trace.ErrorTraceEvent[];
hasSource: boolean; hasSource: boolean;
}; };
@ -68,6 +69,7 @@ export function createEmptyContext(): ContextEntry {
resources: [], resources: [],
actions: [], actions: [],
events: [], events: [],
errors: [],
stdio: [], stdio: [],
hasSource: false hasSource: false
}; };

View file

@ -223,6 +223,10 @@ export class TraceModel {
contextEntry!.stdio.push(event); contextEntry!.stdio.push(event);
break; break;
} }
case 'error': {
contextEntry!.errors.push(event);
break;
}
case 'console': { case 'console': {
contextEntry!.events.push(event); contextEntry!.events.push(event);
break; break;

View file

@ -20,20 +20,50 @@ import type * as modelUtil from './modelUtil';
import { PlaceholderPanel } from './placeholderPanel'; import { PlaceholderPanel } from './placeholderPanel';
import { renderAction } from './actionList'; import { renderAction } from './actionList';
import type { Language } from '@isomorphic/locatorGenerators'; import type { Language } from '@isomorphic/locatorGenerators';
import type { StackFrame } from '@protocol/channels';
type ErrorDescription = {
action?: modelUtil.ActionTraceEventInContext;
stack?: StackFrame[];
};
type ErrorsTabModel = { type ErrorsTabModel = {
errors: Map<string, modelUtil.ActionTraceEventInContext>; errors: Map<string, ErrorDescription>;
}; };
function errorsFromActions(model: modelUtil.MultiTraceModel): Map<string, ErrorDescription> {
const errors = new Map<string, ErrorDescription>();
for (const action of model.actions || []) {
// Overwrite errors with the last one.
if (!action.error?.message || errors.has(action.error.message))
continue;
errors.set(action.error.message, {
action,
stack: action.stack,
});
}
return errors;
}
function errorsFromTestRunner(model: modelUtil.MultiTraceModel): Map<string, ErrorDescription> {
const actionErrors = errorsFromActions(model);
const errors = new Map<string, ErrorDescription>();
for (const error of model.errors || []) {
if (!error.message || errors.has(error.message))
continue;
errors.set(error.message, actionErrors.get(error.message) || error);
}
return errors;
}
export function useErrorsTabModel(model: modelUtil.MultiTraceModel | undefined): ErrorsTabModel { export function useErrorsTabModel(model: modelUtil.MultiTraceModel | undefined): ErrorsTabModel {
return React.useMemo(() => { return React.useMemo(() => {
const errors = new Map<string, modelUtil.ActionTraceEventInContext>(); if (!model)
for (const action of model?.actions || []) { return { errors: new Map() };
// Overwrite errors with the last one. // Feature detection: if there is test runner info, pick errors from the 'error' trace events.
if (action.error?.message) // If there are no test errors, but there are action errors - render those instead.
errors.set(action.error.message, action); const testHasErrors = !!model.errors.length;
} return { errors: testHasErrors ? errorsFromTestRunner(model) : errorsFromActions(model) };
return { errors };
}, [model]); }, [model]);
} }
@ -46,13 +76,14 @@ export const ErrorsTab: React.FunctionComponent<{
return <PlaceholderPanel text='No errors' />; return <PlaceholderPanel text='No errors' />;
return <div className='fill' style={{ overflow: 'auto' }}> return <div className='fill' style={{ overflow: 'auto' }}>
{[...errorsModel.errors.entries()].map(([message, action]) => { {[...errorsModel.errors.entries()].map(([message, error]) => {
let location: string | undefined; let location: string | undefined;
let longLocation: string | undefined; let longLocation: string | undefined;
if (action.stack?.[0]) { const stackFrame = error.stack?.[0];
const file = action.stack[0].file.replace(/.*\/(.*)/, '$1'); if (stackFrame) {
location = file + ':' + action.stack[0].line; const file = stackFrame.file.replace(/.*\/(.*)/, '$1');
longLocation = action.stack[0].file + ':' + action.stack[0].line; location = file + ':' + stackFrame.line;
longLocation = stackFrame.file + ':' + stackFrame.line;
} }
return <div key={message}> return <div key={message}>
<div className='hbox' style={{ <div className='hbox' style={{
@ -62,9 +93,9 @@ export const ErrorsTab: React.FunctionComponent<{
fontWeight: 'bold', fontWeight: 'bold',
color: 'var(--vscode-errorForeground)', color: 'var(--vscode-errorForeground)',
}}> }}>
{renderAction(action, { sdkLanguage })} {error.action && renderAction(error.action, { sdkLanguage })}
{location && <div className='action-location'> {location && <div className='action-location'>
@ <span title={longLocation} onClick={() => revealInSource(action)}>{location}</span> @ <span title={longLocation} onClick={() => error.action && revealInSource(error.action)}>{location}</span>
</div>} </div>}
</div> </div>
<ErrorMessage error={message} /> <ErrorMessage error={message} />

View file

@ -61,6 +61,7 @@ export class MultiTraceModel {
readonly actions: ActionTraceEventInContext[]; readonly actions: ActionTraceEventInContext[];
readonly events: (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[]; readonly events: (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[];
readonly stdio: trace.StdioTraceEvent[]; readonly stdio: trace.StdioTraceEvent[];
readonly errors: trace.ErrorTraceEvent[];
readonly hasSource: boolean; readonly hasSource: boolean;
readonly sdkLanguage: Language | undefined; readonly sdkLanguage: Language | undefined;
readonly testIdAttributeName: string | undefined; readonly testIdAttributeName: string | undefined;
@ -86,6 +87,7 @@ export class MultiTraceModel {
this.actions = mergeActions(contexts); this.actions = mergeActions(contexts);
this.events = ([] as (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[]).concat(...contexts.map(c => c.events)); this.events = ([] as (trace.EventTraceEvent | trace.ConsoleMessageTraceEvent)[]).concat(...contexts.map(c => c.events));
this.stdio = ([] as trace.StdioTraceEvent[]).concat(...contexts.map(c => c.stdio)); this.stdio = ([] as trace.StdioTraceEvent[]).concat(...contexts.map(c => c.stdio));
this.errors = ([] as trace.ErrorTraceEvent[]).concat(...contexts.map(c => c.errors));
this.hasSource = contexts.some(c => c.hasSource); this.hasSource = contexts.some(c => c.hasSource);
this.resources = [...contexts.map(c => c.resources)].flat(); this.resources = [...contexts.map(c => c.resources)].flat();

View file

@ -145,6 +145,12 @@ export type StdioTraceEvent = {
base64?: string; base64?: string;
}; };
export type ErrorTraceEvent = {
type: 'error';
message: string;
stack?: StackFrame[];
};
export type TraceEvent = export type TraceEvent =
ContextCreatedTraceEvent | ContextCreatedTraceEvent |
ScreencastFrameTraceEvent | ScreencastFrameTraceEvent |
@ -157,4 +163,5 @@ export type TraceEvent =
ConsoleMessageTraceEvent | ConsoleMessageTraceEvent |
ResourceSnapshotTraceEvent | ResourceSnapshotTraceEvent |
FrameSnapshotTraceEvent | FrameSnapshotTraceEvent |
StdioTraceEvent; StdioTraceEvent |
ErrorTraceEvent;

View file

@ -156,7 +156,7 @@ export async function parseTraceRaw(file: string): Promise<{ events: any[], reso
}; };
} }
export async function parseTrace(file: string): Promise<{ resources: Map<string, Buffer>, events: (EventTraceEvent | ConsoleMessageTraceEvent)[], actions: ActionTraceEvent[], apiNames: string[], traceModel: TraceModel, model: MultiTraceModel, actionTree: string[] }> { export async function parseTrace(file: string): Promise<{ resources: Map<string, Buffer>, events: (EventTraceEvent | ConsoleMessageTraceEvent)[], actions: ActionTraceEvent[], apiNames: string[], traceModel: TraceModel, model: MultiTraceModel, actionTree: string[], errors: string[] }> {
const backend = new TraceBackend(file); const backend = new TraceBackend(file);
const traceModel = new TraceModel(); const traceModel = new TraceModel();
await traceModel.load(backend, () => {}); await traceModel.load(backend, () => {});
@ -174,6 +174,7 @@ export async function parseTrace(file: string): Promise<{ resources: Map<string,
resources: backend.entries, resources: backend.entries,
actions: model.actions, actions: model.actions,
events: model.events, events: model.events,
errors: model.errors.map(e => e.message),
model, model,
traceModel, traceModel,
actionTree, actionTree,

View file

@ -323,7 +323,6 @@ test('should not override trace file in afterAll', async ({ runInlineTest, serve
' fixture: page', ' fixture: page',
' browserContext.newPage', ' browserContext.newPage',
'page.goto', 'page.goto',
'error',
'After Hooks', 'After Hooks',
' fixture: page', ' fixture: page',
' fixture: context', ' fixture: context',
@ -335,6 +334,7 @@ test('should not override trace file in afterAll', async ({ runInlineTest, serve
' fixture: request', ' fixture: request',
' apiRequestContext.dispose', ' apiRequestContext.dispose',
]); ]);
expect(trace1.errors).toEqual([`'oh no!'`]);
const error = await parseTrace(testInfo.outputPath('test-results', 'a-test-2', 'trace.zip')).catch(e => e); const error = await parseTrace(testInfo.outputPath('test-results', 'a-test-2', 'trace.zip')).catch(e => e);
expect(error).toBeTruthy(); expect(error).toBeTruthy();
@ -668,11 +668,11 @@ test('should show non-expect error in trace', async ({ runInlineTest }, testInfo
' fixture: page', ' fixture: page',
' browserContext.newPage', ' browserContext.newPage',
'expect.toBe', 'expect.toBe',
'ReferenceError: undefinedVariable1 is not defined',
'After Hooks', 'After Hooks',
' fixture: page', ' fixture: page',
' fixture: context', ' fixture: context',
]); ]);
expect(trace.errors).toEqual(['ReferenceError: undefinedVariable1 is not defined']);
}); });
test('should not throw when attachment is missing', async ({ runInlineTest }, testInfo) => { test('should not throw when attachment is missing', async ({ runInlineTest }, testInfo) => {

View file

@ -820,7 +820,7 @@ for (const useIntermediateMergeReport of [false, true] as const) {
await showReport(); await showReport();
await page.locator('text=sample').first().click(); await page.locator('text=sample').first().click();
await expect(page.locator('text=ouch')).toHaveCount(2); await expect(page.locator('text=ouch')).toHaveCount(1);
await page.locator('text=All').first().click(); await page.locator('text=All').first().click();
await page.locator('text=sample').nth(1).click(); await page.locator('text=sample').nth(1).click();