2016-11-28 72 views
2

我正在使用futuresimple的Android Floating Action Button。我想用Espresso來創建與之交互的UI測試。 multiple_actions只是打開浮動操作按鈕(FAB)菜單的按鈕。 draw_fab是單擊multiple_actions時出現的浮動操作按鈕之一。點擊draw_fab開始一個新的android活動,在這個活動中,我希望按一個id爲componentMenuButton的標準按鈕(它本身會顯示一個菜單)。Android上的Espresso UI測試浮動操作按鈕菜單項

@Test 
public void simpleCircuitTest() { 
    onView(withId(R.id.multiple_actions)).perform(click()); 
    onView(withId(R.id.draw_fab)).perform(click()); 
    onView(withId(R.id.componentMenuButton)).perform(click()); 
    // other stuff... 
} 

當我運行這個測試時,我看到第一次點擊工作和浮動菜單按鈕顯示。然而,點擊draw_fab的呼叫似乎沒有任何影響,因此在達到點擊componentMenuButton的呼叫時,出現No views in hierarchy found matching: with id...錯誤。

這是我困惑的地方。

@Test 
public void simpleCircuitTest() { 
    onView(withId(R.id.draw_fab)).check(matches(isDisplayed())); 
    View v = mActivityRule.getActivity().findViewById(R.id.draw_fab); 
    Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE)); 
    Log.d(TAG, String.valueOf(v.isShown())); 
    Log.d(TAG, String.valueOf(v.isEnabled())); 

    onView(withId(R.id.multiple_actions)).perform(click()); 
    onView(withId(R.id.draw_fab)).check(matches(isDisplayed())); 
    Log.d(TAG, String.valueOf(v.getVisibility()==v.VISIBLE)); 
    Log.d(TAG, String.valueOf(v.isShown())); 
    Log.d(TAG, String.valueOf(v.isEnabled())); 
} 

以上就是我試圖搞清楚是怎麼回事。當我運行這個測試時,即使我還沒有點擊multiple_actions FAB,isDisplayed通過,並且所有的日誌輸出都是真的。然後在下一部分中,Espresso點擊multiple_actions並再次輸出所有信息。所以就好像點擊multiple_actions來點擊draw_fab點擊對測試通過沒有任何影響。這不是應該如何?

我的預感現在是我用於浮動動作按鈕的回購單不支持Espresso使用?這或那裏有一些特別的關於FABs或關於Espresso的基本信息,我錯過了。這是什麼?

回答

1

如果我看我activity_home.xml爲我想測試我對晶圓廠以下活動(浮動操作按鈕)

<com.getbase.floatingactionbutton.FloatingActionsMenu 
    android:id="@+id/multiple_actions" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    fab:fab_addButtonColorNormal="@color/colorPrimary" 
    fab:fab_addButtonColorPressed="@color/white_pressed" 
    fab:fab_addButtonPlusIconColor="@color/white" 
    fab:fab_labelStyle="@style/menu_labels_style" 
    android:layout_marginBottom="16dp" 
    android:layout_marginRight="16dp" 
    android:layout_marginEnd="16dp"> 

    <com.getbase.floatingactionbutton.FloatingActionButton 
     android:id="@+id/draw_fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     fab:fab_colorNormal="@color/colorPrimary" 
     fab:fab_title="Draw Circuit" 
     fab:fab_colorPressed="@color/white_pressed" 
     fab:fab_icon="@drawable/ic_draw"/> 

所以我很自然地認爲ID multiple_actions可以用來點擊打開FAB菜單的FAB。然而,看着我的項目values.xml我有一個被我用FAB庫生成我看到

<item name="fab_expand_menu_button" type="id"/> 

這可能來自here。使用這個ID(我沒有寫),所有東西都是固定的。我假設FAB ID是硬編碼的頂級。

@Test 
public void simpleCircuitTest() { 
    onView(withId(R.id.fab_expand_menu_button)).perform(click()); 
    onView(withId(R.id.draw_fab)).perform(click()); 
    onView(withId(R.id.componentMenuButton)).perform(click()); 
    // other stuff... 
} 

更有趣的是,當我使用multiple_actions我想通了,它在屏幕上的其他地方按下(沒有,因爲沒有視覺反饋實現的,但後來我放在HorizontalScrollView佔用整個屏幕,果然我看到Espresso單擊它),這就意味着爲什麼這些日誌值總是返回true。這些值對於屏幕上的另一個元素而言是真實的,該元素不會被視爲可見或不是通過單擊FAB