Espresso does not wait for the keyboard to open

I have an espresso test where my screen contains EditText

and skip Button

from the bottom. When I launch the activity, the keyboard pops up, focuses on EditText

and overlaps Button

.
Now I want to write a test for the skip button and assert what happens next.

The problem is that espresso doesn't wait for the keyboard to open.
So what's going on

  • Espresso does not wait for the keyboard and presses skip
  • Keyboard slides open.
  • The assertion for something now under the keyboard fails.

The code looks like this:

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

      

I found an issue where espresso was not waiting for the keyboard to close but nothing about it waiting for the keyboard to open.

Has anyone solved this problem?

Edit

When you look at a method closeSoftKeyboard

, you can find a class called CloseKeyboardAction

. You can see that it even registers when the keyboard is not recognized as open.

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

      

+3


source to share


1 answer


Unfortunately at the moment it seems that Espresso has no way of checking if the keyboard is on screen! ( https://groups.google.com/forum/#!topic/android-platform/FyjybyM0wGA )

As a workaround, we should check the input field that should have focus and then close the keyboard. This prevents Espresso from calling closeSoftKeyboard () before the keyboard appears on the screen ...

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

      



Then add EspressoExtensions to your project:

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

      

Hope this helps until Espresso has the ability to assert if the keyboard is on screen

+4


source







All Articles