Extent Report Selenium WebDriver 截图加载问题 c#
Extent Report Selenium WebDriver Screenshot Loading Issue c#
我正在使用以下代码将屏幕截图保存到与我的报告 HTML 文件相同的文件夹 (d:\DevTeam\LSPTestSuites)。
ScreenshotFilePath = $"{Reporter.LatestResultsReportFolder}\{screenshotName}.jpg";
ScreenshotFilePath = ScreenshotFilePath.Replace('/', ' ').Replace('"', ' ');
ss.SaveAsFile(ScreenshotFilePath, ScreenshotImageFormat.Png);
但是如果我将文件夹复制到另一个位置,屏幕截图不会加载。
保存在report.html文件中的完整截图文件路径如下:
<td class='step-details'>Launch Introductory Video.<img data-featherlight='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' class='step-img' src='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' data-src='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg'></td>
一旦您的当前项目目录中有了 "Screenshots" 文件夹(位于根位置),您可以通过以下方式获取它:
//Set Current Project directory
public static String dir = AppDomain.CurrentDomain.BaseDirectory;
public static FileInfo fileInfo = new FileInfo(dir);
public static DirectoryInfo currentDir = fileInfo.Directory.Parent.Parent;
public static string parentDirName = currentDir.FullName;
//Save screenshot
Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
file.SaveAsFile(parentDirName + "\Screenshots\" + "Tempname.png", ScreenshotImageFormat.Png);
//To load it for Selenium Extent Report
testlog.Info("Details with screenshot" , MediaEntityBuilder.CreateScreenCaptureFromPath(parentDirName + "\Screenshots\" + "Tempname.png").Build());
首先,我将截图保存到reports目录(c://temp/LSPTestSuites/screenshots)内的screenshots文件夹中,然后,我从截图文件路径中删除了reports目录路径,稍后将其添加到报告。
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}");
关键是在将屏幕截图添加到范围报告时使用相对路径 (screenshots/screenshot.png):
CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);
现在,我可以将报告文件夹移动到任何计算机的任何位置,屏幕截图 loaded/displayed 没有任何问题。
我正在使用以下代码将屏幕截图保存到与我的报告 HTML 文件相同的文件夹 (d:\DevTeam\LSPTestSuites)。
ScreenshotFilePath = $"{Reporter.LatestResultsReportFolder}\{screenshotName}.jpg";
ScreenshotFilePath = ScreenshotFilePath.Replace('/', ' ').Replace('"', ' ');
ss.SaveAsFile(ScreenshotFilePath, ScreenshotImageFormat.Png);
但是如果我将文件夹复制到另一个位置,屏幕截图不会加载。
保存在report.html文件中的完整截图文件路径如下:
<td class='step-details'>Launch Introductory Video.<img data-featherlight='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' class='step-img' src='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg' data-src='d:\DevTeam\LSPTestSuites181128_1135\PASSED_STEP_TCID31906a_Launch Introductory Video..jpg'></td>
一旦您的当前项目目录中有了 "Screenshots" 文件夹(位于根位置),您可以通过以下方式获取它:
//Set Current Project directory
public static String dir = AppDomain.CurrentDomain.BaseDirectory;
public static FileInfo fileInfo = new FileInfo(dir);
public static DirectoryInfo currentDir = fileInfo.Directory.Parent.Parent;
public static string parentDirName = currentDir.FullName;
//Save screenshot
Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
file.SaveAsFile(parentDirName + "\Screenshots\" + "Tempname.png", ScreenshotImageFormat.Png);
//To load it for Selenium Extent Report
testlog.Info("Details with screenshot" , MediaEntityBuilder.CreateScreenCaptureFromPath(parentDirName + "\Screenshots\" + "Tempname.png").Build());
首先,我将截图保存到reports目录(c://temp/LSPTestSuites/screenshots)内的screenshots文件夹中,然后,我从截图文件路径中删除了reports目录路径,稍后将其添加到报告。
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}");
关键是在将屏幕截图添加到范围报告时使用相对路径 (screenshots/screenshot.png):
CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);
现在,我可以将报告文件夹移动到任何计算机的任何位置,屏幕截图 loaded/displayed 没有任何问题。