import 'codeceptjs'; const Helper = require(`@codeceptjs/helper`); import { Wait } from './dewWait'; import { logger } from '../Logger/logger'; const containers = require(`codeceptjs/lib/container`); import { ReadLocators } from "../readLocators"; const locators:any = ReadLocators.readJson(); /** * Screenshot Component */ export class Screenshot extends Helper { /** * The element is used to take a screenshot of particular element in DOM and saves it with ".png" extension * @param {any} locatorParam * @param {string} filename * */ static async saveElementScreenshot(locatorParam: any, filename: string) { let selector: string; if (Array.isArray(locatorParam)) { selector = locatorParam[0]; } else { selector = locatorParam; } try { const helper = this.prototype.helpers[`WebDriver`]; await Wait.waitUntilVisibilityOfElement(selector); // await helper.waitForVisible(selector); const els = await helper._locate(selector); if (!els.length) throw new Error(`Element ${selector} couldn't be located`); const el = els[0]; await el.saveScreenshot(filename + `.png`); } catch (err) { logger.log(`Issue while performing operation in DDCC: Unable to save screenshot`); throw err; } } /** * This method is used to attched screenshot in allure report * * ```js * await Screenshot.attachScreenShotInAllure(); * ``` */ static async attachScreenShotInAllure() { const browser = this.prototype.helpers[`WebDriver`].browser; const allureReporter = await containers.plugins(`allure`) || { addAttachment: () => { } }; const fileName = new Date().getTime(); console.log(fileName); await browser.saveScreenshot(locators.attachScreenShotInAllureXpath + fileName + `.png`); const readFromfs = await require(`fs`).readFileSync(locators.attachScreenShotInAllureXpath + fileName + `.png`); await allureReporter.addAttachment(`Additional Screenshot`, readFromfs, `image/png`); } }