2

I have just started using TestNG (selenium webdriver) for automation in eclipse. I want to capture screenshot and show it in the "emailable-report.html" in the test-output folder. I am able to capture the screenshot and save it to a location in system. But I am not able to display it in the report. The report always display 'text' instead of image.

In TestNG, I have used ITestListener, which is getting called properly. I have also fiddled with property "Reporter.setEscapeHtml" but it was of no help.

PS: I have not used ReportNG; and do not intend to use it unless real need arises.

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;

public class TestListener implements ITestListener {
    WebDriver driver = null;
    String filePath = "C:\\opt\\data\\";
    Logger log = Logger.getLogger(TestListener.class.getName());

    @Override
    public void onTestFailure(ITestResult result) {
        log.error("Error " + result.getName() + " test has failed");
        String methodName = result.getName().toString().trim();
        takeScreenShot(methodName);
    }

    public void takeScreenShot(String methodName) {
        driver = getDriver();
        File scrFile =  ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        try {
            File f = new File(filePath + methodName + ".png");
            FileUtils.copyFile(scrFile, f);
            log.error("Fail screenshot captured @" + f.getPath() + "");
            //Reporter.setEscapeHtml(true);
            Reporter.log("<img src=\"file:///" + f.getPath() + "\" alt=\"\"/><br />");
        } catch (IOException e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }
    }

    public void onFinish(ITestContext context) {}

    public void onTestStart(ITestResult result) {}

    public void onTestSuccess(ITestResult result) {}

    public void onTestSkipped(ITestResult result) {}

    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {}

    public void onStart(ITestContext context) {}

}

deep
  • 386
  • 4
  • 10

0 Answers0