//截屏功能,并以当前时间戳作为文件名保存在指定目录下
public void takeScreenShot(){
String dir_name = "screenshot"; //定义一个截图存放的目录
//判断目录是否存在
if(!(new File(dir_name).isDirectory())){
//如果不存在则新建目录
new File(dir_name).mkdir();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
//格式化当前时间,例如20130205-120030
String time = sdf.format(new Date());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
//将截图放到指定目录,并以当前时间戳作为文件名保存
FileUtils.copyFile(scrFile,new File(dir_name+File.separator+time+".png"));
}catch(IOException e){
e.printStackTrace();
}
}
自动化跑脚本的时候,有时候希望能将发生错误的画面保存下来,这里使用Selenium-WebDriver实现这一截屏功能。使用java语言实现,这里只写该功能的方法: