2021-06-06 17:09:53 -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.
|
|
|
|
*/
|
|
|
|
|
2022-03-29 15:19:31 -06:00
|
|
|
import fs from 'fs';
|
2022-04-18 19:20:49 -08:00
|
|
|
import { mime } from 'playwright-core/lib/utilsBundle';
|
2022-04-08 13:22:14 -07:00
|
|
|
import util from 'util';
|
2021-07-19 12:20:24 -05:00
|
|
|
import path from 'path';
|
2021-09-13 12:09:38 -04:00
|
|
|
import url from 'url';
|
2022-04-18 20:47:18 -08:00
|
|
|
import { colors, debug, minimatch } from 'playwright-core/lib/utilsBundle';
|
2021-07-19 12:20:24 -05:00
|
|
|
import type { TestError, Location } from './types';
|
2022-04-07 12:55:44 -08:00
|
|
|
import { calculateSha1, isRegExp } from 'playwright-core/lib/utils';
|
2022-02-01 19:40:44 -07:00
|
|
|
import { isInternalFileName } from 'playwright-core/lib/utils/stackTrace';
|
2022-02-23 14:17:37 -07:00
|
|
|
import { currentTestInfo } from './globals';
|
2022-04-06 13:57:14 -08:00
|
|
|
import type { ParsedStackTrace } from 'playwright-core/lib/utils/stackTrace';
|
|
|
|
import { captureStackTrace as coreCaptureStackTrace } from 'playwright-core/lib/utils/stackTrace';
|
2022-03-14 19:01:13 -06:00
|
|
|
|
2022-03-28 17:21:19 -08:00
|
|
|
export type { ParsedStackTrace };
|
2022-02-01 19:40:44 -07:00
|
|
|
|
|
|
|
const PLAYWRIGHT_CORE_PATH = path.dirname(require.resolve('playwright-core'));
|
2022-04-18 16:50:25 -08:00
|
|
|
const EXPECT_PATH = require.resolve('./expectBundle');
|
|
|
|
const EXPECT_PATH_IMPL = require.resolve('./expectBundleImpl');
|
2022-02-01 19:40:44 -07:00
|
|
|
const PLAYWRIGHT_TEST_PATH = path.join(__dirname, '..');
|
|
|
|
|
|
|
|
function filterStackTrace(e: Error) {
|
2022-08-01 13:44:59 -07:00
|
|
|
if (process.env.PWDEBUGIMPL)
|
|
|
|
return;
|
|
|
|
|
2022-02-01 19:40:44 -07:00
|
|
|
// This method filters internal stack frames using Error.prepareStackTrace
|
|
|
|
// hook. Read more about the hook: https://v8.dev/docs/stack-trace-api
|
|
|
|
//
|
|
|
|
// NOTE: Error.prepareStackTrace will only be called if `e.stack` has not
|
|
|
|
// been accessed before. This is the case for Jest Expect and simple throw
|
|
|
|
// statements.
|
|
|
|
//
|
|
|
|
// If `e.stack` has been accessed, this method will be NOOP.
|
|
|
|
const oldPrepare = Error.prepareStackTrace;
|
|
|
|
const stackFormatter = oldPrepare || ((error, structuredStackTrace) => [
|
|
|
|
`${error.name}: ${error.message}`,
|
|
|
|
...structuredStackTrace.map(callSite => ' at ' + callSite.toString()),
|
|
|
|
].join('\n'));
|
|
|
|
Error.prepareStackTrace = (error, structuredStackTrace) => {
|
|
|
|
return stackFormatter(error, structuredStackTrace.filter(callSite => {
|
|
|
|
const fileName = callSite.getFileName();
|
|
|
|
const functionName = callSite.getFunctionName() || undefined;
|
|
|
|
if (!fileName)
|
|
|
|
return true;
|
2022-02-14 15:33:14 -07:00
|
|
|
return !fileName.startsWith(PLAYWRIGHT_TEST_PATH) &&
|
|
|
|
!fileName.startsWith(PLAYWRIGHT_CORE_PATH) &&
|
|
|
|
!isInternalFileName(fileName, functionName);
|
2022-02-01 19:40:44 -07:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
// eslint-disable-next-line
|
|
|
|
e.stack; // trigger Error.prepareStackTrace
|
|
|
|
Error.prepareStackTrace = oldPrepare;
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2022-03-14 19:01:13 -06:00
|
|
|
export function captureStackTrace(customApiName?: string): ParsedStackTrace {
|
|
|
|
const stackTrace: ParsedStackTrace = coreCaptureStackTrace();
|
|
|
|
const frames = [];
|
|
|
|
const frameTexts = [];
|
|
|
|
for (let i = 0; i < stackTrace.frames.length; ++i) {
|
|
|
|
const frame = stackTrace.frames[i];
|
2022-04-18 16:50:25 -08:00
|
|
|
if (frame.file === EXPECT_PATH || frame.file === EXPECT_PATH_IMPL)
|
2022-03-14 19:01:13 -06:00
|
|
|
continue;
|
|
|
|
frames.push(frame);
|
|
|
|
frameTexts.push(stackTrace.frameTexts[i]);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
allFrames: stackTrace.allFrames,
|
|
|
|
frames,
|
|
|
|
frameTexts,
|
|
|
|
apiName: customApiName ?? stackTrace.apiName,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:53 -07:00
|
|
|
export function serializeError(error: Error | any): TestError {
|
|
|
|
if (error instanceof Error) {
|
2022-02-01 19:40:44 -07:00
|
|
|
filterStackTrace(error);
|
2021-06-06 17:09:53 -07:00
|
|
|
return {
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
value: util.inspect(error)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Matcher = (value: string) => boolean;
|
|
|
|
|
2022-08-04 08:09:54 -07:00
|
|
|
export type TestFileFilter = {
|
|
|
|
re?: RegExp;
|
|
|
|
exact?: string;
|
2021-06-24 10:02:34 +02:00
|
|
|
line: number | null;
|
2022-04-07 22:45:45 +02:00
|
|
|
column: number | null;
|
2021-06-24 10:02:34 +02:00
|
|
|
};
|
|
|
|
|
2021-09-13 12:09:38 -04:00
|
|
|
export function createFileMatcher(patterns: string | RegExp | (string | RegExp)[]): Matcher {
|
2021-06-06 17:09:53 -07:00
|
|
|
const reList: RegExp[] = [];
|
|
|
|
const filePatterns: string[] = [];
|
|
|
|
for (const pattern of Array.isArray(patterns) ? patterns : [patterns]) {
|
|
|
|
if (isRegExp(pattern)) {
|
|
|
|
reList.push(pattern);
|
|
|
|
} else {
|
|
|
|
if (!pattern.startsWith('**/') && !pattern.startsWith('**/'))
|
|
|
|
filePatterns.push('**/' + pattern);
|
|
|
|
else
|
|
|
|
filePatterns.push(pattern);
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 12:09:38 -04:00
|
|
|
return (filePath: string) => {
|
2021-06-06 17:09:53 -07:00
|
|
|
for (const re of reList) {
|
|
|
|
re.lastIndex = 0;
|
2021-09-13 12:09:38 -04:00
|
|
|
if (re.test(filePath))
|
2021-06-06 17:09:53 -07:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-01 16:09:41 -03:00
|
|
|
// Windows might still receive unix style paths from Cygwin or Git Bash.
|
2021-09-13 12:09:38 -04:00
|
|
|
// Check against the file url as well.
|
|
|
|
if (path.sep === '\\') {
|
|
|
|
const fileURL = url.pathToFileURL(filePath).href;
|
|
|
|
for (const re of reList) {
|
|
|
|
re.lastIndex = 0;
|
|
|
|
if (re.test(fileURL))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
for (const pattern of filePatterns) {
|
2021-09-13 12:09:38 -04:00
|
|
|
if (minimatch(filePath, pattern, { nocase: true, dot: true }))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-13 13:56:03 -05:00
|
|
|
export function createTitleMatcher(patterns: RegExp | RegExp[]): Matcher {
|
2021-09-13 12:09:38 -04:00
|
|
|
const reList = Array.isArray(patterns) ? patterns : [patterns];
|
|
|
|
return (value: string) => {
|
|
|
|
for (const re of reList) {
|
|
|
|
re.lastIndex = 0;
|
|
|
|
if (re.test(value))
|
2021-06-06 17:09:53 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:51:45 -07:00
|
|
|
export function mergeObjects<A extends object, B extends object>(a: A | undefined | void, b: B | undefined | void): A & B {
|
|
|
|
const result = { ...a } as any;
|
|
|
|
if (!Object.is(b, undefined)) {
|
|
|
|
for (const [name, value] of Object.entries(b as B)) {
|
|
|
|
if (!Object.is(value, undefined))
|
|
|
|
result[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result as any;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function forceRegExp(pattern: string): RegExp {
|
|
|
|
const match = pattern.match(/^\/(.*)\/([gi]*)$/);
|
|
|
|
if (match)
|
|
|
|
return new RegExp(match[1], match[2]);
|
|
|
|
return new RegExp(pattern, 'g');
|
|
|
|
}
|
2021-07-19 12:20:24 -05:00
|
|
|
|
|
|
|
export function relativeFilePath(file: string): string {
|
|
|
|
if (!path.isAbsolute(file))
|
|
|
|
return file;
|
|
|
|
return path.relative(process.cwd(), file);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatLocation(location: Location) {
|
|
|
|
return relativeFilePath(location.file) + ':' + location.line + ':' + location.column;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function errorWithFile(file: string, message: string) {
|
|
|
|
return new Error(`${relativeFilePath(file)}: ${message}`);
|
|
|
|
}
|
2021-07-22 12:34:37 -07:00
|
|
|
|
|
|
|
export function errorWithLocation(location: Location, message: string) {
|
|
|
|
return new Error(`${formatLocation(location)}: ${message}`);
|
|
|
|
}
|
2021-07-28 15:44:44 -07:00
|
|
|
|
2022-03-11 23:40:28 -07:00
|
|
|
export function expectTypes(receiver: any, types: string[], matcherName: string) {
|
|
|
|
if (typeof receiver !== 'object' || !types.includes(receiver.constructor.name)) {
|
|
|
|
const commaSeparated = types.slice();
|
|
|
|
const lastType = commaSeparated.pop();
|
|
|
|
const typesString = commaSeparated.length ? commaSeparated.join(', ') + ' or ' + lastType : lastType;
|
|
|
|
throw new Error(`${matcherName} can be only used with ${typesString} object${types.length > 1 ? 's' : ''}`);
|
|
|
|
}
|
2021-07-28 15:44:44 -07:00
|
|
|
}
|
2021-08-11 00:24:35 -04:00
|
|
|
|
|
|
|
export function sanitizeForFilePath(s: string) {
|
2021-11-24 19:36:38 -05:00
|
|
|
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
2021-08-11 00:24:35 -04:00
|
|
|
}
|
2021-09-23 11:56:39 -04:00
|
|
|
|
2021-12-13 13:56:03 -05:00
|
|
|
export function trimLongString(s: string, length = 100) {
|
|
|
|
if (s.length <= length)
|
|
|
|
return s;
|
|
|
|
const hash = calculateSha1(s);
|
|
|
|
const middle = `-${hash.substring(0, 5)}-`;
|
|
|
|
const start = Math.floor((length - middle.length) / 2);
|
|
|
|
const end = length - middle.length - start;
|
|
|
|
return s.substring(0, start) + middle + s.slice(-end);
|
|
|
|
}
|
|
|
|
|
2021-10-01 11:15:44 -05:00
|
|
|
export function addSuffixToFilePath(filePath: string, suffix: string, customExtension?: string, sanitize = false): string {
|
|
|
|
const dirname = path.dirname(filePath);
|
|
|
|
const ext = path.extname(filePath);
|
|
|
|
const name = path.basename(filePath, ext);
|
|
|
|
const base = path.join(dirname, name);
|
|
|
|
return (sanitize ? sanitizeForFilePath(base) : base) + suffix + (customExtension || ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns absolute path contained within parent directory.
|
|
|
|
*/
|
|
|
|
export function getContainedPath(parentPath: string, subPath: string = ''): string | null {
|
|
|
|
const resolvedPath = path.resolve(parentPath, subPath);
|
|
|
|
if (resolvedPath === parentPath || resolvedPath.startsWith(parentPath + path.sep)) return resolvedPath;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:56:39 -04:00
|
|
|
export const debugTest = debug('pw:test');
|
2022-02-23 14:17:37 -07:00
|
|
|
|
|
|
|
export function callLogText(log: string[] | undefined): string {
|
|
|
|
if (!log)
|
|
|
|
return '';
|
|
|
|
return `
|
|
|
|
Call log:
|
|
|
|
${colors.dim('- ' + (log || []).join('\n - '))}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function currentExpectTimeout(options: { timeout?: number }) {
|
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
if (options.timeout !== undefined)
|
|
|
|
return options.timeout;
|
2022-04-14 16:58:01 -07:00
|
|
|
let defaultExpectTimeout = testInfo?.project._expect?.timeout;
|
2022-02-23 14:17:37 -07:00
|
|
|
if (typeof defaultExpectTimeout === 'undefined')
|
|
|
|
defaultExpectTimeout = 5000;
|
|
|
|
return defaultExpectTimeout;
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:19:31 -06:00
|
|
|
const folderToPackageJsonPath = new Map<string, string>();
|
|
|
|
|
|
|
|
export function getPackageJsonPath(folderPath: string): string {
|
|
|
|
const cached = folderToPackageJsonPath.get(folderPath);
|
|
|
|
if (cached !== undefined)
|
|
|
|
return cached;
|
|
|
|
|
|
|
|
const packageJsonPath = path.join(folderPath, 'package.json');
|
|
|
|
if (fs.existsSync(packageJsonPath)) {
|
|
|
|
folderToPackageJsonPath.set(folderPath, packageJsonPath);
|
|
|
|
return packageJsonPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentFolder = path.dirname(folderPath);
|
|
|
|
if (folderPath === parentFolder) {
|
|
|
|
folderToPackageJsonPath.set(folderPath, '');
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = getPackageJsonPath(parentFolder);
|
|
|
|
folderToPackageJsonPath.set(folderPath, result);
|
|
|
|
return result;
|
|
|
|
}
|
2022-04-08 13:22:14 -07:00
|
|
|
|
|
|
|
export async function normalizeAndSaveAttachment(outputPath: string, name: string, options: { path?: string, body?: string | Buffer, contentType?: string } = {}): Promise<{ name: string; path?: string | undefined; body?: Buffer | undefined; contentType: string; }> {
|
|
|
|
if ((options.path !== undefined ? 1 : 0) + (options.body !== undefined ? 1 : 0) !== 1)
|
|
|
|
throw new Error(`Exactly one of "path" and "body" must be specified`);
|
|
|
|
if (options.path !== undefined) {
|
|
|
|
const hash = calculateSha1(options.path);
|
|
|
|
const dest = path.join(outputPath, 'attachments', hash + path.extname(options.path));
|
|
|
|
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
|
|
|
|
await fs.promises.copyFile(options.path, dest);
|
|
|
|
const contentType = options.contentType ?? (mime.getType(path.basename(options.path)) || 'application/octet-stream');
|
|
|
|
return { name, contentType, path: dest };
|
|
|
|
} else {
|
|
|
|
const contentType = options.contentType ?? (typeof options.body === 'string' ? 'text/plain' : 'application/octet-stream');
|
|
|
|
return { name, contentType, body: typeof options.body === 'string' ? Buffer.from(options.body) : options.body };
|
|
|
|
}
|
|
|
|
}
|