systelab-components-wdio-test
Version:
Widgets to be use in the E2E Tests based in WDIO
50 lines (49 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScreenshotReporter = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const index_js_1 = require("../wdio/index.js");
class ScreenshotReporter {
static setBasePath(basePath) {
this.basePath = basePath;
}
static beforeSuite(suite) {
this.specCount = 0;
}
static async afterTest(test, context = null, status = null) {
this.specCount++;
const suiteName = test.fullName.slice(0, -1 * (test.description.length)).trim();
try {
const screenshotFolderPath = `${this.basePath}/${suiteName}`;
if (!fs.existsSync(screenshotFolderPath)) {
fs.mkdirSync(screenshotFolderPath, { recursive: true });
}
const screenshotFilename = `${this.specCount.toString().padStart(2, "0")}-${test.description}`;
const screenshotFilenameClean = this.getSanitizedFilename(screenshotFilename);
const screenshotFilepath = `${screenshotFolderPath}/${screenshotFilenameClean}.png`;
if (index_js_1.AutomationEnvironment.hasWorkingBrowser()) {
await index_js_1.AutomationEnvironment.getWorkingBrowser().saveScreenshot(screenshotFilepath);
}
else {
fs.writeFileSync(screenshotFilepath.replace('.png', '-[no-working-browser].png'), '');
}
}
catch (e) {
console.log(e);
}
}
static getSanitizedFilename(filename) {
return filename
.replace(/[\/\\\:]/g, " ")
.replace(/\"/g, "'")
.replace(/\</g, "(")
.replace(/\>/g, ")")
.replace(/[\*\?\|]/g, "")
.replace(/\n|\r/g, "")
.substring(0, 300).trim();
}
}
exports.ScreenshotReporter = ScreenshotReporter;
ScreenshotReporter.basePath = "./reports/screenshots";
ScreenshotReporter.specCount = 0;