Search code examples
c#screenshotrelative-pathselenium-extent-report

Extent Report Selenium WebDriver Screenshot Loading Issue c#


I am using below code to save the screenshot to the same folder (d:\DevTeam\LSPTestSuites) as my reports HTML file.

ScreenshotFilePath = $"{Reporter.LatestResultsReportFolder}\\{screenshotName}.jpg";
ScreenshotFilePath = ScreenshotFilePath.Replace('/', ' ').Replace('"', ' ');
ss.SaveAsFile(ScreenshotFilePath, ScreenshotImageFormat.Png);

but the screenshots don't load if I copy the folder to another location.

The complete screenshot file path saved in the report.html file is as follows:

<td class='step-details'>Launch Introductory Video.<img data-featherlight='d:\DevTeam\LSPTestSuites\20181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' class='step-img' src='d:\DevTeam\LSPTestSuites\20181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' data-src='d:\DevTeam\LSPTestSuites\20181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg'></td>

Solution

  • First, I saved the screenshot to the screenshots folder inside the reports directory (c://temp/LSPTestSuites/screenshots) then, I removed the reports directory path from the screenshot file path and later used it to add it to the report.

    ScreenshotFilePath = $"{Reporter.LatestResultsReportFolder}\\screenshots\\{screenshotName}.jpg";
    ScreenshotFilePath = ScreenshotFilePath.Replace('/', ' ').Replace('"', ' ');
    ss.SaveAsFile(ScreenshotFilePath, ScreenshotImageFormat.Png);
    //to save relative screenshots in Reports html file - start
    if(ScreenshotFilePath.IndexOf("screenshots") != -1)
    {
           ScreenshotFilePath = ScreenshotFilePath.Substring(ScreenshotFilePath.IndexOf("screenshots"));
    }
    //to save relative screenshots in Reports html file - end
    Logger.Trace($"ScreenshotFilePath => {ScreenshotFilePath}");
    

    The key was to use relative path (screenshots/screenshot.png) while adding the screenshot to the extent report:

    CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);
    

    Now, I can move the reports folder to any location to any machine and the screenshots are loaded/displayed without any issues.