ExtentReports - screenshot not in the report - broken image
Solution 1
As suggested - the absolute path could be a solution, but I didn't want to go that way.
I've figured out that a solution is to store the images in the same directory where the report gets generated, give the image name to .addScreenCaptureFromPath(screenshotName.PNG) and it works perfectly.
Solution 2
I used the absolute path
Note: inspects the broken image from the browser to validate the absolute path of the image
Take ScreenShot:
public static String TakesScreenshot(IWebDriver driver, string FileName)
{
string pathProject = AppDomain.CurrentDomain.BaseDirectory;
string pathScreen = pathProject.Replace("\\bin\\Debug", "");
string path = pathScreen + "project/Test-output/Images/";
StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString());
TimeAndDate.Replace("/", "_");
TimeAndDate.Replace(":", "_");
TimeAndDate.Replace(" ", "_");
string imageName = FileName + TimeAndDate.ToString();
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(path + "_" + imageName + "." + System.Drawing.Imaging.ImageFormat.Jpeg);
return path + "_" + imageName + "." + "jpeg";
}
Attach image to the report with path of the preview method: In the specific step:
ExtentTest.Fail("message", MediaEntityBuilder.CreateScreenCaptureFromPath(TakeScreenShot.TakesScreenshot(driver, "Fatal")).Build());
With the method "TakesScreenshot" Take the screenshot
Version ExtentReport: 3, C#, NUnit 3
USING JAVA:
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
Is:
ExtentTestManager.getTest().log(LogStatus.ERROR, ExtentTestManager.getTest().addScreenCapture("//ABOLUTE/PATH/IMAGE.PNG"));
regards.
Solution 3
In order to get screenshot in the extent report just add a extra dot in the extent report screenshot path. Refer code below:
test.log(Status.INFO, "FAQs button clicked",
MediaEntityBuilder.createScreenCaptureFromPath("." + screenshot()).build());
Hope this helps!
Related videos on Youtube
StopTheRain
Updated on August 02, 2022Comments
-
StopTheRain over 1 year
I'm trying to add a screenshot to my ExtentReport HTML file, but for some reason, the image is not there even though it DOES exist and the console shows that it's looking at the correct place (href is correct).
This is the latest trial code:
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver); String destination = getScreenshotPath(); ImageIO.write(screenshot.getImage(), "IMG", new File(destination)); test.fail("Details: " + test.addScreenCaptureFromPath(destination));
The screenshot gets saved in the destination. When I try the debugging mode, or look at the report, it's printed as:
Details: com.aventstack.extentreports.ExtentTest@62041567 and there's a broken image under it:
-
Agent Shoulder over 4 yearsThis does not solve the issue, at least not in my case.
-
Prophet over 3 yearsWhat if you run several tests in the same suite and it would be multiple failures with screenshot for each failure?
-
Yeheshuah almost 3 yearsCan you a bit clarify about
Path
inString imgPath = Path+screenshotId+".png";
? -
DMac almost 3 yearsIn the above code, Path = System.getProperty("user.dir")+"/ExtentReports/";
-
Mátyás Grőger about 2 yearsare you sure it is solving the issue, if it resulted the same error?