I am trying to attach screenshots to the report that gets generated.
As far as I can see I'm adding the file path so it should be attaching.
This is the code that captures the screenshot and generates the file
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
public static String captureScreen(WebDriver driver, String screenName) throws IOException {
TakesScreenshot screen = (TakesScreenshot) driver;
File src = screen.getScreenshotAs(OutputType.FILE);
String path = "X:\\ExtentReports";
String javaPath = path.replace("\\", "/");
File target = Paths.get(javaPath,
screenName + ".png").toFile();
FileUtils.copyFile(src, target);
return javaPath;
}
public static String generateFileName(ITestResult results) {
Date date = new Date();
String fileName = results.getName() + "_" + dateFormat.format(date);
return fileName;
// return results.getName() + "_" + dateFormat.format(date);
}
}
This is where the report gets generated and the screensots should get added to the test. By using:
.addScreenCaptureFromPath(screenShot));
public synchronized void afterMethod(ITestResult result) throws IOException {
String screenShot = CaptureScreenShot.captureScreen(MetricellTest.driver, CaptureScreenShot.generateFileName(result));
if (result.getStatus() == ITestResult.FAILURE) {
test.get().log(Status.FAIL, result.getName());
test.get().log(Status.FAIL, result.getThrowable());
test.get().fail("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
} else if (result.getStatus() == ITestResult.SUCCESS) {
test.get().log(Status.PASS, result.getName());
test.get().pass("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
} else if (result.getStatus() == ITestResult.SKIP) {
test.get().skip("Test Case : " + result.getName() + " has been skipped");
extent.flush();
MetricellTest.driver.close();
}
}
@AfterTest
public void endTest() {
extent.flush();
}
At the end of the tests I hope to be able to open the report and see the screenshots inside the report.
Comment from Bill was enough to solve this:
"The expected path of the screenshot file is always relative to the report file itself. If you are wanting to store the screenshots in the same folder as the report file, specify no path, just the filename, otherwise if you have, say, a \screenshots folder off the report location then prepend your filename with "screenshots" + filename. Hard-coding a full path will almost never work."