633 lines
20 KiB
TypeScript
Raw Normal View History

2023-03-01 15:27:23 -08:00
/**
* 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.
*/
import '@web/third_party/vscode/codicon.css';
import { Workbench } from './workbench';
2023-03-01 15:27:23 -08:00
import '@web/common.css';
import React from 'react';
2023-03-08 17:33:27 -08:00
import { TreeView } from '@web/components/treeView';
import type { TreeState } from '@web/components/treeView';
2023-03-01 15:27:23 -08:00
import { TeleReporterReceiver } from '../../../playwright-test/src/isomorphic/teleReceiver';
import type { FullConfig, Suite, TestCase, TestResult, TestStep, Location } from '../../../playwright-test/types/testReporter';
2023-03-01 15:27:23 -08:00
import { SplitView } from '@web/components/splitView';
import { MultiTraceModel } from './modelUtil';
2023-03-01 15:27:23 -08:00
import './watchMode.css';
import { ToolbarButton } from '@web/components/toolbarButton';
import { Toolbar } from '@web/components/toolbar';
import { toggleTheme } from '@web/theme';
import type { ContextEntry } from '../entries';
import type * as trace from '@trace/trace';
2023-03-07 14:24:50 -08:00
import type { XtermDataSource } from '@web/components/xtermWrapper';
import { XtermWrapper } from '@web/components/xtermWrapper';
2023-03-01 15:27:23 -08:00
let updateRootSuite: (rootSuite: Suite, progress: Progress) => void = () => {};
let updateStepsProgress: () => void = () => {};
let runWatchedTests = () => {};
2023-03-07 12:43:16 -08:00
let runVisibleTests = () => {};
2023-03-01 15:27:23 -08:00
2023-03-07 14:24:50 -08:00
const xtermDataSource: XtermDataSource = {
pending: [],
clear: () => {},
write: data => xtermDataSource.pending.push(data),
resize: (cols: number, rows: number) => sendMessageNoReply('resizeTerminal', { cols, rows }),
};
2023-03-01 15:27:23 -08:00
export const WatchModeView: React.FC<{}> = ({
}) => {
const [projects, setProjects] = React.useState<Map<string, boolean>>(new Map());
const [rootSuite, setRootSuite] = React.useState<{ value: Suite | undefined }>({ value: undefined });
2023-03-07 12:43:16 -08:00
const [isRunningTest, setIsRunningTest] = React.useState<boolean>(false);
const [progress, setProgress] = React.useState<Progress>({ total: 0, passed: 0, failed: 0 });
2023-03-08 17:33:27 -08:00
const [selectedTest, setSelectedTest] = React.useState<TestCase | undefined>(undefined);
2023-03-07 12:43:16 -08:00
const [settingsVisible, setSettingsVisible] = React.useState<boolean>(false);
const [isWatchingFiles, setIsWatchingFiles] = React.useState<boolean>(true);
2023-03-07 12:43:16 -08:00
updateRootSuite = (rootSuite: Suite, { passed, failed }: Progress) => {
for (const projectName of projects.keys()) {
if (!rootSuite.suites.find(s => s.title === projectName))
projects.delete(projectName);
}
for (const projectSuite of rootSuite.suites) {
if (!projects.has(projectSuite.title))
projects.set(projectSuite.title, false);
}
if (![...projects.values()].includes(true))
projects.set(projects.entries().next().value[0], true);
progress.passed = passed;
progress.failed = failed;
setRootSuite({ value: rootSuite });
setProjects(new Map(projects));
setProgress({ ...progress });
};
2023-03-07 12:43:16 -08:00
const runTests = (testIds: string[]) => {
setProgress({ total: testIds.length, passed: 0, failed: 0 });
setIsRunningTest(true);
sendMessage('run', { testIds }).then(() => {
setIsRunningTest(false);
});
};
2023-03-07 14:24:50 -08:00
return <div className='vbox'>
<SplitView sidebarSize={250} orientation='horizontal' sidebarIsFirst={true}>
2023-03-08 17:33:27 -08:00
<TraceView test={selectedTest}></TraceView>
2023-03-07 14:24:50 -08:00
<div className='vbox watch-mode-sidebar'>
<Toolbar>
<div className='section-title' style={{ cursor: 'pointer' }} onClick={() => setSettingsVisible(false)}>Tests</div>
<ToolbarButton icon='play' title='Run' onClick={runVisibleTests} disabled={isRunningTest}></ToolbarButton>
<ToolbarButton icon='debug-stop' title='Stop' onClick={() => sendMessageNoReply('stop')} disabled={!isRunningTest}></ToolbarButton>
<ToolbarButton icon='refresh' title='Reload' onClick={() => refreshRootSuite(true)} disabled={isRunningTest}></ToolbarButton>
<ToolbarButton icon='eye-watch' title='Watch' toggled={isWatchingFiles} onClick={() => setIsWatchingFiles(!isWatchingFiles)}></ToolbarButton>
2023-03-07 12:43:16 -08:00
<div className='spacer'></div>
2023-03-07 14:24:50 -08:00
<ToolbarButton icon='gear' title='Toggle color mode' toggled={settingsVisible} onClick={() => { setSettingsVisible(!settingsVisible); }}></ToolbarButton>
</Toolbar>
<TestList
projects={projects}
2023-03-07 14:24:50 -08:00
rootSuite={rootSuite}
isRunningTest={isRunningTest}
isWatchingFiles={isWatchingFiles}
2023-03-07 14:24:50 -08:00
runTests={runTests}
2023-03-08 17:33:27 -08:00
onTestSelected={setSelectedTest}
isVisible={!settingsVisible} />
{settingsVisible && <SettingsView projects={projects} setProjects={setProjects} onClose={() => setSettingsVisible(false)}></SettingsView>}
2023-03-07 14:24:50 -08:00
</div>
</SplitView>
<div className='status-line'>
2023-03-07 12:43:16 -08:00
Running: {progress.total} tests | {progress.passed} passed | {progress.failed} failed
</div>
2023-03-07 14:24:50 -08:00
</div>;
2023-03-07 12:43:16 -08:00
};
2023-03-08 17:33:27 -08:00
const TreeListView = TreeView<TreeItem>;
2023-03-07 12:43:16 -08:00
export const TestList: React.FC<{
projects: Map<string, boolean>,
2023-03-07 12:43:16 -08:00
rootSuite: { value: Suite | undefined },
runTests: (testIds: string[]) => void,
isRunningTest: boolean,
isWatchingFiles: boolean,
isVisible: boolean
2023-03-08 17:33:27 -08:00
onTestSelected: (test: TestCase | undefined) => void,
}> = ({ projects, rootSuite, runTests, isRunningTest, isWatchingFiles, isVisible, onTestSelected }) => {
const [treeState, setTreeState] = React.useState<TreeState>({ expandedItems: new Map() });
2023-03-01 15:27:23 -08:00
const [filterText, setFilterText] = React.useState<string>('');
2023-03-07 12:43:16 -08:00
const [selectedTreeItemId, setSelectedTreeItemId] = React.useState<string | undefined>();
2023-03-01 15:27:23 -08:00
const inputRef = React.useRef<HTMLInputElement>(null);
React.useEffect(() => {
inputRef.current?.focus();
refreshRootSuite(true);
2023-03-01 15:27:23 -08:00
}, []);
2023-03-08 17:33:27 -08:00
const { rootItem, treeItemMap, visibleTestIds } = React.useMemo(() => {
const rootItem = createTree(rootSuite.value, projects);
filterTree(rootItem, filterText);
const treeItemMap = new Map<string, TreeItem>();
const visibleTestIds = new Set<string>();
const visit = (treeItem: TreeItem) => {
if (treeItem.kind === 'test')
visibleTestIds.add(treeItem.id);
treeItem.children?.forEach(visit);
treeItemMap.set(treeItem.id, treeItem);
};
2023-03-08 17:33:27 -08:00
visit(rootItem);
hideOnlyTests(rootItem);
return { rootItem, treeItemMap, visibleTestIds };
}, [filterText, rootSuite, projects]);
2023-03-07 12:43:16 -08:00
runVisibleTests = () => runTests([...visibleTestIds]);
2023-03-08 17:33:27 -08:00
const { selectedTreeItem } = React.useMemo(() => {
const selectedTreeItem = selectedTreeItemId ? treeItemMap.get(selectedTreeItemId) : undefined;
2023-03-08 17:33:27 -08:00
let selectedTest: TestCase | undefined;
if (selectedTreeItem?.kind === 'test')
2023-03-08 17:33:27 -08:00
selectedTest = selectedTreeItem.test;
else if (selectedTreeItem?.kind === 'case' && selectedTreeItem.tests.length === 1)
selectedTest = selectedTreeItem.tests[0];
onTestSelected(selectedTest);
return { selectedTreeItem };
}, [onTestSelected, selectedTreeItemId, treeItemMap]);
React.useEffect(() => {
sendMessageNoReply('watch', { fileName: isWatchingFiles ? fileName(selectedTreeItem) : undefined });
}, [selectedTreeItem, isWatchingFiles]);
const runTreeItem = (treeItem: TreeItem) => {
2023-03-08 17:33:27 -08:00
// expandedItems.set(treeItem.id, true);
setSelectedTreeItemId(treeItem.id);
runTests(collectTestIds(treeItem));
};
runWatchedTests = () => {
runTests(collectTestIds(selectedTreeItem));
};
if (!isVisible)
return <></>;
2023-03-07 12:43:16 -08:00
return <div className='vbox'>
<Toolbar>
<input ref={inputRef} type='search' placeholder='Filter (e.g. text, @tag)' spellCheck={false} value={filterText}
onChange={e => {
setFilterText(e.target.value);
2023-03-01 15:27:23 -08:00
}}
2023-03-07 12:43:16 -08:00
onKeyDown={e => {
if (e.key === 'Enter')
runVisibleTests();
}}></input>
</Toolbar>
2023-03-08 17:33:27 -08:00
<TreeListView
treeState={treeState}
setTreeState={setTreeState}
rootItem={rootItem}
render={treeItem => {
2023-03-07 12:43:16 -08:00
return <div className='hbox watch-mode-list-item'>
<div className='watch-mode-list-item-title'>{treeItem.title}</div>
<ToolbarButton icon='play' title='Run' onClick={() => runTreeItem(treeItem)} disabled={isRunningTest}></ToolbarButton>
<ToolbarButton icon='go-to-file' title='Open in VS Code' onClick={() => sendMessageNoReply('open', { location: locationToOpen(treeItem) })}></ToolbarButton>
2023-03-07 12:43:16 -08:00
</div>;
}}
2023-03-08 17:33:27 -08:00
icon={treeItem => {
if (treeItem.status === 'running')
return 'codicon-loading';
if (treeItem.status === 'failed')
return 'codicon-error';
if (treeItem.status === 'passed')
return 'codicon-check';
2023-03-08 17:33:27 -08:00
return 'codicon-circle-outline';
2023-03-07 12:43:16 -08:00
}}
selectedItem={selectedTreeItem}
onAccepted={runTreeItem}
2023-03-08 17:33:27 -08:00
onSelected={treeItem => {
2023-03-07 12:43:16 -08:00
setSelectedTreeItemId(treeItem.id);
}}
2023-03-07 14:24:50 -08:00
noItemsMessage='No tests' />
</div>;
};
export const SettingsView: React.FC<{
projects: Map<string, boolean>,
setProjects: (projectNames: Map<string, boolean>) => void,
2023-03-07 14:24:50 -08:00
onClose: () => void,
}> = ({ projects, setProjects, onClose }) => {
2023-03-07 14:24:50 -08:00
return <div className='vbox'>
<div className='hbox' style={{ flex: 'none' }}>
<div className='section-title' style={{ marginTop: 10 }}>Projects</div>
<div className='spacer'></div>
<ToolbarButton icon='close' title='Close settings' toggled={false} onClick={onClose}></ToolbarButton>
</div>
{[...projects.entries()].map(([projectName, value]) => {
return <div style={{ display: 'flex', alignItems: 'center', lineHeight: '24px', marginLeft: 5 }}>
<input id={`project-${projectName}`} type='checkbox' checked={value} style={{ cursor: 'pointer' }} onClick={() => {
const copy = new Map(projects);
copy.set(projectName, !copy.get(projectName));
if (![...copy.values()].includes(true))
copy.set(projectName, true);
setProjects(copy);
}}/>
<label htmlFor={`project-${projectName}`} style={{ cursor: 'pointer' }}>
2023-03-07 14:24:50 -08:00
{projectName}
</label>
</div>;
})}
<div className='section-title'>Appearance</div>
<div style={{ marginLeft: 3 }}>
<ToolbarButton icon='color-mode' title='Toggle color mode' toggled={false} onClick={() => toggleTheme()}>Toggle color mode</ToolbarButton>
</div>
2023-03-07 12:43:16 -08:00
</div>;
2023-03-01 15:27:23 -08:00
};
export const TraceView: React.FC<{
2023-03-08 17:33:27 -08:00
test: TestCase | undefined,
}> = ({ test }) => {
2023-03-01 15:27:23 -08:00
const [model, setModel] = React.useState<MultiTraceModel | undefined>();
const [stepsProgress, setStepsProgress] = React.useState(0);
updateStepsProgress = () => setStepsProgress(stepsProgress + 1);
2023-03-01 15:27:23 -08:00
React.useEffect(() => {
(async () => {
2023-03-08 17:33:27 -08:00
if (!test) {
2023-03-01 15:27:23 -08:00
setModel(undefined);
return;
}
2023-03-08 17:33:27 -08:00
const result = test.results?.[0];
if (result) {
2023-03-01 15:27:23 -08:00
const attachment = result.attachments.find(a => a.name === 'trace');
if (attachment && attachment.path)
loadSingleTraceFile(attachment.path).then(setModel);
else
setModel(stepsToModel(result));
} else {
setModel(undefined);
2023-03-01 15:27:23 -08:00
}
})();
2023-03-08 17:33:27 -08:00
}, [test, stepsProgress]);
2023-03-01 15:27:23 -08:00
2023-03-07 14:24:50 -08:00
const xterm = <XtermWrapper source={xtermDataSource}></XtermWrapper>;
return <Workbench model={model} output={xterm} rightToolbar={[
<ToolbarButton icon='trash' title='Clear output' onClick={() => xtermDataSource.clear()}></ToolbarButton>,
]}/>;
2023-03-01 15:27:23 -08:00
};
declare global {
interface Window {
binding(data: any): Promise<void>;
}
}
let receiver: TeleReporterReceiver | undefined;
const refreshRootSuite = (eraseResults: boolean) => {
if (!eraseResults) {
sendMessageNoReply('list');
return;
}
let rootSuite: Suite;
const progress: Progress = {
total: 0,
passed: 0,
failed: 0,
};
receiver = new TeleReporterReceiver({
onBegin: (config: FullConfig, suite: Suite) => {
if (!rootSuite)
rootSuite = suite;
progress.passed = 0;
progress.failed = 0;
updateRootSuite(rootSuite, progress);
},
onTestBegin: () => {
updateRootSuite(rootSuite, progress);
},
onTestEnd: (test: TestCase) => {
if (test.outcome() === 'unexpected')
++progress.failed;
else
++progress.passed;
updateRootSuite(rootSuite, progress);
// This will update selected trace viewer.
updateStepsProgress();
},
onStepBegin: () => {
updateStepsProgress();
},
onStepEnd: () => {
updateStepsProgress();
},
});
sendMessageNoReply('list');
};
2023-03-01 15:27:23 -08:00
(window as any).dispatch = (message: any) => {
if (message.method === 'listChanged') {
refreshRootSuite(false);
return;
}
2023-03-07 14:24:50 -08:00
if (message.method === 'fileChanged') {
runWatchedTests();
return;
}
if (message.method === 'stdio') {
2023-03-07 14:24:50 -08:00
if (message.params.buffer) {
const data = atob(message.params.buffer);
xtermDataSource.write(data);
} else {
xtermDataSource.write(message.params.text);
}
return;
2023-03-07 14:24:50 -08:00
}
receiver?.dispatch(message);
};
const sendMessage = async (method: string, params: any) => {
await (window as any).sendMessage({ method, params });
2023-03-01 15:27:23 -08:00
};
const sendMessageNoReply = (method: string, params?: any) => {
sendMessage(method, params).catch((e: Error) => {
// eslint-disable-next-line no-console
console.error(e);
2023-03-01 15:27:23 -08:00
});
};
const fileName = (treeItem?: TreeItem): string | undefined => {
return treeItem?.location.file;
};
const locationToOpen = (treeItem?: TreeItem) => {
if (!treeItem)
return;
return treeItem.location.file + ':' + treeItem.location.line;
};
const collectTestIds = (treeItem?: TreeItem): string[] => {
if (!treeItem)
return [];
const testIds: string[] = [];
const visit = (treeItem: TreeItem) => {
2023-03-08 17:33:27 -08:00
if (treeItem.kind === 'case')
testIds.push(...treeItem.tests.map(t => t.id));
else if (treeItem.kind === 'test')
testIds.push(treeItem.id);
2023-03-08 17:33:27 -08:00
else
treeItem.children?.forEach(visit);
};
visit(treeItem);
return testIds;
};
type Progress = {
total: number;
passed: number;
failed: number;
};
type TreeItemBase = {
kind: 'root' | 'group' | 'case' | 'test',
id: string;
title: string;
location: Location,
2023-03-08 17:33:27 -08:00
children: TreeItem[];
status: 'none' | 'running' | 'passed' | 'failed';
};
type GroupItem = TreeItemBase & {
kind: 'group',
children: (TestCaseItem | GroupItem)[];
};
type TestCaseItem = TreeItemBase & {
kind: 'case',
2023-03-08 17:33:27 -08:00
tests: TestCase[];
};
type TestItem = TreeItemBase & {
kind: 'test',
test: TestCase;
};
type TreeItem = GroupItem | TestCaseItem | TestItem;
function createTree(rootSuite: Suite | undefined, projects: Map<string, boolean>): GroupItem {
const rootItem: GroupItem = {
kind: 'group',
2023-03-08 17:33:27 -08:00
id: 'root',
title: '',
location: { file: '', line: 0, column: 0 },
2023-03-08 17:33:27 -08:00
children: [],
status: 'none',
2023-03-08 17:33:27 -08:00
};
const visitSuite = (projectName: string, parentSuite: Suite, parentGroup: GroupItem) => {
for (const suite of parentSuite.suites) {
const title = suite.title;
let group = parentGroup.children.find(item => item.title === title) as GroupItem | undefined;
if (!group) {
group = {
kind: 'group',
id: parentGroup.id + '\x1e' + title,
title,
location: suite.location!,
children: [],
status: 'none',
};
parentGroup.children.push(group);
}
visitSuite(projectName, suite, group);
}
for (const test of parentSuite.tests) {
const title = test.title;
let testCaseItem = parentGroup.children.find(t => t.title === title) as TestCaseItem;
if (!testCaseItem) {
testCaseItem = {
kind: 'case',
id: parentGroup.id + '\x1e' + title,
title,
2023-03-08 17:33:27 -08:00
children: [],
tests: [],
location: test.location,
status: 'none',
};
parentGroup.children.push(testCaseItem);
}
let status: 'none' | 'running' | 'passed' | 'failed' = 'none';
if (test.results.some(r => r.duration === -1))
status = 'running';
else if (test.results.length && test.outcome() !== 'expected')
status = 'failed';
else if (test.outcome() === 'expected')
status = 'passed';
testCaseItem.tests.push(test);
testCaseItem.children.push({
kind: 'test',
id: test.id,
title: projectName,
location: test.location!,
test,
children: [],
status,
});
}
};
for (const projectSuite of rootSuite?.suites || []) {
if (!projects.get(projectSuite.title))
continue;
visitSuite(projectSuite.title, projectSuite, rootItem);
}
const propagateStatus = (treeItem: TreeItem) => {
for (const child of treeItem.children)
propagateStatus(child);
let allPassed = treeItem.children.length > 0;
let hasFailed = false;
let hasRunning = false;
for (const child of treeItem.children) {
allPassed = allPassed && child.status === 'passed';
hasFailed = hasFailed || child.status === 'failed';
hasRunning = hasRunning || child.status === 'running';
}
if (hasRunning)
treeItem.status = 'running';
else if (hasFailed)
treeItem.status = 'failed';
else if (allPassed)
treeItem.status = 'passed';
};
propagateStatus(rootItem);
2023-03-08 17:33:27 -08:00
return rootItem;
}
function filterTree(rootItem: GroupItem, filterText: string) {
const trimmedFilterText = filterText.trim();
const filterTokens = trimmedFilterText.toLowerCase().split(' ');
const visit = (treeItem: GroupItem) => {
const newChildren: (GroupItem | TestCaseItem)[] = [];
for (const child of treeItem.children) {
if (child.kind === 'case') {
const title = child.tests[0].titlePath().join(' ').toLowerCase();
if (filterTokens.every(token => title.includes(token)))
newChildren.push(child);
} else {
visit(child);
if (child.children.length)
newChildren.push(child);
}
}
treeItem.children = newChildren;
};
visit(rootItem);
}
function hideOnlyTests(rootItem: GroupItem) {
2023-03-08 17:33:27 -08:00
const visit = (treeItem: TreeItem) => {
if (treeItem.kind === 'case' && treeItem.children.length === 1)
treeItem.children = [];
else
treeItem.children.forEach(visit);
};
visit(rootItem);
}
async function loadSingleTraceFile(url: string): Promise<MultiTraceModel> {
const params = new URLSearchParams();
params.set('trace', url);
const response = await fetch(`contexts?${params.toString()}`);
const contextEntries = await response.json() as ContextEntry[];
return new MultiTraceModel(contextEntries);
}
function stepsToModel(result: TestResult): MultiTraceModel {
let startTime = Number.MAX_VALUE;
let endTime = Number.MIN_VALUE;
const actions: trace.ActionTraceEvent[] = [];
const flatSteps: TestStep[] = [];
const visit = (step: TestStep) => {
flatSteps.push(step);
step.steps.forEach(visit);
};
result.steps.forEach(visit);
for (const step of flatSteps) {
let callId: string;
if (step.category === 'pw:api')
callId = `call@${actions.length}`;
else if (step.category === 'expect')
callId = `expect@${actions.length}`;
else
continue;
const action: trace.ActionTraceEvent = {
type: 'action',
callId,
startTime: step.startTime.getTime(),
endTime: step.startTime.getTime() + step.duration,
apiName: step.title,
class: '',
method: '',
params: {},
wallTime: step.startTime.getTime(),
log: [],
snapshots: [],
error: step.error ? { name: 'Error', message: step.error.message || step.error.value || '' } : undefined,
};
if (startTime > action.startTime)
startTime = action.startTime;
if (endTime < action.endTime)
endTime = action.endTime;
actions.push(action);
}
const contextEntry: ContextEntry = {
traceUrl: '',
startTime,
endTime,
browserName: '',
options: {
viewport: undefined,
deviceScaleFactor: undefined,
isMobile: undefined,
userAgent: undefined
},
pages: [],
resources: [],
actions,
events: [],
initializers: {},
hasSource: false
};
return new MultiTraceModel([contextEntry]);
}