public class Screenshot {
public static void screenshot (Androiddriver driver,string screenname) throws ioexception{
Set the time format
SimpleDateFormat formatter=new SimpleDateFormat ("Yyyy-mm-dd-mm-ss");
Generate timestamp
String datestring =formatter.format (New Date ());
String dir_name=system.getproperty ("User.dir") + "\ \ abnormal picture";
System.out.println ("Exception picture directory" +dir_name);
Because there may be abnormal pictures and when it is possible to delete, so this side first determine whether the directory exists
if (! ( New File (Dir_name). Isdirectory ());
{
Determine if the directory exists
New File (Dir_name). mkdir ();
}
Call Method Capture Screen
File Screen=driver.getscreenshotas (Outputtype.file);
Copy files to the specified directory
The path of the last picture stored by the directory: Dir_name + timestamp + test suite + test case + test step combination generation
System.out.println ("Exception picture name" +dir_name+ "\ \" +datestring+screenname+ ". jpg");
Fileutils.copyfile (screen,new File (dir_name+ "\ \" +datestring+screenname+ ". jpg"));
}
}
Test class
Calling Method: Screenshot (Driver, "Demo")
You can also try: Catch
try{
Swipe_1 ();
}catch (Exception e) {
Screenshot (Driver, "Demo")
}
Appium Automation--