import { Screenshot } from "../components/screenshot"; const assert = require(`soft-assert`); const hardassert = require(`assert`); /** * Soft Assert class */ export class SoftAssert { /** * Checks if a value is true. Same as Assert.ok() * ```js *Assert.assert(50 > 70,"Expression is incorrect"); * ``` * @param {any} expression Specifies an expression to be evaluated * @param {String} message Specifies the error message to be assigned to the AssertionError. If omitted, a default message is assigned */ static async assert(expression: any, message?: string) { try { if (message == undefined) { hardassert(expression); } else { hardassert(expression, message); } } catch (err) { await Screenshot.attachScreenShotInAllure(); } } /** * Check if actual equals expected * @param {any} actual * @param {any} expected */ static assertEquals(actual:any, expected:any) { assert.softContains(actual, expected, `Not Matched`); } /** * Check if condition is true * @param {boolean} value * @param {string} failedMsg */ static softTrue(value:boolean, failedMsg?:string) { assert.softTrue(value, failedMsg); } /** * perform Assert All * @param {any} failedEntity */ static softAssetAll(failedEntity:any) { try { assert.softAssertAll(); } catch (error) { console.log(error.message); const jsonObject: any = {}; failedEntity.forEach((value:any, key: any) => { jsonObject[key] = value; }); console.log(JSON.stringify(jsonObject)); const errorMsd = `\nActual: ` + JSON.stringify(error.actual) + `\n\n Expected: ` + JSON.stringify(error.expected) + `\n\n Failed Entities:` + JSON.stringify(jsonObject); throw new Error(errorMsd); } } }