Reputation: 1843
I don't understand why my screenshots are not appearing in my ExtentReport locally (I haven't tried from a build server yet). Here is the TestNG @AfterMethod where the takeScreenShot method is called:-
@AfterMethod
public void afterMethod(ITestResult result) throws IOException {
APPLICATION_LOGS.debug("Running @AfterMethod");
switch (result.getStatus()) {
case ITestResult.FAILURE:
childTest.fail(result.getThrowable())
.addScreenCaptureFromPath((takeScreenShot(result.getMethod().getMethodName())));
try {
childTest.info("<a href='https://mywebsite.com/fabrix/logs/s/" + webSessionId + "'" + "target=_'blank'>Data control logs</a>");
} catch (Exception e) {
childTest.info("Unable to get Session ID");
}//.....more code
And here is the called method:
private String takeScreenShot(String methodName) {
String path = System.getProperty("user.dir") + "\\target\\reports\\screenshots\\" + methodName + ".png";
try {
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(path));
} catch (Exception e) {
APPLICATION_LOGS.error("Could not write screenshot" + e);
}
return path;
}
And the report:
[![enter image description here][1]][1]
I examine the image using Chrome developer tools and the path is perfect and image is saved in the same directory as the report. So why not showing?
From IntelliJ:
Upvotes: 1
Views: 2267
Reputation: 2836
It's because the screenshot path specified needs to be relative -to- the reports path. I found that out the hard way myself. While the screenshot is indeed being taken and stored where you think it is, since it's the exact same location as the report, simply leave-off the path leading-up to the filename.
In my output, I place the report file in the target\reports folder off the project folder, but then I have a target\reports\screenshots folder, so when I attach the screenshots to the test object, I only specify "screenshots\filename.png" It would not work when I tried the full path.
Upvotes: 2