UNPKG

testplane

Version:

Tests framework based on mocha and wdio

147 lines 6.06 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Calibrator = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const looks_same_1 = __importDefault(require("looks-same")); const core_error_1 = require("./core-error"); const isomorphic_1 = require("./isomorphic"); const logger = __importStar(require("../utils/logger")); const node_os_1 = __importDefault(require("node:os")); const debug_1 = __importDefault(require("debug")); const debug = (0, debug_1.default)("testplane:screenshots:calibrator"); class Calibrator { constructor() { this._cache = {}; this._script = fs_1.default.readFileSync(path_1.default.join(__dirname, "client-scripts", "calibrate.js"), "utf8"); } async calibrate(browser) { if (this._cache[browser.id]) { return this._cache[browser.id]; } debug("calibrating browser %s", browser.id); await browser.open("about:blank"); const features = await browser.evalScript(this._script); debug("features: %O", features); const image = await browser.captureViewportImage(); const { innerWidth, pixelRatio } = features; const hasPixelRatio = Boolean(pixelRatio && pixelRatio > 1.0); const screenshotSize = image.getSize(); const searchColor = image.hasICCPChunk ? await image.getRGB(Math.floor(screenshotSize.width / 2), Math.floor(screenshotSize.height / 2)) : { R: 148, G: 250, B: 0 }; const imageFeatures = await this._findMarkerAreaInImage(image, searchColor); if (!imageFeatures) { const screenshotPath = path_1.default.join(node_os_1.default.tmpdir(), "testplane-calibration-page.png"); await image.save(screenshotPath); logger.error("Could not calibrate, because marker area was not found. See calibration page screenshot for details: " + screenshotPath); await image.save(screenshotPath); throw new core_error_1.CoreError("Could not calibrate. This could be due to calibration page has failed to open properly"); } const calibratedFeatures = { ...features, viewportArea: imageFeatures, screenshotSize, usePixelRatio: hasPixelRatio && imageFeatures.width > innerWidth, }; this._cache[browser.id] = calibratedFeatures; return calibratedFeatures; } async _findMarkerAreaInImage(image, searchColor) { const imageHeight = image.getSize().height; let topPart = null; for (let y = 0; y < imageHeight; y++) { const result = await findMarkerXBandInRow(y, image, searchColor); if (result) { topPart = { top: y, left: result.left, width: result.width, height: (0, isomorphic_1.getHeight)(y, imageHeight), }; break; } } if (topPart === null) { return null; } for (let y = (imageHeight - 1); y >= 0; y--) { const result = await findMarkerXBandInRow(y, image, searchColor); if (result) { const bottomPart = { top: 0, left: result.left, width: result.width, height: (0, isomorphic_1.getHeight)(0, (y + 1)), }; return (0, isomorphic_1.getIntersection)(topPart, bottomPart); } } return null; } } exports.Calibrator = Calibrator; async function findMarkerXBandInRow(row, image, searchColor) { const markerStart = await findMarkerStartInRow(row, image, searchColor); if (markerStart === null) { return null; } const markerEnd = await findMarkerEndInRow(row, image, searchColor); if (markerEnd === null) { return null; } return { left: markerStart, width: (0, isomorphic_1.getWidth)(markerStart, (markerEnd + 1)), }; } async function isMarkerColorAtPoint(image, x, y, searchColor) { const color = await image.getRGB(x, y); return looks_same_1.default.colors(color, searchColor); } async function findMarkerStartInRow(row, image, searchColor) { const imageWidth = image.getSize().width; for (let x = 0; x < imageWidth; x++) { if (await isMarkerColorAtPoint(image, x, row, searchColor)) { return x; } } return null; } async function findMarkerEndInRow(row, image, searchColor) { const imageWidth = image.getSize().width; for (let x = (imageWidth - 1); x >= 0; x--) { if (await isMarkerColorAtPoint(image, x, row, searchColor)) { return x; } } return null; } //# sourceMappingURL=calibrator.js.map