ADB+Appium+Java获取系统日志分析准确定位Bug

 简介

 配合ADB+Appium+Java自动化测试准确定位app的Bug

 ADB获取手机系统日志文件保存本地并且过滤



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();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

멋진

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值