html-reporter
Version:
Html-reporter and GUI for viewing and managing results of a tests run. Currently supports Testplane and Hermione.
83 lines • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlaywrightConfigAdapter = exports.DEFAULT_BROWSER_ID = void 0;
const node_path_1 = __importDefault(require("node:path"));
const node_crypto_1 = __importDefault(require("node:crypto"));
const lodash_1 = __importDefault(require("lodash"));
exports.DEFAULT_BROWSER_ID = 'chromium';
const DEFAULT_SNAPSHOT_PATH_TEMPLATE = '{snapshotDir}/{testFileDir}/{testFileName}-snapshots/{arg}{-projectName}{-snapshotSuffix}{ext}';
class PlaywrightConfigAdapter {
static create(config) {
return new this(config);
}
constructor(config) {
this._config = config;
this._browserIds = lodash_1.default.isEmpty(this._config.projects) ? [exports.DEFAULT_BROWSER_ID] : this._config.projects.map(prj => prj.name).filter(Boolean);
}
get original() {
return this._config;
}
get tolerance() {
return 2.3;
}
get antialiasingTolerance() {
return 4;
}
get browserIds() {
return this._browserIds;
}
// used from pwt - https://github.com/microsoft/playwright/blob/v1.45.1/packages/playwright/src/worker/testInfo.ts#L452-L473
getScreenshotPath(test, stateName) {
const subPath = `${stateName}.png`;
const parsedSubPath = node_path_1.default.parse(subPath);
const parsedRelativeTestFilePath = node_path_1.default.parse(test.file);
const currProject = (this._config.projects || []).find(prj => prj.name === test.browserId) || {};
const projectNamePathSegment = sanitizeForFilePath(test.browserId);
const snapshotPathTemplate = currProject.snapshotPathTemplate || this._config.snapshotPathTemplate || DEFAULT_SNAPSHOT_PATH_TEMPLATE;
const testDir = node_path_1.default.resolve(currProject.testDir || this._config.testDir || '');
let snapshotDir = currProject.snapshotDir || this._config.snapshotDir;
snapshotDir = snapshotDir ? node_path_1.default.resolve(snapshotDir) : testDir;
const snapshotSuffix = process.platform;
const snapshotPath = snapshotPathTemplate
.replace(/\{(.)?testDir\}/g, '$1' + testDir)
.replace(/\{(.)?snapshotDir\}/g, '$1' + snapshotDir)
.replace(/\{(.)?snapshotSuffix\}/g, snapshotSuffix ? '$1' + snapshotSuffix : '')
.replace(/\{(.)?testFileDir\}/g, '$1' + parsedRelativeTestFilePath.dir)
.replace(/\{(.)?platform\}/g, '$1' + process.platform)
.replace(/\{(.)?projectName\}/g, projectNamePathSegment ? '$1' + projectNamePathSegment : '')
.replace(/\{(.)?testName\}/g, '$1' + fsSanitizedTestName(test.titlePath))
.replace(/\{(.)?testFileName\}/g, '$1' + parsedRelativeTestFilePath.base)
.replace(/\{(.)?testFilePath\}/g, '$1' + test.file)
.replace(/\{(.)?arg\}/g, '$1' + node_path_1.default.join(parsedSubPath.dir, parsedSubPath.name))
.replace(/\{(.)?ext\}/g, parsedSubPath.ext ? '$1' + parsedSubPath.ext : '');
return node_path_1.default.normalize(node_path_1.default.resolve(snapshotPath));
}
}
exports.PlaywrightConfigAdapter = PlaywrightConfigAdapter;
function sanitizeForFilePath(s) {
// eslint-disable-next-line no-control-regex
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
}
function fsSanitizedTestName(titlePath) {
const fullTitleWithoutSpec = titlePath.join(' ');
return sanitizeForFilePath(trimLongString(fullTitleWithoutSpec));
}
function trimLongString(s, 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);
}
function calculateSha1(buffer) {
const hash = node_crypto_1.default.createHash('sha1');
hash.update(buffer);
return hash.digest('hex');
}
//# sourceMappingURL=playwright.js.map