From 7173edeb8c9aeed62ed14374e18e1c8fd967834b Mon Sep 17 00:00:00 2001 From: Kartikeya Gokhale Date: Mon, 10 Jun 2019 13:31:23 +0530 Subject: [PATCH] Added allure reporter functionality --- README.md | 9 +++++++++ index.js | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f8764df..b777ec3 100644 --- a/README.md +++ b/README.md @@ -102,3 +102,12 @@ Scenario('Compare CPU Usage Images', async (I) => { }); ``` +Allure reports may also be generated directly from the tool. To do so, add +``` +"plugins": { + "allure": {} +} +``` +in the config file. +The attachments will be added to the report only when the calulated mismatch is greater than the given tolerance. +Set `output` to where the generated report is to be stored. Default is the output directory of the project. \ No newline at end of file diff --git a/index.js b/index.js index 6c65532..7f0b0f0 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,8 @@ const resemble = require("resemblejs"); const fs = require('fs'); const assert = require('assert'); const mkdirp = require('mkdirp'); -const getDirName = require('path').dirname; +const path = require('path'); +const getDirName = path.dirname; /** * Resemble.js helper class for CodeceptJS, this allows screen comparison @@ -91,6 +92,25 @@ class ResembleHelper extends Helper { else throw new Error("Method only works with Puppeteer"); } + /** + * This method attaches image attachments of the base, screenshot and diff to the allure reporter when the mismatch exceeds tolerance. + * @param baseImage + * @param misMatch + * @param tolerance + * @returns {Promise} + */ + + async _addAttachment(baseImage, misMatch, tolerance) { + const allure = codeceptjs.container.plugins('allure'); + const diffImage = "Diff_" + baseImage.split(".")[0] + ".png"; + + if(allure && misMatch >= tolerance) { + allure.addAttachment('Base Image', fs.readFileSync(path.join(this.config.baseFolder, baseImage)), 'image/png'); + allure.addAttachment('Screenshot Image', fs.readFileSync(path.join(this.config.screenshotFolder, baseImage)), 'image/png'); + allure.addAttachment('Diff Image', fs.readFileSync(path.join(this.config.diffFolder, diffImage)), 'image/png'); + } + } + /** * Check Visual Difference for Base and Screenshot Image * @param baseImage Name of the Base Image (Base Image path is taken from Configuration) @@ -108,6 +128,9 @@ class ResembleHelper extends Helper { } const misMatch = await this._fetchMisMatchPercentage(baseImage, options); + + this._addAttachment(baseImage, misMatch, options.tolerance); + this.debug("MisMatch Percentage Calculated is " + misMatch); assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch); } @@ -133,6 +156,9 @@ class ResembleHelper extends Helper { options.boundingBox = await this._getBoundingBox(selector); const misMatch = await this._fetchMisMatchPercentage(baseImage, options); + + this._addAttachment(baseImage, misMatch, options.tolerance); + this.debug("MisMatch Percentage Calculated is " + misMatch); assert(misMatch <= options.tolerance, "MissMatch Percentage " + misMatch); }