2017-08-08 43 views
3

我有一個咖啡測試,其中我的屏幕包含EditText和跳過Button下面。 當我啓動活動時,鍵盤會彈出,重點在EditText,並與Button重疊。
我現在想寫一個跳過按鈕的測試,並斷言之後會發生什麼。意式咖啡並不等待鍵盤打開

問題在於espresso不會等待鍵盤打開。
所以會發生什麼是

  • 咖啡不是等待鍵盤,並按下「跳過」
  • 鍵盤滑開
  • 斷言的東西,現在這就是下面的鍵盤出現故障

代碼看起來像這樣:

public void givenSkipped_whenConfirmed_thenMainActivityLaunched() { 
    Espresso.closeSoftKeyboard();// <- Not working as espresso seems to think it is not open yet 
    skipPostcodeEntry.perform(click()); //<- Can click this as keyboard is not open yet. 

    warningText.check(matches(withText(R.string.some_text))); 

    confirmationButton.perform(click());//<- Fails as this is now overlapped by KB 

    Assert.DoesSomething() 
} 

我發現咖啡是not waiting for the keyboard to close的問題,但沒有等待鍵盤打開。

有沒有人解決過這個問題?

編輯

當你看看closeSoftKeyboard方法,你可以找到一個名爲CloseKeyboardAction類。當鍵盤未被識別爲打開時,您可以看到它甚至會記錄下來。

Log.w(TAG, "Attempting to close soft keyboard, while it is not shown."); 

回答

4

不幸的是,目前似乎Espresso沒有辦法檢查鍵盤是否在屏幕上! (https://groups.google.com/forum/#!topic/android-platform/FyjybyM0wGA

作爲一種解決方法,我們所做的是檢查應該有焦點的輸入字段,然後關閉鍵盤。這樣可以防止咖啡調用closeSoftKeyboard()之前的鍵盤在屏幕上...

@Test 
public void testSomething() { 
    EspressoExtensions.closeKeyboardOnFocused(fieldThatShouldHaveFocus); 
    //Continue with normal test 
} 

然後EspressoExtensions添加到您的項目:

public class EspressoExtensions { 
    /** 
    * This can be used to close the keyboard on an input field when Android opens the keyboard and 
    * selects the first input when launching a screen. 
    * <p> 
    * This is needed because at the moment Espresso does not wait for the keyboard to open 
    */ 
    public static void closeKeyboardOnFocused(ViewInteraction viewInteraction) { 
    viewInteraction.check(matches(hasFocus())).perform(closeSoftKeyboard()); 
    } 
} 

希望這有助於,直到咖啡有辦法斷言是否鍵盤在屏幕上