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

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

问题描述

我正在尝试使用新的 android-test-kit(浓咖啡).但是我找不到有关如何检查对话框是否显示并对其执行某些操作的任何信息(例如,单击肯定和否定按钮,例如e.t.c.).请注意,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天全站免登陆