如何让 Selenium firefox 驱动截取仅查看页面的屏幕截图
How to make Selenium firefox driver take screenshots of only viewed page
我 运行 在 Java 中使用 Selenium 进行了一系列自动化 GUI 测试。这些测试定期使用以下屏幕截图:
public static void takeScreenshot(String screenshotPathAndName, WebDriver driver) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(screenshotPathAndName));
} catch(Exception e) {
e.printStackTrace();
}
}
这在 Chrome 和 IE 中运行良好,但在 firefox 中,我的屏幕截图下总是出现大片空白。我怀疑空白实际上是页面本身的一部分,但通常隐藏在浏览器的视图之外(滚动条在空白之前停止)。我用
做了一个快速测试
driver.get("http://whosebug.com/");
takeScreenshot("D:\TestRuns\Whosebug.png", driver);
并发现当使用 Firefox 驱动程序时,整个页面都会在屏幕截图中捕获,而使用 Chrome 驱动程序时,只会捕获浏览器中显示的内容。
有什么方法可以强制 Firefox 驱动程序截取仅包含浏览器中实际可见内容(实际用户会看到的内容)的屏幕截图?
试试这个:
private static void snapshotBrowser(TakesScreenshot driver, String screenSnapshotName, File browserFile) {
try {
File scrFile = driver.getScreenshotAs(OutputType.FILE);
log.info("PNG browser snapshot file name: \"{}\"", browserFile.toURI().toString());
FileUtils.deleteQuietly(browserFile);
FileUtils.moveFile(scrFile, browserFile);
} catch (Exception e) {
log.error("Could not create browser snapshot: " + screenSnapshotName, e);
}
}
根据 this question 的回答,我可以添加 4 行代码来将图像裁剪到浏览器大小。这确实解决了我的问题,虽然如果可以通过驱动程序解决而不是在截屏后裁剪会更好。
public static void takeScreenshot(String screenshotPathAndName, WebDriver driver) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
int height = driver.manage().window().getSize().getHeight();
BufferedImage img = ImageIO.read(scrFile);
BufferedImage dest = img.getSubimage(0, 0, img.getWidth(), height);
ImageIO.write(dest, "png", scrFile);
FileUtils.copyFile(scrFile, new File(screenshotPathAndName));
} catch(Exception e) {
e.printStackTrace();
}
}
我 运行 在 Java 中使用 Selenium 进行了一系列自动化 GUI 测试。这些测试定期使用以下屏幕截图:
public static void takeScreenshot(String screenshotPathAndName, WebDriver driver) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(screenshotPathAndName));
} catch(Exception e) {
e.printStackTrace();
}
}
这在 Chrome 和 IE 中运行良好,但在 firefox 中,我的屏幕截图下总是出现大片空白。我怀疑空白实际上是页面本身的一部分,但通常隐藏在浏览器的视图之外(滚动条在空白之前停止)。我用
做了一个快速测试 driver.get("http://whosebug.com/");
takeScreenshot("D:\TestRuns\Whosebug.png", driver);
并发现当使用 Firefox 驱动程序时,整个页面都会在屏幕截图中捕获,而使用 Chrome 驱动程序时,只会捕获浏览器中显示的内容。
有什么方法可以强制 Firefox 驱动程序截取仅包含浏览器中实际可见内容(实际用户会看到的内容)的屏幕截图?
试试这个:
private static void snapshotBrowser(TakesScreenshot driver, String screenSnapshotName, File browserFile) {
try {
File scrFile = driver.getScreenshotAs(OutputType.FILE);
log.info("PNG browser snapshot file name: \"{}\"", browserFile.toURI().toString());
FileUtils.deleteQuietly(browserFile);
FileUtils.moveFile(scrFile, browserFile);
} catch (Exception e) {
log.error("Could not create browser snapshot: " + screenSnapshotName, e);
}
}
根据 this question 的回答,我可以添加 4 行代码来将图像裁剪到浏览器大小。这确实解决了我的问题,虽然如果可以通过驱动程序解决而不是在截屏后裁剪会更好。
public static void takeScreenshot(String screenshotPathAndName, WebDriver driver) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
int height = driver.manage().window().getSize().getHeight();
BufferedImage img = ImageIO.read(scrFile);
BufferedImage dest = img.getSubimage(0, 0, img.getWidth(), height);
ImageIO.write(dest, "png", scrFile);
FileUtils.copyFile(scrFile, new File(screenshotPathAndName));
} catch(Exception e) {
e.printStackTrace();
}
}