Narendra Chetan
Narendra Chetan

Reputation: 29

Not able to add the screenshot to the extent report in Cucumber

I am unable to add the screenshot of failed case to the extent report in Cucumber (java).

//Runner class to generate extent report
@AfterClass
public static void Report() {

    Reporter.loadXMLConfig(new File("/Users/chetan/Documents/workspace/Packages/extent-config.xml"));
    Properties p = new Properties();
    p.list(System.out);

}

//Main class contains step definitions
@After("@browser")
public void teardown(WebDriver driver, Scenario scenario, String screenshotName) throws IOException {
    if (scenario.isFailed()) {
        final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");

        driver.quit();
    }
}

Upvotes: 1

Views: 6530

Answers (3)

Vedran Zenzerović
Vedran Zenzerović

Reputation: 45

This is question for extent reports cucumber adapter but answers are for extent reports. Please open report and see in console the link to the screenshot path and check if images are there.

It is known issue that extent adapters save screenshots in test-output folder for some reason, you may need to do put screenshot folder in extent.properties or use SystemSet

The problem was apparently solved in new snapshot but I have to check that

Upvotes: 0

Ishita Shah
Ishita Shah

Reputation: 4035

You need to define Extent Report Object at class Level or wherever you like. And then you can use it on Failure.

  • For the Report location: Create New Folder with name Report in your project directory at root location
  • For the Screenshot location: Create New Folder with name Screenshots in your project directory at root location

Code:

//Report Initialization
ExtentHtmlReporter htmlreport = new ExtentHtmlReporter(".\\Report\\Extent Report with Screenshot.html");
ExtentReports reports = new ExtentReports();
reports.attachReporter(htmlreport);
ExtentTest testlog;

//Capture and save screenshot
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + "Test.png"));

//Log Screenshot in Report
testlog.info("Details of " + "Test screenshot", MediaEntityBuilder
                .createScreenCaptureFromPath(System.getProperty("user.dir") + "\\Screenshots\\" + "Test.png").build());

//Flush Report-Mandatory, Else report will not generate.  
reports.flush();

You can refer my example from Github (Click here), for ease of Use.

Upvotes: 1

Guy
Guy

Reputation: 50819

You didn't add the screenshot to the report

ExtentTest test = extent.createTest("TestName");
test.fail("details").addScreenCaptureFromPath("pathToScreenshot");
// or
test.fail("details", MediaEntityBuilder.createScreenCaptureFromPath("pathToScreenshot").build());

Upvotes: 0

Related Questions