Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检查是否显示带有浓缩咖啡的对话框_Android_Android Espresso - Fatal编程技术网

Android 检查是否显示带有浓缩咖啡的对话框

Android 检查是否显示带有浓缩咖啡的对话框,android,android-espresso,Android,Android Espresso,我正试着用新软件写一些测试。但是我找不到任何关于如何检查对话框是否显示并对其执行某些操作的信息(如单击正按钮和负按钮,e.t.c.)。请注意,对话框也可能由网络视图显示,而不是由应用程序本身显示 任何帮助都将不胜感激。我只需要一个链接,或者一些基本的示例代码: 检查是否出现一个对话框 单击对话框按钮 与对话框的内部视图交互(如果是自定义视图) Preform在对话框外部单击,并检查它是否显示(例如,如果在对话框生成器上调用了setCancelable(false),我们想检查一下) 谢谢你的建议

我正试着用新软件写一些测试。但是我找不到任何关于如何检查对话框是否显示并对其执行某些操作的信息(如单击正按钮和负按钮,e.t.c.)。请注意,对话框也可能由
网络视图显示,而不是由应用程序本身显示

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

  • 检查是否出现一个对话框
  • 单击对话框按钮
  • 与对话框的内部视图交互(如果是自定义视图)
  • Preform在对话框外部单击,并检查它是否显示(例如,如果在对话框生成器上调用了
    setCancelable(false)
    ,我们想检查一下)
  • 谢谢你的建议

  • 要验证对话框是否出现,只需检查对话框中是否显示文本视图:

    onView(withText("dialogText")).check(matches(isDisplayed()));
    
    或者,基于id为的文本

    onView(withId(R.id.myDialogTextId)).check(matches(allOf(withText(myDialogText), isDisplayed()));
    
  • 要单击对话框按钮执行此操作(按钮1-确定,按钮2-取消):

    更新

  • 我认为这是可能的,因为浓缩咖啡
  • 不确定是否在自定义对话框视图外部单击,但要检查它是否显示,您必须创建自定义匹配器并在其内部进行检查

  • 我目前使用这个,它似乎工作得很好

    onView(withText(R.string.my_title))
        .inRoot(isDialog()) // <---
        .check(matches(isDisplayed()));
    
    onView(带文本(R.string.my_title))
    
    .inRoot(isDialog())/为了回答问题4(被接受的答案没有),我修改了以下代码,我在堆栈溢出()上找到了这些代码,用于测试是否显示了Toast

    @NonNull
    public static ViewInteraction getRootView(@NonNull Activity activity, @IdRes int id) {
        return onView(withId(id)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
    }
    
    传入的
    id
    是当前显示在对话框中的
    视图的id。您也可以这样编写方法:

    @NonNull
    public static ViewInteraction getRootView(@NonNull Activity activity, @NonNull String text) {
        return onView(withText(text)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
    }
    
    现在它正在寻找一个包含特定文本字符串的
    视图

    像这样使用它:

    getRootView(getActivity(), R.id.text_id).perform(click());
    

    按钮id R.id.button1和R.id.button2在不同的设备上是不同的。ID可能会随着操作系统版本的变化而变化

    实现这一点的正确方法是使用UIAutomator。 在build.gradle中包含UIAutomator依赖项

    // Set this dependency to build and run UI Automator tests
      androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    
    和使用

    // Initialize UiDevice instance
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    
    // Search for correct button in the dialog.
    UiObject button = uiDevice.findObject(new UiSelector().text("ButtonText"));
    if (button.exists() && button.isEnabled()) {
        button.click();
    }
    

    如果您有这样的AlertDialog:

    您可以检查组件是否显示:

    int titleId = mActivityTestRule.getActivity().getResources()
            .getIdentifier( "alertTitle", "id", "android" );
    
    onView(withId(titleId))
            .inRoot(isDialog())
            .check(matches(withText(R.string.my_title)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.text1))
            .inRoot(isDialog())
            .check(matches(withText(R.string.my_message)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.button2))
            .inRoot(isDialog())
            .check(matches(withText(android.R.string.no)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.button3))
            .inRoot(isDialog())
            .check(matches(withText(android.R.string.yes)))
            .check(matches(isDisplayed()));
    
    并执行以下操作:

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

    以防有人像我一样偶然发现这个问题。所有答案仅适用于带有对话框按钮的对话框。在没有用户交互的情况下,不要尝试将其用于进度对话框。Espresso一直在等待应用程序进入空闲状态。只要进度对话框可见,应用程序就不会处于空闲状态。

    下面我的答案有任何反馈??步骤1在进度对话框中对我不起作用。只是尝试验证对话框的标题和消息,浓缩咖啡和静态导入是什么?这些方法来自哪些类?为什么在堆栈溢出答案上使用静态导入?@jvrodrigues每个浓缩咖啡教程都使用静态导入。我建议你只是习惯它(尽管我理解你的沮丧)。这有助于:对于第4步,您可以调用“pressBack();”来取消对话框,而不是在对话框外单击。这相当于使用“硬件后退”按钮。@denys项目已被移动。看起来链接已经失效了。实际上,
    android.R.id.button1
    android.R.id.button2
    android.R.id.button3
    表示“正”、“中性”和“负”是可以使用的全局符号。如果你选择通过文本来选择按钮——这很好——你不需要UIAutomator,但是可以用Espresso的
    onView(withText(“ButtonTest”))来做同样的事情。执行(click())
    。我将此解决方案与Robotium测试框架一起使用,并且我能够轻松地选择Android操作系统对话框按钮。节省了我很多时间。谢谢jaydeepw@ThomasKeller我过去使用过button1,button2 ID,当我在各种设备上运行它们时,我的测试失败了。显示的对话框是系统控件。不是您的控件/UI。对于UI之外的任何内容,建议使用UIAutomator。就是这样。非常感谢。我案例中的文本id为
    android.R.id.message
    ,标题的隐藏id为
    android.R.id.alertTitle
    。如果您使用来自AppCompat支持库的AlertDialog(或DialogFragment),请使用以下命令:
    int alertDialogTitleId=android.support.v7.AppCompat.R.id.alertTitle如果任何人需要知道参数值。如果您不知道名称或def类型,请尝试检查一个不存在的视图,并查看logcat上的布局层次结构。您将看到一组
    defType=name
    。例如,
    message=hello world
    ,那么您的参数应该像
    “hello world”、“message”、“您的包名”
    onView(withId(android.R.id.button3)).perform(click());