目的:Appium自动化测试截图
环境:Appium + Eclipse
代码:
1.编写截图+保存的方法:
public static void snapshot(TakesScreenshot driver, String filename) {
String currentpath = System.getProperty("user.dir"); // get current work folder
File scrFile = driver.getScreenshotAs(OutputType.FILE);
try {
System.out.println("save snapshot path is:" + currentpath + "\\" + filename + "\n");
FileUtils.copyFile(scrFile, new File(currentpath + "\\" + filename)); // save screenshot
} catch (IOException e) {
System.out.println("Cannot take screenshot");
e.printStackTrace();
} finally{
System.out.println("already saved screenshot in:" + currentpath);
}
}
2.调用截图方法:
try {
//login code
if (condition){
System.out.println("succeessful to login");
snapshot((TakesScreenshot)driver, "loginsuccess.png");
}else{
System.out.println("failed to login");
snapshot((TakesScreenshot)driver, "loginfail.png");
}
} catch (Exception e) {
snapshot((TakesScreenshot)driver, "loginerror.png");
e.printStackTrace();
}