2016-09-21 18 views
1

我有下面的咖啡测试:咖啡:调用openActionBarOverflowOrOptionsMenu()在菜单中打开的第一个项目

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。

+0

我想你正在测试类中使用ActivityTest规则。请尝试使用'openActionBarOverflowOrOptionsMenu(mActivityRule.getActivity());' – piotrek1543

+0

同样的问题:-( – mbonnin

回答

0

试一下:

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()); 

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

请记住总是点击菜单项“按名称”,而不是他们的ID,因为ID不起作用。所以你的“点击项目”应该很好:

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

同样的问题:-( – mbonnin

+0

@mbonnin是否禁用了测试设备上的动画? – tomrozb

相关问题