我正在尝试将屏幕截图添加到我的ExtentReport HTML文件中,但由于某种原因,即使它存在并且控制台显示它正在查看正确的位置(href是正确的),图像也不存在。< / p>
这是最新的试用代码:
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));
屏幕截图将保存在目标中。 当我尝试调试模式或查看报告时,它打印为:
详细信息:com.aventstack.extentreports.ExtentTest@62041567 并且它下面有一个破碎的图像:
答案 0 :(得分:2)
我使用了绝对路径
注意 :检查浏览器中损坏的图像以验证图像的绝对路径
采取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";
}
使用预览方法的路径将图像附加到报表: 在具体步骤中:
ExtentTest.Fail("message", MediaEntityBuilder.CreateScreenCaptureFromPath(TakeScreenShot.TakesScreenshot(driver, "Fatal")).Build());
使用方法“TakesScreenshot”截取屏幕截图
版本ExtentReport:3, C#, NUnit 3
使用JAVA:
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
时:
ExtentTestManager.getTest().log(LogStatus.ERROR, ExtentTestManager.getTest().addScreenCapture("//ABOLUTE/PATH/IMAGE.PNG"));
问候。
答案 1 :(得分:1)
正如所建议的那样 - 绝对路径可能是一种解决方案,但我并不想这样做。
我已经发现解决方案是将图像存储在生成报告的同一目录中,将图像名称提供给.addScreenCaptureFromPath( screenshotName.PNG )并且它可以正常工作完美。
答案 2 :(得分:0)
为了在扩展区报告中获取屏幕截图,只需在扩展区报告屏幕快照路径中添加一个额外的点。请参考下面的代码:
test.log(Status.INFO, "FAQs button clicked",
MediaEntityBuilder.createScreenCaptureFromPath("." + screenshot()).build());
希望这会有所帮助!
答案 3 :(得分:0)
这对您也一样,对我也一样。
public void afterScenario(Scenario scenario) throws IOException {
if (scenario.isFailed()) {
String screenshotName = scenario.getName().replaceAll(" ", "_");
//This takes a screenshot from the driver at save it to the specified location
File sourcePath = ((TakesScreenshot) testContext.getWebDriverManager().getDriver()).getScreenshotAs(OutputType.FILE);
//Building up the destination path for the screenshot to save
//Also make sure to create a folder 'screenshots' with in the cucumber-report folder
File destinationPath = new File("./src/test/resources/"+screenshotName + ".png");
//Copy taken screenshot from source location to destination location
Files.copy(sourcePath, destinationPath);
//This attach the specified screenshot to the test
Reporter.addScreenCaptureFromPath(screenshotName+".png");
System.out.println("Cant take screenshot in grid.");
}
}
答案 4 :(得分:0)
您可以为绝对路径添加截图,如下所示:
文件源 = ((TakesScreenshot) 驱动程序).getScreenshotAs(OutputType.FILE);
//对于文件复制,绝对路径从你的项目目录开始
FileUtils.copyFile(src, new File("../resources/reports/screenshots/screen2.png"));
String img = logger.addScreenCapture("./screenshots/screen2.png");
logger.log(LogStatus.PASS, "标题验证...", img);
请注意,这里的 addScreenCapture() 方法路径是从截图文件夹开始的,而不是像 copyFile() 方法中的资源。这是因为 ExtentReports 被初始化为:
ExtentReports 报告 = new ExtentReports("../resources/reports/extentreports_v2.html", true);
所以extentreports_v2.html要找到截图的绝对路径,应该从“reports”目录开始
答案 5 :(得分:0)
我尝试了各种网站和博客来解决这个问题,但在任何地方都找不到解决方案。在尝试了各种方法后,我得到了以下解决方案。尝试以下解决方案,它非常适合我。
public static synchronized String addScreenshots(){
WebDriver webDriver = Browser.getDriver();
Long l = Calendar.getInstance().getTimeInMillis();
String screenshotId = l.toString();
String Path = System.getProperty("user.dir")+"/ExtentReports/";
File screenshot = ((TakeScreenshot)WebDriver).getScreenshotAs(OutputType.FILE);
String imgPath = Path+screenshotId+".png";
File dest = new File(imgPath);
try {
FileUtils.copyFile(screenShot, dest);
} catch (IOException e) {
e.printStackTrace();
}
String ImagePath = "../ExtentReports/"+screenshotId+".png";
return ImagePath;
}