Android Espresso - 等待直到显示对话框

标签 android android-espresso android-testing

编写等待对话框的测试的可能方法是什么,应该支持dialogFragment和Dialog(AlertDialog)?

最佳答案

这就是我能够弄清楚的方式,发布示例代码可能会对其他人有所帮助。这个想法是定期检查,直到真正超时发生。

public static void waitForDialogWithText(String text, long timeout) {
    waitForDialog(withText(text), timeout);
}

private static void waitForDialog(Matcher<View> viewMatcher, long timeout) {
    final long endTime = System.currentTimeMillis() + timeout;
    Exception exception = null;
    while (System.currentTimeMillis() < endTime) {
        try {
            // wait for x second // Thread.sleep(x mills)
            onView(viewMatcher).check(matches(isDisplayed()));
            return;
        } catch (Exception e) {
            exception = e;
        }
    }
    if (exception != null) {
        throw new RuntimeException(exception.getMessage());
    }
}

关于Android Espresso - 等待直到显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67333426/

相关文章:

AndroidJUnit4 和参数化测试

Android BLE - 确定特征的存储值类型

android - 写入cpu0/cpufreq/cpuinfo_min_freq : open failed: EACCES (Permission denied)

android - 在为 Android 进行 Espresso 功能测试时让 Dagger 注入(inject)模拟对象

ui-automation - 用于测试的 Webview 元素检查

android - 葫芦安卓 : Is it possible to use wildcards in `query` ?

java - 错误 :Execution failed for task ':app:processDebugResources' . > java.io.IOException:无法在 android studio 中删除文件夹 ""

android - shell-查找文件中的su命令的结果android

java - Espresso 2.0 - 在扩展 junit3 测试用例的类中用 @Test 注释的方法

java - 带有 fragment 的 Android Espresso 功能测试