Appium how to get adb logcat

2019-05-03 07:41发布

I was wondering if anybody knows a way that i can get the logcat while running automated tests using Appium for android mobile device. I'm using Java and I'm in a windows environment.

Any ideas? Thanks!!

3条回答
爷的心禁止访问
2楼-- · 2019-05-03 08:02

But be carefully! Function .getTimestamp() for single LogEntry returns incorrect value

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

Output (it's a bit formatted):

First timestamp: 1 514 841 545 766

Last timestamp: 1 514 841 594 154

A part of real 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

查看更多
等我变得足够好
3楼-- · 2019-05-03 08:11

You can use:

Process process = Runtime.getRuntime().exec("//Users//.....//.....//android-sdk-macosx//platform-tools//adb logcat -d");
查看更多
Animai°情兽
4楼-- · 2019-05-03 08:16

You can use this implementation:

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

Before quitting the driver. Then just print the list to an external file.

The method will look something like that:

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.");
    }
}
查看更多
登录 后发表回答