appium 自動化學習之截圖操作
截圖操作
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");
}
}