Mobile Testing

Auto-capture logs, video & screenshots on failed mobile tests

Reading Time : 12 min read
Auto-capture logs

Key takeaways

  • Hook a failure handler into your test runner so logs, a screenshot and a screen recording save themselves the moment a test fails.
  • On Android, use Tradefed’s log-on-failure flags, Appium’s recording and screenshot commands, or adb as a fallback. On iOS, attach screenshots with XCTAttachment and keep them with .keepAlways.
  • Name every artifact with a test-run ID, device model, OS version and app build so a bundle can be matched to a ticket without guessing.
  • Kobiton records real-device sessions with logs, device details and session context, so evidence from a failed run is already in one place when you file the bug.

The bug ticket that shows up with no evidence

A test fails on a Pixel overnight. By morning there’s a Jira ticket titled “Checkout broken on Android” with one line of description. No logs, no video, no screenshot. The developer asks which build, which device, which OS version. The tester reruns the test, and it passes. Three days later the ticket is closed as “cannot reproduce,” and the bug ships.

Every mobile team I talk to has lived this loop. The fix isn’t more discipline from testers. It’s moving evidence collection out of human hands and into the failure path of your test code. This post walks through how to do that for Android and iOS, which artifacts to collect for which symptoms, and how to turn the output into a ticket an engineer can act on without asking a single follow-up question.

What good failure evidence looks like

Good failure evidence is three artifacts captured together: device logs, a screen recording, and screenshots. Each answers a different question, and none of them substitutes for the others.

  • Device logs are the ground truth. Android logcat and iOS syslog or .xcresult logs show stack traces, ANR messages, network errors and lifecycle events.
  • Screen video gives you the timeline. It shows what happened in the seconds before the failure, which matters for flaky async behavior, animations and race conditions.
  • Screenshots capture the exact UI state at the moment of failure. They also double as a reference for locators and layout mismatches.

Different failures lean on different artifacts. A crash lives in the logs. A UI mismatch is obvious in a screenshot and nearly invisible in a log. A timeout could be a slow network, a blocked main thread or a missing element, and you usually need the video to tell which.

One caveat before you automate any of this: logs contain things you don’t want in a ticket. Auth tokens, emails, device identifiers and request bodies all show up in verbose output. Plan for filtering before attachments reach a shared tracker, and we’ll cover that below.

Android: capture logs, screenshots and video on failure

On Android you have three practical routes: Tradefed’s built-in collectors, Appium commands inside your failure handler, or raw adb. Pick the one that matches how your tests already run.

Option A: Tradefed log-on-failure flags

If you run instrumentation tests through Android’s Trade Federation harness, collection is a configuration choice. The Android Open Source Project documents automated log-on-failure collection with --auto-collect values such as LOGCAT_ON_FAILURE and SCREENSHOT_ON_FAILURE, plus direct flags:

--logcat-on-failure
--screenshot-on-failure

The AOSP page is short and doesn’t cover where files land, how long they’re kept or how to filter them. Run a deliberately failing test once and find the output in your results directory before you trust it.

Option B: Appium start and stop recording, plus screenshots

If your suite uses Appium, wrap every test in a failure handler. Appium exposes startRecordingScreen and stopRecordingScreen, a screenshot endpoint ( GET /session/:session_id/screenshot, returned as Base64), and device log retrieval for logcat on Android and syslog on iOS. A Python pytest sketch:

import base64, pytest

@pytest.fixture(autouse=True)
def evidence(request, driver):
    driver.start_recording_screen()
    yield
    video = driver.stop_recording_screen()
    failed = request.node.rep_call.failed  # set via a pytest hook
    if failed:
        run_id = f"{request.node.name}-{driver.session_id[:8]}"
        with open(f"{run_id}.mp4", "wb") as f:
            f.write(base64.b64decode(video))
        driver.save_screenshot(f"{run_id}.png")
        logs = driver.get_log("logcat")
        with open(f"{run_id}-logcat.txt", "w") as f:
            f.write("\n".join(l["message"] for l in logs))

Start the recording before the test body, not after the failure. You can’t record the past. Discard the video on success if storage matters.

Option C: raw adb for local reproduction

When you need evidence fast on a connected device, adb covers all three:

adb logcat -c                                  # clear old buffer first
adb logcat -d > logcat.txt                     # dump logcat to file
adb exec-out screencap -p > failure.png        # screenshot
adb shell screenrecord /sdcard/run.mp4         # record (Android 4.4+)
adb pull /sdcard/run.mp4

screenrecord stops at three minutes by default, so it suits single-scenario repros rather than full suites. Clear the logcat buffer at the start of each test. Otherwise the dump includes noise from the previous run, and the buffer may have already rolled over the failure you care about.

iOS: attach evidence inside XCTest results

On iOS, the cleanest route is to attach evidence to the test result itself. XCTest supports XCTAttachment objects added inside an XCTActivity, and everything lands in the .xcresult bundle that CI archives.

Here’s a pattern for capturing a screenshot when a test fails:

override func tearDown() {
    if let run = testRun, !run.hasSucceeded {
        XCTContext.runActivity(named: "Failure evidence") { activity in
            let shot = XCUIScreen.main.screenshot()
            let attachment = XCTAttachment(screenshot: shot)
            attachment.name = "\(name)-failure"
            attachment.lifetime = .keepAlways
            activity.add(attachment)
        }
    }
    super.tearDown()
}

The lifetime property is the detail people miss. By default Xcode can discard attachments from passing tests, and setting .keepAlways guarantees retention. Because the screenshot only exists on failure, you won’t bloat results from green runs.

