【问题标题】:AndroidJUnit test does not find design support snackbar on API level 17 deviceAndroidJUnit 测试在 API 级别 17 设备上找不到设计支持快餐栏
【发布时间】:2016-01-09 23:19:36
【问题描述】:

我有一个 AndroidJUnit4 测试用例,在这个测试用例中我使用以下代码来验证小吃栏是否显示并包含正确的文本:

onView(withId(android.support.design.R.id.snackbar_text)).check(matches(isDisplayed()));
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.msg_error_unknown_user)))
                    .check(matches(isDisplayed()));

当我在 API 级别 22 的模拟器/设备上运行此测试时,测试运行正确。当我切换到 API 级别 17 时,测试失败并出现以下异常:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.mydomain.myappname:id/snackbar_text

请注意,应用程序在两个 API 级别上的行为方式相同;小吃栏正在正确显示。

为什么在我的测试用例中没有在 API 级别 17 上检索到快餐栏,我该如何解决这个问题?

【问题讨论】:

    标签: android android-espresso androiddesignsupport android-junit


    【解决方案1】:

    好吧,Espresso 正在查看您的 xml 文件并搜索 snackbar_text,但它找不到它,因为它具有 android 系统 ID,而 Espresso 无权访问。如果您仍想检查它,请了解另一个名为 uiautomator 的 Google 仪器测试框架。

    在这里你会发现它是如何与Espresso一起工作的:http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

    另外请花点时间看看我已经在下面的代码中测试了Snackbar

    @Test
    public void checkIfSnackBarIsProperlyDisplayed() throws InterruptedException {
            onView(withId(R.id.fab)).perform(click());
            onView(withText(R.string.function_not_available)).check(matches(isDisplayed()));
    
        }
    
        @Test
        public void clickOnFloatingActionButtonToShowSnackBar() {
            onView(withId(R.id.fab)).perform(click());
            onView(withId(R.id.snackbar_text))
                    .check(matches(withText(R.string.function_not_available)));
    
        }
    
        @Test
        public void swipeRightToDismissDisplayedCommunique() {
            onView(withId(R.id.fab)).perform(click());
            onView(withId(R.id.snackbar_text))
                    .perform(swipeRight())
                    .check(matches(not(isCompletelyDisplayed())));
    
        }
    }
    

    希望你觉得它有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2019-11-21
      • 2017-01-31
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多