【问题标题】:Espresso: calling openActionBarOverflowOrOptionsMenu() opens the first item in the menuEspresso:调用 openActionBarOverflowOrOptionsMenu() 打开菜单中的第一项
【发布时间】:2016-09-21 22:43:43
【问题描述】:

我有以下浓缩咖啡测试:

    openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());

    // if I Thread.sleep() here, I can see that the MenuItem has been clicked already

    onView(withText("Sign in")) //<= click on the MenuItem
            .perform(click());

    onView(withId(R.id.signupButton)) //<= click the signup button in my UI
            .perform(click());

那里的第一行打开溢出菜单并同时单击第一项(恰好是登录项)。所以测试失败,因为它找不到 MenuItem 视图。有什么我做错了吗?我正在使用模拟器 API 22,再次编译 targetSdk 24 并使用 espresso 2.2.1。

【问题讨论】:

  • 我想您在测试类中使用了 ActivityTest Rule。请尝试使用openActionBarOverflowOrOptionsMenu(mActivityRule.getActivity());

标签: android functional-testing android-espresso


【解决方案1】:

试试看:

public class EspressoMatchers {    
    public static Matcher<View> withOverflowMenuButton() {
        return anyOf(allOf(isDisplayed(), withContentDescription("More options")),
                allOf(isDisplayed(), withClassName(endsWith("OverflowMenuButton"))));
    }
}

打开溢出菜单:

onView(allOf(EspressoMatchers.withOverflowMenuButton(),
             isDescendantOfA(withId(R.id.toolbar)))).perform(click());

那么它应该可以正常工作。只需为您的Toolbar 使用正确的 ID。我知道这只是 Espresso 课程的副本,但我也遇到了这个问题,这对我有帮助。

请记住始终“按名称”单击菜单项,而不是按其 ID,因为 ID 不起作用。所以你的“点击项”应该没问题:

onView(withText("Sign in")) //<= click on the MenuItem
        .perform(click());

【讨论】:

  • @mbonnin 您是否在测试设备上禁用了动画?
猜你喜欢
  • 2012-10-04
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多