Skip to content

Add resilience to UI tests for frozen/unresponsive apps#34023

Merged
PureWeen merged 35 commits intomainfrom
uitest-resilience-frozen-apps
Feb 26, 2026
Merged

Add resilience to UI tests for frozen/unresponsive apps#34023
PureWeen merged 35 commits intomainfrom
uitest-resilience-frozen-apps

Conversation

@PureWeen
Copy link
Copy Markdown
Member

Summary

When an app enters an infinite loop (e.g., layout cycle on iOS), WDA (WebDriverAgent) blocks forever waiting for the main thread to become idle. This causes Appium commands to hang indefinitely, which in turn hangs the entire test run.

This PR adds resilience to prevent test hangs and allows graceful failure with clear error messages.

Changes

HelperExtensions.cs

  • Add RunWithTimeout wrapper (45s hard limit) around all Appium commands
  • Wrap Tap(), Click(), and Wait() query calls with timeout
  • Uses CancellationTokenSource for proper cleanup hygiene
  • Logs warning about orphaned threads when timeout occurs
  • Throws clear TimeoutException when app is unresponsive
  • Uses Task.Run + GetAwaiter().GetResult() to properly unwrap exceptions

AppiumLifecycleActions.cs

  • Add ForceCloseApp command using OS-level termination:
    • iOS: xcrun simctl terminate
    • Android: adb shell am force-stop
    • Mac: osascript quit
  • Wrap CloseApp with 15s timeout, auto-falls back to ForceCloseApp
  • Bypasses WDA when normal Appium termination hangs

UITestBase.cs

  • Catch TimeoutException for unresponsive apps in TearDown
  • Force-terminate and reset session when app freezes
  • Allows subsequent tests to continue instead of hanging forever

Result

Before: Tests hang indefinitely when app freezes, blocking entire CI run

After: Tests fail gracefully after 45s with clear error message:

TimeoutException: An Appium command did not complete within 45s. 
The application may be unresponsive (e.g., due to an infinite layout loop).

Testing

Tested with Issue32586 test case that triggers infinite layout cycle on iOS:

  • Test correctly times out after 45s (previously hung forever)
  • Clear error message indicates app is unresponsive
  • Subsequent tests can continue (session is reset via ForceCloseApp)

Notes

  • 45s timeout balances avoiding false positives on slow CI (normal operations: 10-30s) while still failing reasonably fast when app is truly frozen
  • Thread leak is acceptable: background Task.Run threads may remain blocked after timeout, but ForceCloseApp kills the process which unblocks the socket
  • Debug logging helps diagnose issues in CI

Copilot AI review requested due to automatic review settings February 12, 2026 17:56
@PureWeen PureWeen added the area-ai-agents Copilot CLI agents, agent skills, AI-assisted development label Feb 12, 2026
@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the reliability of the Appium-based UI test infrastructure by preventing indefinite hangs when the app under test becomes frozen/unresponsive (e.g., iOS layout cycles that block WDA), and by adding a force-termination fallback path.

Changes:

  • Add a hard timeout wrapper around select Appium UI interactions and polling queries to fail fast instead of hanging indefinitely.
  • Add a new forceCloseApp command that bypasses Appium/WDA by terminating the app via OS-level commands, and use it as a fallback when closeApp hangs/fails.
  • Update NUnit TearDown handling to attempt force-close/reset when unresponsive timeouts are detected.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.

File Description
src/TestUtils/src/UITest.NUnit/UITestBase.cs Adds TearDown handling intended to force-close/reset when the app becomes unresponsive.
src/TestUtils/src/UITest.Appium/HelperExtensions.cs Introduces RunWithTimeout and applies it to Tap/Click and to the internal Wait(...) polling query calls.
src/TestUtils/src/UITest.Appium/Actions/AppiumLifecycleActions.cs Adds forceCloseApp and wraps closeApp with a 15s timeout + fallback to force-close.

Comment on lines +2938 to +2942
if (!task.Wait(timeout.Value))
{
// Signal cancellation (Appium driver won't respect it, but good hygiene)
cts.Cancel();

Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On timeout, this code throws while the background task continues running and is never observed. If the underlying Appium call later faults, it can surface as an unobserved task exception (noise/flakiness). Consider attaching a continuation in the timeout path to observe/log task.Exception (OnlyOnFaulted) before throwing.

Copilot uses AI. Check for mistakes.
kubaflo
kubaflo previously approved these changes Feb 13, 2026
@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests, maui-pr-devicetests

@PureWeen PureWeen added this to the .NET 10 SR5 milestone Feb 17, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen PureWeen moved this from Todo to Approved in MAUI SDK Ongoing Feb 17, 2026
@PureWeen PureWeen force-pushed the uitest-resilience-frozen-apps branch from 60d8eed to 3c5fc28 Compare February 18, 2026 16:53
@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

kubaflo
kubaflo previously approved these changes Feb 18, 2026
@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen
Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@PureWeen
Copy link
Copy Markdown
Member Author

/azp run

This was referenced Mar 11, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai-agents Copilot CLI agents, agent skills, AI-assisted development

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants