21 May 2024 Leave a comment Tech-Help
Encountering the error “Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds” during Android UI testing can be frustrating. This error often appears when trying to interact with a spinner item in a dialog. In this guide, we’ll explore the most effective solutions to resolve this issue.
Common Causes
This error typically occurs under the following conditions:
- A system dialog, such as “Power Off” or “Unfortunately, [App] has stopped,” is visible.
- A background application is experiencing an Application Not Responding (ANR) event.
Solution: Dismissing System Dialogs
One effective workaround is to dismiss any system dialogs before running your tests. This can be achieved programmatically or via command line.
Programmatic Dismissal
Add the following dependencies to your app/build.gradle
file:
dependencies {
...
androidTestImplementation "androidx.test.uiautomator:uiautomator:2.2.0"
}
In your test class, implement the following method to dismiss system dialogs:
@BeforeClass
public static void dismissAppCrashSystemDialogIfShown() {
try {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
.executeShellCommand("am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS");
} catch (IOException e) {
System.out.println("Exception: " + e);
}
}
Command Line Dismissal
Alternatively, you can use the following ADB command to dismiss system dialogs:
adb shell am broadcast -a android.intent.action.CLOSE_SYSTEM_DIALOGS
Handling ANR Dialogs
If your tests are interrupted by an ANR dialog, you can use UI Automator to press the “Wait” button:
Programmatic Handling
companion object {
@JvmStatic
@BeforeClass
fun dismissANRSystemDialog() {
val device = UiDevice.getInstance(getInstrumentation())
var waitButton = device.findObject(UiSelector().textContains("wait"))
if (waitButton.exists()) {
waitButton.click()
}
waitButton = device.findObject(UiSelector().textContains("待機"))
if (waitButton.exists()) {
waitButton.click()
}
}
}
Command Line Handling
Use the following ADB commands to simulate user interaction:
# Tap the "OK" button on a 320x480 screen
adb shell input tap 233 293
# Simulate keystrokes
sleep 3; adb shell input keyevent 22
sleep 3; adb shell input keyevent 22
sleep 3; adb shell input keyevent 66
Additional Resources
For more detailed information on handling system dialogs and other test automation techniques, please refer to our relevant documentation and blog articles:
Enhancing Your Test Automation with Repeato
While addressing these issues manually can be effective, leveraging a robust test automation tool can streamline the process significantly. Repeato is a no-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps. Its intuitive test recorder and computer vision-based approach ensure quick setup and reliable results.
With Repeato, you can easily dismiss system dialogs and handle complex test scenarios without writing extensive code. For more advanced users, Repeato also provides a scripting interface to automate intricate use cases. Visit our documentation to learn more about how Repeato can enhance your test automation workflows.