检查 Espresso 是否显示对话框 [英] Check if a dialog is displayed with Espresso

查看:24
本文介绍了检查 Espresso 是否显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 android-test-kit (浓缩咖啡).但是我找不到关于如何检查对话框是否显示并对其执行一些操作(例如单击正负按钮等)的任何信息.请注意,对话框也可能由 WebView 显示,而不是由应用程序本身显示.

I'm trying to write some tests with the new android-test-kit (Espresso). But I can't find any information on how to check if a dialog is displayed and perform some actions on it (like clicking the positive and negative buttons, e.t.c.). Note that a dialog may be also displayed by a WebView, not by the application it self.

任何帮助将不胜感激.我只需要一个链接,或者一些基本的示例代码:

Any help would be appreciated. I just need a link, or some example code for the basics:

  1. 检查是否出现对话框
  2. 点击对话框按钮
  3. 与对话框的内部视图交互(如果是自定义视图)
  4. 在对话框外预成型点击,并检查它是否显示(例如,如果 setCancelable(false) 在对话框构建器上被调用,我们想要检查)
  1. Check if a dialog appears
  2. Perform clicks on dialog buttons
  3. Interact with the dialog's inner view (if it's a custom view)
  4. Preform clicks outside the dialog, and check if it's displaying or not (for example if setCancelable(false) was called on the dialog builder and we want to check that)

谢谢你的建议!

推荐答案

  1. 要验证是否出现对话框,您可以简单地检查是否显示了对话框内的文本:

  1. To verify if dialog appears you can simply check if View with a text that present inside the dialog is shown:

onView(withText("dialogText")).check(matches(isDisplayed()));

或者,基于带有 id 的文本

or, based on text with id

onView(withId(R.id.myDialogTextId)).check(matches(allOf(withText(myDialogText), isDisplayed()));

  • 要单击对话框按钮,请执行此操作(按钮 1 - 确定,按钮 2 - 取消):

  • To click on dialogs button do this (button1 - OK, button2 - Cancel):

    onView(withId(android.R.id.button1)).perform(click());
    

    更新

    这篇关于检查 Espresso 是否显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