1.截图并保存到本地
package scriptall; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.plaf.SliderUI; public class screenshot { public static void getpictrue(String str1) throws IOException { Runtime r=Runtime.getRuntime(); r.exec("cmd /c adb shell screencap -p /sdcard/"+str1+".png"); } public static String picnametime() { Date date=new Date(); DateFormat format=new SimpleDateFormat("yy/MM/dd HH:mm:ss"); String time=format.format(date); String time2=filt(time); return time2; } public static String filt(String str) { Pattern p=Pattern.compile("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"); Matcher m=p.matcher(str); return m.replaceAll("").replace(" ", "").trim(); } public static void main(String[] args) throws Exception { String picname; picname=picnametime(); getpictrue(picname); } }
解析:1)调用系统命令时一定要加cmd/c
r.exec("cmd /c adb shell screencap -p /sdcard/"+str1+".png");
2)获取系统时间
Date date=new Date();
DateFormat format=new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String time=format.format(date);
3)过滤字符串
Pattern p=Pattern.compile("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]");
Matcher m=p.matcher(str);
return m.replaceAll("").replace(" ", "").trim();
2、录制视频
package scriptall; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class recordvideo { public static void record(int time,String videoname) throws IOException, Exception { Runtime r=Runtime.getRuntime(); Process p2=r.exec("cmd /c adb shell getprop ro.build.version.sdk"); InputStreamReader in2=new InputStreamReader(p2.getInputStream()); BufferedReader br2=new BufferedReader(in2); String str2; str2=br2.readLine(); if(Integer.parseInt(str2)<19) { System.out.print("需要Android4.4及4.4以上版本才能录制视频哦"); } else{ Process p=r.exec("cmd /c adb shell screenrecord --time-limit "+time+" /sdcard/"+videoname+".mp4"); InputStreamReader in=new InputStreamReader(p.getInputStream()); BufferedReader br=new BufferedReader(in); String str; while((str=br.readLine())!=null) { Thread.sleep(1000); } System.out.println("已录制完成!"); } } public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); String str=null; System.out.println("please input the video time:"); str=sc.nextLine(); int time=Integer.parseInt(str); Scanner sc2=new Scanner(System.in); String str2=null; System.out.println("please input the video name:"); str2=sc2.nextLine(); record(time,str2); } }
解析:1)adb shell getprop ro.build.version.sdk可以获取sdk版本,如19的话就是低于android4.4的
2)获取命令行输入
Scanner sc2=new Scanner(System.in);
String str2=null;
System.out.println("please input the video name:");
str2=sc2.nextLine();
3.自己也写了个跑monkey的脚本,用白名单的方式,测试时只要输入要测试包的包名,包名间用空格隔开。另外每跑一次种子都是随机的。
package scriptall; import java.awt.List; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MonkeyLog { private static final String Logname = null; //运行monkey并获取LOG public static void monkeyrun(String logname) throws IOException { Runtime r=Runtime.getRuntime(); r.exec("cmd /c md d:\"+logname); for(int i=0;i<500;i++) { System.out.println("第"+i+"次测试。。。 "); int seed=(int)(Math.random()*100); r.exec("cmd /c adb shell monkey --pkg-whitelist-file /data/local/tmp/whitelist.txt --throttle 300 -s "+seed+" -v -v -v 1000 >d:\"+logname+"\Monkey.txt"); r.exec("cmd /c adb pull /data/anr d:\"+logname); r.exec("cmd /c adb pull /sdcard/mtklog d:\"+logname); r.exec("cmd /c adb pull /data/tombstones d:\"+logname); } } //获取Log文件夹的名字,以时间命名 public static String logname() { Date date=new Date(); DateFormat dateformat=new SimpleDateFormat("yy/MM/dd HH:mm:ss"); String time=dateformat.format(date); String time2=filt(time); return "Log"+time2; } //过滤掉特殊字符串 public static String filt(String str) { Pattern p=Pattern.compile("[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"); Matcher m=p.matcher(str); return m.replaceAll("").replace(" ", "").trim(); } //把用户输入的包名写入D盘中的whitelist.txt文件中,并把它push到data/local/tmp目录下 public static void getmonkeypsg(ArrayList list) throws IOException { File f=new File("D:\whitelist.txt");//注意符号 if(!f.exists()) { f.createNewFile(); } FileWriter fw=new FileWriter(f); BufferedWriter bw = new BufferedWriter(fw); Iterator it=list.iterator(); while(it.hasNext()) { String str=it.next().toString(); bw.write(str); bw.write(" "); } bw.close(); Runtime r=Runtime.getRuntime(); r.exec("cmd /c adb push D:\whitelist.txt /data/local/tmp/"); } public static void main(String[] args) throws IOException { ArrayList<String> list=new ArrayList<String>(); //获取命令行的输入 Scanner s=new Scanner(System.in); System.out.println("请输入要测试的包名,空格隔开:"); String str; str=s.nextLine(); String[] strlist=str.split(" "); for(int i=0;i<strlist.length;i++) { list.add(strlist[i]); }; getmonkeypsg(list); String Lognamme=logname(); monkeyrun(Lognamme); } }
知识点:1)