import 'codeceptjs'; import { Helper } from 'codeceptjs'; import { Wait } from './dewWait'; import { logger } from '../Logger/logger'; const containers = require(`codeceptjs/lib/container`); /** * 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 {string} selector * @param {string} filename * */ static async saveElementScreenshot(selector: string, filename: string) { 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(`./output/` + fileName + `.png`); const readFromfs = await require(`fs`).readFileSync(`./output/` + fileName + `.png`); await allureReporter.addAttachment(`Additional Screenshot`, readFromfs, `image/png`); } }