2016-09-21 50 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