使用场景
WebUI自动化测试框架中错误截图方法,需要进行全屏幕截取or当前窗口截取
解决方案
导入Ashot的maven依赖,使用Ashot不同的截图策略
<!-- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot -->
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
代码
如下
public void takeScreenShot(WebDriver driver) {
String screenshotfolder = "./output/errorScreenShot";
String timestamp = String.join("_", sdf.format(Date()))
try {
if(!new File(screenshotfolder.exists())){
new File(screenshotfolder).mkdir(); //如果文件路径不存在,就新建一个文件路径
}
AShot ashot = new AShot(); //需要import ru.yandex.qatools.ashot.Ashot及Screenshot
//ashot.coordsProvider(new WebDriverCoordsProvider()).shootingStrategies.viewportPasting(1000)); //滚动以全屏截图
Screenshot currentScreen = ashot.takeScreenshot(driver); //截图当前窗口显示内容
ImageIO.write(currentScreen.getImage(),"PNG", new File(screenshotfolder+"/" + timestamps +".png"));
} catch (IOException e) {
e.printStackTrace();
}
}