All posts

Recover Android App Crashes in Phoma Flows

Recover Android App Crashes in Phoma Flows

Why a crashed app breaks a Phoma flow

A Phoma flow usually assumes the Android app stays in a predictable state. You open the app, wait for a screen, tap a button, and continue. When the app crashes, that assumption disappears. The screen may jump to the launcher, a crash dialog, a blank window, or the app's splash screen. The next tap either hits nothing or hits the wrong control, and the flow fails in a confusing way.

For QA testers and operations teams, this is a common source of false failures. The test case may be fine, but the app closed itself during the run. A good Phoma flow should not treat every crash as an immediate failure. It should detect the problem, capture evidence, restart the app, and try the step again a small number of times.

What a crash-safe Phoma flow should do

A crash-safe flow needs a clear recovery path. The goal is not to hide real bugs. The goal is to keep routine runs moving when a phone has a temporary app failure.

Use this pattern:

  1. Open the app and wait for a stable anchor element.
  2. If the anchor is missing, assume the app may have crashed or restarted.
  3. Capture a screenshot so the failure is visible.
  4. Close and reopen the app.
  5. Wait for the anchor again.
  6. Repeat only a limited number of times.
  7. If the app still cannot reach the expected screen, stop with a clear message.

This pattern works because it turns a vague timeout into a deliberate recovery step. It also gives you evidence when the app truly is broken.

Step 1: Choose a reliable recovery anchor

The recovery anchor is the element Phoma should see when the app is ready to continue. It must be stable, visible, and unique enough to prove that the app reached the right screen.

Good anchors include:

  • A dashboard title that appears only after login.
  • A selected bottom-navigation tab.
  • A stable text label such as Home, Orders, or Settings.
  • A content description on an icon that does not change often.

Avoid anchors that are temporary, such as a loading spinner, a splash logo, a network banner, or an element inside a list that may scroll off screen. If the app shows onboarding after a crash, your anchor should account for that state too, or the flow should close onboarding first.

Step 2: Wait for the anchor instead of using a fixed delay

A fixed delay is tempting because it is simple. It is also unreliable. A phone may be slow, an app may need to restore its session, or a network request may take longer than expected. Use a wait-for-element step with a reasonable timeout.

A practical starting point is:

  • 5 seconds for a normal screen transition.
  • 10 seconds after a crash recovery or app restart.
  • Longer only when the app is known to restore a large amount of data.

If the anchor appears, continue. If it does not appear, enter the recovery branch. This is better than tapping blindly after sleeping, because the flow reacts to the actual screen state.

Step 3: Build a bounded recovery loop

Do not retry forever. An endless loop can waste time and hide a serious app defect. Use a loop with a clear limit, such as three attempts.

A simple visual logic looks like this:

Open app
Wait for DashboardTitle
If DashboardTitle is not visible:
    Press key Back
    Close app
    Open app
    Wait for DashboardTitle
    Screenshot crash-recovery-1
If DashboardTitle is still not visible:
    Press key Back
    Close app
    Open app
    Wait for DashboardTitle
    Screenshot crash-recovery-2
If DashboardTitle is still not visible:
    Stop flow with message App did not recover

In Phoma, build this with open app, wait for element, condition, press key, close app, screenshot, and loop steps. Keep the recovery branch short. The shorter it is, the easier it is to maintain.

Step 4: Handle common crash screens

Android does not always show the same screen after a crash. Your flow should handle the common cases without becoming complicated.

The app returns to the launcher

If the expected element is missing and the launcher is visible, close and reopen the app. A press of the Back key can help dismiss a half-open app state before reopening.

A system dialog appears

Some phones show an app has stopped dialog. If your flow can see the dialog text, press the appropriate button or dismiss it, then reopen the app. If the dialog text is not stable across devices, use a screenshot checkpoint and let a human review the failure.

The app opens to a different state

After a crash, the app may open to login, onboarding, or a recovery screen. Add conditions for those states. For example, if a login button is visible, tap it only when the test expects a logged-in state. If a dashboard is visible, continue. This makes the flow tolerant of both a fresh start and a recovered session.

Step 5: Continue only after the app is stable

A common mistake is to resume the flow immediately after the anchor appears. The screen may be visible but not ready for input. Add a second short wait for the control you are about to tap. For example, wait for the target button to be visible and enabled before tapping it.

This is especially important after a crash, because the app may still be restoring data. A stable flow waits for the next meaningful element, not just the first sign of life.

Example: data-entry flow with crash recovery

Imagine a routine where an operator opens an app, taps New Entry, types a reference number, and saves the record. The app occasionally crashes after saving.

A robust Phoma flow could work like this:

Open app
Wait for HomeTab
Tap NewEntryButton
Wait for EntryForm
Type reference number
Tap SaveButton
Wait for SuccessToast or HomeTab
If neither is visible:
    Screenshot save-failure
    Close app
    Open app
    Wait for HomeTab
    Tap NewEntryButton
    Wait for EntryForm
    Type reference number again
    Tap SaveButton
    Wait for SuccessToast or HomeTab

The key idea is that the flow knows what success looks like. It does not assume that tapping Save always works. It checks the result and has a recovery path when the app disappears.

Why this is better than adding longer sleeps

Longer sleeps can make a flaky flow pass sometimes, but they do not explain what happened. They also make every run slower. A wait-and-condition flow is faster when the app is healthy and more informative when it is not.

Crash recovery also gives you better test data. A screenshot from the recovery branch shows whether the app returned to the launcher, showed a dialog, or opened the wrong screen. That is more useful than a generic timeout error.

Troubleshooting crash recovery flows

If your recovery loop never sees the anchor, check these points first:

  • The anchor may be inside a scrollable area and not visible at first.
  • The app may require a permission dialog after restart.
  • The app may show an update prompt or terms screen after a crash.
  • The phone may be offline, so the app cannot restore its session.
  • The selector may match an element that exists in the layout but is not visible.

Use screenshots after each attempt. They show whether the flow is recovering the app or simply repeating the same failure. If the same screenshot appears every time, the recovery branch is not changing the app state enough.

Final checklist

A good crash-recovery section in a Phoma flow should have:

  • One stable anchor element for the expected screen.
  • A wait-for-element step instead of a fixed delay.
  • A limited number of recovery attempts.
  • A screenshot after each failed attempt.
  • A clear stop message when the app cannot recover.
  • A final wait for the control used by the next step.

This approach keeps automated runs moving on real phones while still surfacing genuine app defects. It is especially useful for smoke tests, repetitive data entry, and daily checks where the same app is opened many times on one or many Android devices.