Espresso不会等到显示对话框并失败

ada*_*aRi 5 android android-espresso

我正在使用Espresso进行一些简单的测试。其中之一是关于单击视图并检查是否显示对话框。

我的问题是有时有效,有时无效。仅当我在检查对话框之前先入睡时,它才始终有效。有没有不使用睡眠的解决方案?

这是我的代码(非常简单):

onView(withId(R.id.forgot_password)).perform(click());
// only works if use Thread.sleep(ms) here
onView(withText(R.string.reset_password)).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

编辑:

我正在显示带有静态助手的对话框,但是简化了。而且我不在中间执行任何后台任务。

最终的TextInputDialog textInputDialog = new

TextInputDialog.Builder(context)
                .setTitle(titleId)
                .setInputType(inputType)
                .setHint(hintId)
                .setPreFilledText(preFilledText)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(positiveButtonId, onTextSubmittedListener)
                .create();
textInputDialog.show(textInputDialog);
Run Code Online (Sandbox Code Playgroud)

谢谢!

Mig*_*Slv 6

在某些情况下,禁用动画是不可能的,例如:

  • 在云设备上运行测试时。Firebase 测试实验室不允许更改设备配置
  • 当您等待某个后台线程更新用户界面时,例如 http 响应。

在这些情况下,最简单的方法是等待元素显示。方法如下:

简单的帮手

class WaifForUIUpdate {

public static void waifForWithId(@IdRes int stringId) {
    
    ViewInteraction element;
    do {
        waitFor(500);

        //simple example using withText Matcher.
        element = onView(withText(stringId));

    } while (!MatcherExtension.exists(element));

}

static void waitFor(int ms) {
    final CountDownLatch signal = new CountDownLatch(1);

    try {
        signal.await(ms, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
}
}
Run Code Online (Sandbox Code Playgroud)

来自twisterrob的匹配器

public class MatcherExtension {
 @CheckResult
    public static boolean exists(ViewInteraction interaction) {
        try {
            interaction.perform(new ViewAction() {
                @Override
                public Matcher<View> getConstraints() {
                    return any(View.class);
                }

                @Override
                public String getDescription() {
                    return "check for existence";
                }

                @Override
                public void perform(UiController uiController, View view) {
                    // no op, if this is run, then the execution will continue after .perform(...)
                }
            });
            return true;
        } catch (AmbiguousViewMatcherException ex) {
            // if there's any interaction later with the same matcher, that'll fail anyway
            return true; // we found more than one
        } catch (NoMatchingViewException ex) {
            return false;
        } catch (NoMatchingRootException ex) {
            // optional depending on what you think "exists" means
            return false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

用法

WaifForUIUpdate.waifForWithId(R.string.some_string);
//now do your validations
Run Code Online (Sandbox Code Playgroud)


ada*_*aRi 4

最后看来问题出在动画上。为了使 Espresso 正常工作,需要在开发者选项菜单中禁用动画。

禁用动画

这样的话,问题就解决了。但在其他情况下,问题可能是后台任务,就像我的问题的评论所暗示的那样。所以我建议看看IdlingResource https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.pw55uipfj或这个Espresso: Thread.sleep( );