diff --git a/tests/e2e/specs/MochaHooks.ts b/tests/e2e/specs/MochaHooks.ts index bdd93a86ba3..42a403628b1 100644 --- a/tests/e2e/specs/MochaHooks.ts +++ b/tests/e2e/specs/MochaHooks.ts @@ -29,6 +29,7 @@ import { REPORTER_CONSTANTS } from '../constants/REPORTER_CONSTANTS'; const driverHelper: DriverHelper = e2eContainer.get(CLASSES.DriverHelper); let latestWorkspace: string = ''; +export let rpApi: any = undefined; export function registerRunningWorkspace(workspaceName: string): void { workspaceName !== '' ? Logger.debug(`with workspaceName:${workspaceName}`) : Logger.debug('delete workspace name'); @@ -38,6 +39,10 @@ export function registerRunningWorkspace(workspaceName: string): void { exports.mochaHooks = { beforeAll: [ + function initRPApi(): any { + rpApi = require('@reportportal/agent-js-mocha/lib/publicReportingAPI.js'); + }, + function decorateExternalClasses(): void { decorate(injectable(), Main); decorate(injectable(), LocatorLoader); @@ -87,6 +92,21 @@ exports.mochaHooks = { } } }, + async function saveReportportalAttachments(this: Mocha.Context): Promise { + if (REPORTER_CONSTANTS.SAVE_RP_REPORT_DATA && this.currentTest?.state === 'failed') { + try { + const screenshot: string = await driverHelper.getDriver().takeScreenshot(); + const attachment: { name: string; type: string; content: string } = { + name: 'screenshot.png', + type: 'image/png', + content: screenshot + }; + rpApi.error('Screenshot on fail: ', attachment); + } catch (e) { + rpApi.error('Could not attach the screenshot'); + } + } + }, // stop and remove running workspace function deleteWorkspaceOnFailedTest(this: Mocha.Context): void { if (this.currentTest?.state === 'failed') { @@ -104,8 +124,7 @@ exports.mochaHooks = { if (!BASE_TEST_CONSTANTS.TS_DEBUG_MODE && CHROME_DRIVER_CONSTANTS.TS_USE_WEB_DRIVER_FOR_TEST) { // ensure that fired events done await driverHelper.wait(5000); - await driverHelper.getDriver().quit(); - Logger.info('Chrome driver session stopped.'); + await driverHelper.quit(); } } ] diff --git a/tests/e2e/utils/DriverHelper.ts b/tests/e2e/utils/DriverHelper.ts index 95a1386fefa..f94a6796133 100644 --- a/tests/e2e/utils/DriverHelper.ts +++ b/tests/e2e/utils/DriverHelper.ts @@ -742,4 +742,10 @@ export class DriverHelper { await this.getDriver().navigate().to(url); } + + async quit(): Promise { + Logger.trace(); + + await this.getDriver().quit(); + } } diff --git a/tests/e2e/utils/Logger.ts b/tests/e2e/utils/Logger.ts index 68512f02b24..1431dadcf14 100644 --- a/tests/e2e/utils/Logger.ts +++ b/tests/e2e/utils/Logger.ts @@ -8,17 +8,23 @@ * SPDX-License-Identifier: EPL-2.0 **********************************************************************/ import { REPORTER_CONSTANTS } from '../constants/REPORTER_CONSTANTS'; +import { rpApi } from '../specs/MochaHooks'; -export abstract class Logger { +export class Logger { /** * uses for logging of fatal errors. * @param text log text * @param indentLevel log level */ + static error(text: string = '', indentLevel: number = 1): void { const callerInfo: string = this.getCallerInfo(); const logLevelSymbol: string = '[ERROR] '; - this.logText(indentLevel, logLevelSymbol, `${this.getFullMessage(callerInfo, text)}`); + const message: string = this.getFullMessage(callerInfo, text); + this.logText(indentLevel, logLevelSymbol, message); + if (this.sendLogMessageIntoReportPortal()) { + rpApi.error(message); + } } /** @@ -32,7 +38,11 @@ export abstract class Logger { } const callerInfo: string = this.getCallerInfo(); const logLevelSymbol: string = '[WARN] '; - this.logText(indentLevel, logLevelSymbol, `${this.getFullMessage(callerInfo, text)}`); + const message: string = this.getFullMessage(callerInfo, text); + this.logText(indentLevel, logLevelSymbol, message); + if (this.sendLogMessageIntoReportPortal()) { + rpApi.warn(message); + } } /** @@ -46,7 +56,11 @@ export abstract class Logger { } const callerInfo: string = this.getCallerInfo(); const logLevelSymbol: string = '• '; - this.logText(indentLevel, logLevelSymbol, `${this.getFullMessage(callerInfo, text)}`); + const message: string = this.getFullMessage(callerInfo, text); + this.logText(indentLevel, logLevelSymbol, message); + if (this.sendLogMessageIntoReportPortal()) { + rpApi.info(message); + } } /** @@ -64,7 +78,11 @@ export abstract class Logger { } const callerInfo: string = this.getCallerInfo(); const logLevelSymbol: string = '▼ '; - this.logText(indentLevel, logLevelSymbol, `${this.getFullMessage(callerInfo, text)}`); + const message: string = this.getFullMessage(callerInfo, text); + this.logText(indentLevel, logLevelSymbol, message); + if (this.sendLogMessageIntoReportPortal()) { + rpApi.debug(message); + } } /** @@ -84,13 +102,21 @@ export abstract class Logger { } const callerInfo: string = this.getCallerInfo(); const logLevelSymbol: string = '‣ '; - this.logText(indentLevel, logLevelSymbol, `${this.getFullMessage(callerInfo, text)}`); + const message: string = this.getFullMessage(callerInfo, text); + this.logText(indentLevel, logLevelSymbol, message); + if (this.sendLogMessageIntoReportPortal()) { + rpApi.trace(message); + } } private static getFullMessage(callerInfo: string, text: string): string { return `${callerInfo}${this.separator(text, callerInfo)}${text}`; } + private static separator(text: string, caller: string): string { + return text ? (caller ? ' - ' : '') : ''; + } + private static logText(messageIndentationLevel: number, logLevelSymbol: string, text: string): void { if (text) { // start group for every level @@ -107,14 +133,26 @@ export abstract class Logger { } } - private static getCallerInfo(): string { - const e: Error = new Error(); - const stack: string[] = e.stack ? e.stack.split('\n') : []; + private static getCallerInfo(i: number = 4): string { + const stack: string[] = this.getCallStackArray(); // " at functionName ( ..." => "functionName" - return stack[3].includes('. { + return acc || /MochaHooks|CheReporter/.test(e); + }, false); } }