user2983190
user2983190

Reputation:

Appium how to get adb logcat

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!!

Upvotes: 6

Views: 13803

Answers (3)

Dmity
Dmity

Reputation: 21

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

Upvotes: 2

Eyal Sooliman
Eyal Sooliman

Reputation: 2268

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.");
    }
}

Upvotes: 13

Gaurav
Gaurav

Reputation: 1370

You can use:

Process process = Runtime.getRuntime().exec("//Users//.....//.....//android-sdk-macosx//platform-tools//adb logcat -d");

Upvotes: 0

Related Questions