Where video and logs live

XCTest has no equivalent of Tradefed’s one-line flags for video. Screen recording is configured in the Xcode test plan’s capture settings, and recordings appear in the report navigator alongside the logs. For CI, you need to extract them from the bundle. Recent Xcode versions ship xcrun xcresulttool subcommands for exporting attachments, so add that step to your pipeline rather than asking people to open Xcode on a build agent.

Keep the screenshots even if you have video. A screenshot is a single image a developer can open in a ticket preview. A video needs a click, a download and a scrub.

Which evidence to capture for which failure

Match the artifact to the symptom, then choose a preset based on how much storage and run time you can spend.

Failure symptomPrimary evidenceSupporting evidence
Crash or stack traceDevice logsScreenshot of the last screen
ANR or timeoutDevice logs, videoScreenshot
UI mismatch or layout bugScreenshotVideo
Flaky async or race conditionVideoDevice logs
Network or API errorDevice logs, network trace (HAR, if your stack supports it)Video

Two presets cover most teams:

PresetWhat it capturesUse it for
Minimum viableScreenshot and logs on failure onlyEvery PR build, fast feedback
Full fidelityVideo for every test, logs, screenshots, network traceNightly regression, release candidates

BrowserStack’s Espresso documentation makes a related point: some debugging options, such as device logs, network logs and screenshots, must be explicitly enabled rather than coming on by default. Whatever platform you use, check what’s off by default before you assume the evidence exists.

Turn artifacts into a bug report engineers use

Artifacts only help if they travel with the ticket. Package one bundle per failed run and make the ticket template carry the context that no file can.

Every bug filed from a failed run should include:

  1. Test name and the step that failed
  2. Device model and OS version
  3. App build number or commit
  4. A link to the artifact bundle (video, screenshots, log excerpt)
  5. The test-run ID used in the filenames

Name files with that run ID, for example checkout_login-7f3a9c21-pixel8-android15.mp4, so a log and a video from the same run can be correlated by eye. Timestamps do the rest: if the video starts when the test starts, a stack trace at 00:42 in the log points to 00:42 in the recording.

Redact before you attach

Strip secrets at the bundle step, not by hand. A simple pass can mask bearer tokens and email addresses before upload:

sed -E 's/(Bearer )[A-Za-z0-9._-]+/\1[REDACTED]/g; s/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/[EMAIL]/g' logcat.txt > logcat-clean.txt

Trim the log to the window around the failure as well. A 200 MB logcat dump is not a bug report. Network traces carry even more sensitive data than logs, so treat them with stricter access rules.

Where Kobiton fits

Hand-built bundles work, but they’re maintenance you own. On real devices in Kobiton, sessions are recorded with device details and logs, and Session Explorer lets you replay a run and pinpoint where a defect appeared. Kobiton also supports one-click Jira bug filing with logs, device details and session context pre-populated, and it works with Appium, XCUITest and Espresso. For teams with strict data rules, deployment options range from public cloud to full on-prem, so you can keep artifacts and their retention inside your own boundary.

Evidence also has a second life. A recorded manual session can generate an Appium script, so the bug you just reproduced becomes a regression test.

For more on organizing output, see our guide to structuring mobile test results and a deeper look at device log capture.

Implementation checklist

Work through this list once per test stack, then copy it into your team wiki.

  1. A failure hook exists (pytest fixture, JUnit rule, XCTest tearDown, or Tradefed flags).
  2. Logs, screenshots and recording are explicitly enabled, not assumed.
  3. Recording starts before the test body.
  4. Filenames include a test-run ID, device model, OS version and build.
  5. iOS attachments use .keepAlways, and CI extracts them from the .xcresult bundle.
  6. A redaction step runs before anything is uploaded to a ticket.
  7. The bug template has fields for build, device, step and artifact link.

Then verify it. Add a test that fails on purpose, run it in CI, and confirm the video, screenshot and log open from the CI results page without rerunning anything. If someone has to ask “can you rerun it?”, the pipeline isn’t done.

Start small, then add video

If your tickets still arrive empty, start with the minimum viable setup: screenshot and logs on failure. Get the bug template working and the redaction step trusted. Once engineers are actually opening those bundles, add screen recording for your nightly regression runs.

It’s worth a quick tally of how many hours your team lost last quarter to “repro steps and logs, please.” That number tends to justify the afternoon of setup. For related reading, see how to fix device-specific bug reproduction, or how to run automated testing on real devices without a physical lab.

Frequently asked questions

How do I capture logcat automatically when a test fails?

In Tradefed, enable --logcat-on-failure or --auto-collect LOGCAT_ON_FAILURE. In Appium, call get_log("logcat") inside your failure handler and write it to a file. For a quick manual dump, adb logcat -d > logcat.txt works after you clear the buffer with adb logcat -c.

Can I record video of only the failed tests?

Not directly, because a recording can’t start after the failure happens. Record every test, then keep the file only when the test fails. That keeps storage low while still giving you the full timeline for any failure.

Where do XCTest screenshots end up?

They’re stored inside the .xcresult bundle for the test run, attached to the activity or test that created them. Set lifetime to .keepAlways so they aren’t discarded, and add an export step with xcresulttool so CI can publish them.

Should I attach full logs to Jira?

Trim to the window around the failure and redact tokens, emails and identifiers first. Link to the full log in your artifact store instead of attaching it.

Interested in Learning More?

Subscribe today to stay informed and get regular updates from Kobiton

Ready to accelerate delivery of
your mobile apps?

Request a Demo