2023-10-24 09:35:07 -07: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.
|
|
|
|
*/
|
|
|
|
|
2024-10-31 21:42:06 -07:00
|
|
|
import { ansi2html } from '@web/ansi2html';
|
2023-10-24 09:35:07 -07:00
|
|
|
import * as React from 'react';
|
|
|
|
import './testErrorView.css';
|
2024-10-10 16:49:17 -07:00
|
|
|
import type { ImageDiff } from '@web/shared/imageDiffView';
|
|
|
|
import { ImageDiffView } from '@web/shared/imageDiffView';
|
2025-02-10 15:02:19 +01:00
|
|
|
import type { TestResult } from './types';
|
|
|
|
import { fixTestPrompt } from '@web/components/prompts';
|
2025-02-26 08:40:30 -08:00
|
|
|
import { useHTMLReport } from './reportContext';
|
|
|
|
import type { MetadataWithCommitInfo } from '@playwright/isomorphic/types';
|
2023-10-24 09:35:07 -07:00
|
|
|
|
2025-02-26 08:40:30 -08:00
|
|
|
export const TestErrorView: React.FC<{
|
|
|
|
error: string;
|
|
|
|
testId?: string;
|
|
|
|
result?: TestResult
|
|
|
|
}> = ({ error, testId, result }) => {
|
2025-02-10 15:02:19 +01:00
|
|
|
return (
|
|
|
|
<CodeSnippet code={error} testId={testId}>
|
2025-02-21 13:49:23 -08:00
|
|
|
<div style={{ float: 'right', margin: 10 }}>
|
2025-02-10 15:02:19 +01:00
|
|
|
<PromptButton error={error} result={result} />
|
|
|
|
</div>
|
|
|
|
</CodeSnippet>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CodeSnippet = ({ code, children, testId }: React.PropsWithChildren<{ code: string; testId?: string; }>) => {
|
|
|
|
const html = React.useMemo(() => ansiErrorToHtml(code), [code]);
|
|
|
|
return (
|
|
|
|
<div className='test-error-container test-error-text' data-testid={testId}>
|
|
|
|
{children}
|
|
|
|
<div className='test-error-view' dangerouslySetInnerHTML={{ __html: html || '' }}></div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const PromptButton: React.FC<{
|
2023-10-24 09:35:07 -07:00
|
|
|
error: string;
|
2025-02-10 15:02:19 +01:00
|
|
|
result?: TestResult;
|
|
|
|
}> = ({ error, result }) => {
|
2025-02-26 08:40:30 -08:00
|
|
|
const report = useHTMLReport();
|
|
|
|
const commitInfo = report?.metadata as MetadataWithCommitInfo | undefined;
|
2025-02-28 13:16:58 +01:00
|
|
|
const pageSnapshot = result?.attachments.find(a => a.name === 'pageSnapshot')?.body;
|
2025-02-10 15:02:19 +01:00
|
|
|
const prompt = React.useMemo(() => fixTestPrompt(
|
|
|
|
error,
|
2025-02-26 08:40:30 -08:00
|
|
|
commitInfo?.gitDiff,
|
2025-02-28 13:16:58 +01:00
|
|
|
pageSnapshot
|
|
|
|
), [commitInfo, pageSnapshot, error]);
|
2025-02-10 15:02:19 +01:00
|
|
|
|
|
|
|
const [copied, setCopied] = React.useState(false);
|
|
|
|
|
2025-02-28 13:16:58 +01:00
|
|
|
if (!pageSnapshot)
|
|
|
|
return;
|
|
|
|
|
2025-02-10 15:02:19 +01:00
|
|
|
return <button
|
2025-02-21 13:49:23 -08:00
|
|
|
className='button'
|
|
|
|
style={{ minWidth: 100 }}
|
2025-02-10 15:02:19 +01:00
|
|
|
onClick={async () => {
|
|
|
|
await navigator.clipboard.writeText(prompt);
|
|
|
|
setCopied(true);
|
|
|
|
setTimeout(() => {
|
|
|
|
setCopied(false);
|
|
|
|
}, 3000);
|
|
|
|
}}>
|
2025-02-21 13:49:23 -08:00
|
|
|
{copied ? 'Copied' : 'Copy as Prompt'}
|
2025-02-10 15:02:19 +01:00
|
|
|
</button>;
|
2023-10-24 09:35:07 -07:00
|
|
|
};
|
|
|
|
|
2024-10-10 16:49:17 -07:00
|
|
|
export const TestScreenshotErrorView: React.FC<{
|
|
|
|
errorPrefix?: string,
|
|
|
|
diff: ImageDiff,
|
|
|
|
errorSuffix?: string,
|
|
|
|
}> = ({ errorPrefix, diff, errorSuffix }) => {
|
|
|
|
const prefixHtml = React.useMemo(() => ansiErrorToHtml(errorPrefix), [errorPrefix]);
|
|
|
|
const suffixHtml = React.useMemo(() => ansiErrorToHtml(errorSuffix), [errorSuffix]);
|
|
|
|
return <div data-testid='test-screenshot-error-view' className='test-error-view'>
|
|
|
|
<div dangerouslySetInnerHTML={{ __html: prefixHtml || '' }} className='test-error-text' style={{ marginBottom: 20 }}></div>
|
|
|
|
<ImageDiffView key='image-diff' diff={diff} hideDetails={true}></ImageDiffView>
|
|
|
|
<div data-testid='error-suffix' dangerouslySetInnerHTML={{ __html: suffixHtml || '' }} className='test-error-text'></div>
|
|
|
|
</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
function ansiErrorToHtml(text?: string): string {
|
2024-10-31 21:42:06 -07:00
|
|
|
const defaultColors = {
|
2024-10-10 16:49:17 -07:00
|
|
|
bg: 'var(--color-canvas-subtle)',
|
|
|
|
fg: 'var(--color-fg-default)',
|
|
|
|
};
|
2024-10-31 21:42:06 -07:00
|
|
|
return ansi2html(text || '', defaultColors);
|
2023-10-24 09:35:07 -07:00
|
|
|
}
|