检查是否使用Espresso显示对话框 [英] Check if a dialog is displayed with 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:
- 检查是否出现对话框
- 执行对话框按钮的点击
- 与对话框的内部视图进行交互(如果是自定义视图)
- 瓶坯在对话框外单击,并检查其是否显示(例如,是否在对话框构建器上调用了
setCancelable(false)
,我们要进行检查)
- Check if a dialog appears
- Perform clicks on dialog buttons
- Interact with the dialog's inner view (if it's a custom view)
- 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)
谢谢你的建议!
推荐答案
-
要验证是否显示对话框,您可以简单地检查是否显示了对话框中显示的带有文本的视图:
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屋!