Einführung
Arbeiten Sie mit automatisierten ADB+Appium+Java-Tests zusammen, um App-Fehler genau zu lokalisieren
ADB ruft die Protokolldateien des Mobiltelefonsystems ab, speichert sie lokal und filtert sie
import java.io.*;
import java.util.ArrayList;
/**
* @Author GJ
* @Create 2020-05-09 17:30
*/
public class ADBLog {
public Runtime p=Runtime.getRuntime();
public static ArrayList<String> list=new ArrayList<>();
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
list.add(lineTxt);
}
bufferedReader.close();
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
/**
* 从本地获取错误信息
*/
public ArrayList<String> getErro(){
ArrayList<String> erro=new ArrayList<>();
String filePath = "D:\\IdeaProjects\\ESLAppiun\\test_logcat.txt";
readTxtFile(filePath);
for (int i=0;i<list.size();i++){
String temp=list.get(i);
if (temp.contains(" E ")){
erro.add(temp);
}
}
return erro;
}
/**
* adb命令获取系统日志文件保存本地
*/
public void getLog(){
try {
Process exec1 = p.exec("cmd.exe /c adb -s 6EB0217801000899 logcat | find \"com.example.cat\" >test_logcat.txt");
Thread.sleep(2000);
exec1.destroy();
/* Thread.sleep(1000);
Process exec = p.exec("cmd.exe /c adb kill-server");
Thread.sleep(1000);
exec.destroy();*/
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* adb命令清除系统日志文件
*/
public void clearLog(){
try {
Process exec = p.exec("cmd.exe /c adb -s 6EB0217801000899 logcat -c");
Thread.sleep(4000);
exec.destroy();
/*Thread.sleep(1000);
Process exec1 = p.exec("cmd.exe /c adb kill-server");
Thread.sleep(1000);
exec1.destroy();*/
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* 删除本地保存的日志文件
*/
public void deleteFileLog(){
try {
Process exec = p.exec("cmd.exe /c adb kill-server");
Thread.sleep(1000);
exec.destroy();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
File file=new File("D:\\IdeaProjects\\ESLAppiun\\test_logcat.txt");
file.delete();
}
}