gkushang / cucumber-html-reporter

Generates Cucumber HTML reports in three different themes
MIT License
234 stars 155 forks source link

How to attach screenshots for failed steps in cucumber-html-reporter using Puppeteer? #213

Open ilijai1 opened 4 years ago

ilijai1 commented 4 years ago

"cucumber": "^6.0.5", "cucumber-html-reporter": "^5.2.0"

addCase.js:

const {Given, When, Then, Before, After} = require('cucumber');

After(async function(scenario) {
    if (scenario.result.status === 'failed') {
        const screenShot = await this.page.screenshot();
        this.attach(screenShot,'image/png');
    }
});

world.js:

const expect = require("chai");
const puppeteer = require("puppeteer");
const {setWorldConstructor} = require("cucumber");
const helpers = require("../../helpers");
const config = require("../../config");

class CustomWorld{

    async launchBrowser(){
        this.browser = await puppeteer.launch({
        headless: config.isHeadless,
        args: config.args,
        });
        this.page = await this.browser.newPage();
        await this.page.setViewport({ 
            width: config.viewportWidth,
            height: config.viewportHeight
        });
    }

}

setWorldConstructor(CustomWorld);

ERROR: TypeError: this.attach is not a function

I tried with scenario.attach() - error: scenario.attach() not a function Also tried with simple storeScreenshot: false in my reporter.js (no change) I am beginner please if someone can help me out :) ?

pawel-id commented 4 years ago

look at default World object constructor example here. The variable this.attach is initiated there - which is missing in your case.

I would suggest to go with default World constructor (as above), and move your launch browser to Before hook (see here)