Innovenergy_trunk/frontend/node_modules/testcafe/lib/screenshots/capturer.js

162 lines
25 KiB
JavaScript
Raw Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const testcafe_browser_tools_1 = require("testcafe-browser-tools");
const crop_1 = require("./crop");
const async_queue_1 = require("../utils/async-queue");
const warning_message_1 = __importDefault(require("../notifications/warning-message"));
const escape_user_agent_1 = __importDefault(require("../utils/escape-user-agent"));
const correct_file_path_1 = __importDefault(require("../utils/correct-file-path"));
const promisified_functions_1 = require("../utils/promisified-functions");
const default_extension_1 = __importDefault(require("./default-extension"));
const make_dir_1 = __importDefault(require("make-dir"));
class Capturer {
// TODO: refactor to use dictionary
constructor(baseScreenshotsPath, testEntry, connection, pathPattern, fullPage, thumbnails, warningLog, tempDirectoryPath, autoTakeOnFails) {
this.enabled = !!baseScreenshotsPath;
this.baseScreenshotsPath = baseScreenshotsPath;
this.testEntry = testEntry;
this.provider = connection.provider;
this.browserId = connection.id;
this.warningLog = warningLog;
this.pathPattern = pathPattern;
this.fullPage = fullPage;
this.thumbnails = thumbnails;
this.tempDirectoryPath = tempDirectoryPath;
this.autoTakeOnFails = autoTakeOnFails;
}
static _getDimensionWithoutScrollbar(fullDimension, documentDimension, bodyDimension) {
if (bodyDimension > fullDimension)
return documentDimension;
if (documentDimension > fullDimension)
return bodyDimension;
return Math.max(documentDimension, bodyDimension);
}
static _getCropDimensions(cropDimensions, pageDimensions) {
if (!cropDimensions || !pageDimensions)
return null;
const { dpr } = pageDimensions;
const { top, left, bottom, right } = cropDimensions;
return {
top: Math.round(top * dpr),
left: Math.round(left * dpr),
bottom: Math.round(bottom * dpr),
right: Math.round(right * dpr),
};
}
static _getClientAreaDimensions(pageDimensions) {
if (!pageDimensions)
return null;
const { innerWidth, documentWidth, bodyWidth, innerHeight, documentHeight, bodyHeight, dpr } = pageDimensions;
return {
width: Math.floor(Capturer._getDimensionWithoutScrollbar(innerWidth, documentWidth, bodyWidth) * dpr),
height: Math.floor(Capturer._getDimensionWithoutScrollbar(innerHeight, documentHeight, bodyHeight) * dpr),
};
}
static async _isScreenshotCaptured(screenshotPath) {
try {
const stats = await (0, promisified_functions_1.stat)(screenshotPath);
return stats.isFile();
}
catch (e) {
return false;
}
}
_joinWithBaseScreenshotPath(path) {
return (0, path_1.join)(this.baseScreenshotsPath, path);
}
_incrementFileIndexes(forError) {
if (forError)
this.pathPattern.data.errorFileIndex++;
else
this.pathPattern.data.fileIndex++;
}
_getCustomScreenshotPath(customPath) {
const correctedCustomPath = (0, correct_file_path_1.default)(customPath, default_extension_1.default);
return this._joinWithBaseScreenshotPath(correctedCustomPath);
}
_getScreenshotPath(forError) {
const path = this.pathPattern.getPath(forError);
this._incrementFileIndexes(forError);
return this._joinWithBaseScreenshotPath(path);
}
_getThumbnailPath(screenshotPath) {
const imageName = (0, path_1.basename)(screenshotPath);
const imageDir = (0, path_1.dirname)(screenshotPath);
return (0, path_1.join)(imageDir, 'thumbnails', imageName);
}
async _takeScreenshot({ filePath, pageWidth, pageHeight, fullPage = this.fullPage }) {
await this.provider.takeScreenshot(this.browserId, filePath, pageWidth, pageHeight, fullPage);
}
async _capture(forError, { actionId, failedActionId, pageDimensions, cropDimensions, markSeed, customPath, fullPage, thumbnails } = {}) {
if (!this.enabled)
return null;
thumbnails = thumbnails === void 0 ? this.thumbnails : thumbnails;
const screenshotPath = customPath ? this._getCustomScreenshotPath(customPath) : this._getScreenshotPath(forError);
const thumbnailPath = this._getThumbnailPath(screenshotPath);
const tempPath = screenshotPath.replace(this.baseScreenshotsPath, this.tempDirectoryPath);
let screenshotData;
if ((0, async_queue_1.isInQueue)(screenshotPath))
this.warningLog.addWarning(warning_message_1.default.screenshotRewritingError, screenshotPath);
await (0, async_queue_1.addToQueue)(screenshotPath, async () => {
const clientAreaDimensions = Capturer._getClientAreaDimensions(pageDimensions);
const { width: pageWidth, height: pageHeight } = clientAreaDimensions || {};
const takeScreenshotOptions = {
filePath: tempPath,
pageWidth,
pageHeight,
fullPage,
};
await this._takeScreenshot(takeScreenshotOptions);
if (!await Capturer._isScreenshotCaptured(tempPath))
return;
const image = await (0, promisified_functions_1.readPngFile)(tempPath);
const markSeedPosition = markSeed ? (0, crop_1.calculateMarkPosition)(image, markSeed) : null;
if (markSeed && !markSeedPosition)
this.warningLog.addWarning(warning_message_1.default.screenshotMarkNotFound, tempPath, (0, crop_1.markSeedToId)(markSeed));
const croppedImage = await (0, crop_1.cropScreenshot)(image, {
markSeedPosition,
clientAreaDimensions,
path: tempPath,
cropDimensions: Capturer._getCropDimensions(cropDimensions, pageDimensions),
});
if (croppedImage)
await (0, promisified_functions_1.writePng)(tempPath, croppedImage);
screenshotData = await (0, promisified_functions_1.readFile)(tempPath);
if (forError && this.autoTakeOnFails)
return;
await (0, make_dir_1.default)((0, path_1.dirname)(screenshotPath));
await (0, promisified_functions_1.writeFile)(screenshotPath, screenshotData);
if (thumbnails)
await (0, testcafe_browser_tools_1.generateThumbnail)(screenshotPath, thumbnailPath);
});
const testRunId = this.testEntry.testRuns[this.browserId].id;
const userAgent = (0, escape_user_agent_1.default)(this.pathPattern.data.parsedUserAgent.prettyUserAgent);
const quarantineAttempt = this.pathPattern.data.quarantineAttempt;
const takenOnFail = forError;
const screenshot = {
testRunId,
screenshotPath,
screenshotData,
thumbnailPath,
userAgent,
quarantineAttempt,
takenOnFail,
actionId: failedActionId || actionId,
};
this.testEntry.screenshots.push(screenshot);
return screenshotPath;
}
async captureAction(options) {
return await this._capture(false, options);
}
async captureError(options) {
return await this._capture(true, options);
}
}
exports.default = Capturer;
module.exports = exports.default;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2FwdHVyZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc2NyZWVuc2hvdHMvY2FwdHVyZXIuanMiXSwibmFtZXMi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