0

Hello I am trying to capture screenshot of failed test cases using selenium webdriver with Maven and TestNG with following code, it does generate the screenshot in my project but doesn't get included inside the report what do I need to change in the code?

 WebDriver driver;
ExtentReports extent;
ExtentTest logger;
@BeforeTest
public void setUp() {
    extent = new ExtentReports("C:\\Users\\IdeaProjects\\p\\test-output\\report.html", true);
    extent.loadConfig(new File("C:\\Users\\IdeaProjects\\p\\test-output\\extent-config.xml"));
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://www.google.com");
}
@Test
public void testExtentReports() {
    logger = extent.startTest("testExtentReports");
    Assert.assertTrue(false);
}
@AfterMethod
public void tearDown(ITestResult result) throws IOException {
    if(result.getStatus() == ITestResult.FAILURE) {
        logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
        logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
        String screenshotPath = TestMine.getScreenshot(driver, result.getName());
        logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
    } else if(result.getStatus() == ITestResult.SKIP) {
        logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
    }
    extent.endTest(logger);
}
public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
    String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    TakesScreenshot ts = (TakesScreenshot) driver;
    File source = ts.getScreenshotAs(OutputType.FILE);
    // after execution, you could see a folder "FailedTestsScreenshots" under src folder
    String destination = "ss/" + screenshotName+dateName+".png";
    File finalDestination = new File(destination);
    FileUtils.copyFile(source, finalDestination);
    return destination;
}
@AfterTest
public void endReport() {
    extent.flush();
    extent.close();
    driver.quit();
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.