Appium如何获得adb logcat

问题描述 投票:5回答:3

我想知道是否有人知道如何使用Appium for Android移动设备运行自动化测试时获取logcat。我正在使用Java而且我在Windows环境中。

有任何想法吗?谢谢!!

java logcat appium
3个回答
11
投票

您可以使用此实现:

List<LogEntry> logEntries = driver.manage().logs().get("logcat").getAll();

在退出司机之前。然后只需将列表打印到外部文件即可。

该方法看起来像这样:

public static void captureLog(AppiumDriver driver, String testName)
    throws Exception {
    DateFormat df = new SimpleDateFormat("dd_MM_yyyy_HH-mm-ss");
    Date today = Calendar.getInstance().getTime();
    String reportDate = df.format(today);
    String logPath = "C:\\automation_capture\\";
    log.info(driver.getSessionId() + ": Saving device log...");
    List<LogEntry> logEntries = driver.manage().logs().get("logcat").filter(Level.ALL);
    File logFile = new File(logPath + reportDate + "_" + testName + ".txt");
    PrintWriter log_file_writer = new PrintWriter(logFile);
    log_file_writer.println(logEntries );
    log_file_writer.flush();
    log.info(driver.getSessionId() + ": Saving device log - Done.");
    }
}

2
投票

但要小心!单个LogEntry的函数.getTimestamp()返回不正确的值

List<LogEntry> adbLogs = driver().manage().logs().get("logcat").filter(Level.ALL);
log.info("First timestamp: " + adbLogs.get(0).getTimestamp());
log.info("Last timestamp: " + adbLogs.get(adbLogs.size()-1).getTimestamp());

输出(有点格式化):

第一个时间戳:1 514 841 545 766

上次的时间戳:1 514 841 594 154

真正的logcat的一部分:

1514841545767 01-01 19:39:48.570 bla-bla-bla-first-record

1514841594154 01-01 23:19:53.440 bla-bla-bla-last-record


0
投票

您可以使用:

Process process = Runtime.getRuntime().exec("//Users//.....//.....//android-sdk-macosx//platform-tools//adb logcat -d");
© www.soinside.com 2019 - 2024. All rights reserved.