我需要自动化的射手,采取整个网站的屏幕截图。我尝试了一个解决方案,但只取走了页面的一部分。有人能帮上忙吗?下面你可以看到我尝试过的代码。
public void captureScreenshot(WebDriver driver, String screenshotName) {
// Take the screenshot only is the feature is activated
if (isActivate) {
try {
// before to take the screenshot
utils.sleep(1);
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(dirPath + fileSep + screenshotName
+ "_" + strDateStamp + ".png"));
String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
System.setProperty(ESCAPE_PROPERTY, "false");
URL path = new File(dirPath + fileSep + screenshotName + "_"
+ strDateStamp + ".png").toURI().toURL();
String test = "<a href=" + path + "> click to open the screenshot "
+ screenshotName + "</a>";
Reporter.log(screenshotName + test + "<br>");
} catch (Exception e) {
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
}
发布于 2019-06-18 17:08:10
嗨,你可能想试试下面的代码。它可以在chrome和firefox的窗口中截取整个页面的截图。
public void getScreenshotOfWholePageWithScreenshotName(WebDriver driver, String path, String name) {
try {
Thread.sleep(2000);
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(), "PNG", new File(path + "\\" + date() + " - " + name + ".png"));
Thread.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();
}
}
为了运行上面的代码,你必须使用下面的库:
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.2</version>
</dependency>
我使用的是maven,因此我在POM.xml文件中添加了依赖项。
干杯!
发布于 2019-06-18 17:29:50
可以使用Shutterbug来完成:
https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver
要制作整页屏幕快照,请执行以下操作:
Shutterbug.shootPage(driver,ScrollStrategy.WHOLE_PAGE).save();
github https://github.com/assertthat/selenium-shutterbug上的资源
提供在Chrome和其他一些扩展功能中制作全页屏幕截图的能力。
https://stackoverflow.com/questions/56644802
复制相似问题