在web自动化测试时,case执行失败时,提供截图功能,可以通过截取的失败图片,快速发现问题。这里只说明了截图方法,自动化测试中要加上监听器,可以监听用例执行失败时进行截图
通过 getScreenshotAs 方法截取图片
public void TakeScreenShot(){
//获取时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");//格式化类对象
String curTime = sdf.format(new Date());格式化当前时间
//获取类名
String curClassName = this.getClass().getName();
//存放路径
String curPath = System.getProperty("user.dir");
String pngPath = curClassName+"_"+curTime+".png";
//截取图片
File ScrPng = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
try {
Files.copy(ScrPng,new File(curPath+"/"+pngPath));
} catch (IOException e) {
e.printStackTrace();
}
}
getScreenshotAs源码
public &