testplane
Version:
Tests framework based on mocha and wdio
71 lines • 3.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveViewportImageForDebugIfNeeded = exports.cropMarginsToRect = exports.normalizeCropMargins = exports.isFullPage = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const geometry_1 = require("../isomorphic/geometry");
const debug_utils_1 = require("../screen-shooter/composite-image/debug-utils");
const isFullPage = (imageSize, viewportSize, calibratedArea, screenshotMode) => {
// "system ui" is something like status bar on safari mobile, or address bar at the bottom
const systemUiHeight = calibratedArea.top + (imageSize.height - (0, geometry_1.getBottom)(calibratedArea));
switch (screenshotMode) {
case "fullpage":
return true;
case "viewport":
return false;
case "auto":
return imageSize.height > viewportSize.height + systemUiHeight;
}
};
exports.isFullPage = isFullPage;
const normalizeCropMargins = (cropMargins) => {
const result = {
top: cropMargins?.top ?? 0,
right: cropMargins?.right ?? 0,
bottom: cropMargins?.bottom ?? 0,
left: cropMargins?.left ?? 0,
};
for (const side of ["top", "right", "bottom", "left"]) {
const value = result[side];
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || Math.floor(value) !== value) {
throw new Error(`Invalid cropMargins.${side} option: expected a non-negative integer, got ${String(value)}`);
}
}
return result;
};
exports.normalizeCropMargins = normalizeCropMargins;
const cropMarginsToRect = (imageArea, cropMargins) => {
const margins = (0, exports.normalizeCropMargins)(cropMargins);
return {
top: margins.top,
left: margins.left,
width: imageArea.width - margins.left - margins.right,
height: imageArea.height - margins.top - margins.bottom,
};
};
exports.cropMarginsToRect = cropMarginsToRect;
async function saveViewportImageForDebugIfNeeded(viewportImage, viewportCroppedArea, debugDir) {
if (!process.env.TESTPLANE_DEBUG_SCREENSHOTS || !debugDir) {
return;
}
try {
fs_1.default.mkdirSync(debugDir, { recursive: true });
const timestamp = String(Date.now()).padStart(13, "0");
const randomId = Math.random().toString(36).substring(2, 8);
const outputPath = path_1.default.join(debugDir, `viewport-${timestamp}-${randomId}.png`);
await (0, debug_utils_1.saveViewportImageWithDebugRects)(viewportImage, [
{
rect: viewportCroppedArea,
color: { r: 0, g: 255, b: 0, a: 255 },
},
], outputPath);
}
catch (error) {
console.warn("Failed to save camera viewport debug image: %O", error);
}
}
exports.saveViewportImageForDebugIfNeeded = saveViewportImageForDebugIfNeeded;
//# sourceMappingURL=utils.js.map