截图操作
public static void Screenshot(AndroidDriver driver,string ScreenName)throws IOException{
//设置时间格式
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-mm-ss");
//生成时间戳
String dateString =formatter.format(new Date());
String dir_name=System.getProperty("user.dir")+"\\异常图片";
System.out.println("异常图片目录+"dir_name");
//由于可能存在异常图片的且当被删除的可能,所以这边先判断目录是否存在
if(!(new File(dir_name).isDirectory()));
{
//判断是否存在该目录
new File(dir_name).mkdir();
}
//调用方法捕捉画面
File screen=driver.getScreenshotAs(outputType.FILE);
//复制文件到指定目录
//图片最后存放的路径由 目录:dir_name +时间戳+测试套件+测试用例+测试步骤组合生成
system.out.println("异常图片名称"+dir_name+"\\"+dateString+ScreenName+".jpg");
FileUtils.copyFile(screen,new File(dir_name+"\\"+dateString+ScreenName+".jpg"));
}
调用的时候一般使用try{
swipe_1();
}catch(Exception e){
Screenshot(driver,"Demo")
}
public static void snapshot(TakesScreenshot drivername, String filename) {
// this method will take screen shot ,require two parameters ,one is// driver name, another is file name
String currentPath = System.getProperty("user.dir"); // get current work
// folder
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy
// somewhere
try {
System.out.println("save snapshot path is:" + currentPath + "/"
+ filename);
FileUtils
.copyFile(scrFile, new File(currentPath + "\\" + filename));
} catch (IOException e) {
System.out.println("Can't save screenshot");
e.printStackTrace();
} finally {
System.out.println("screen shot finished, it's in " + currentPath
+ " folder");
}
}