Issue
I have an espresso test where my screen contains an EditText
and a skip Button
underneath.
When I launch the activity the keyboard pops open, focuses on the EditText
and overlaps the Button
.
I now want to write a test for the skip button and assert what happens afterwards.
The issue is that espresso does not wait for the keyboard to open.
So what happens is
- Espresso is not waiting for keyboard and presses "skip"
- Keyboard slides open
- Assertion for something thats now underneath 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 not waiting for the keyboard to open.
Has anyone solved this issue?
Edit:
When you look into the closeSoftKeyboard
method you can find a class called CloseKeyboardAction
. You can see that it even logs when the keyboard is not recognised as open.
Log.w(TAG, "Attempting to close soft keyboard, while it is not shown.");
Solution
Unfortunately at the moment it seems that Espresso has no way of checking whether the keyboard is on screen! (https://groups.google.com/forum/#!topic/android-platform/FyjybyM0wGA)
As a workaround, what we do is to check the input field that should have focus and then close the keyboard. This prevents Espresso calling closeSoftKeyboard() before the keyboard is on screen...
@Test
public void testSomething() {
EspressoExtensions.closeKeyboardOnFocused(fieldThatShouldHaveFocus);
//Continue with normal test
}
Then add the 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 a way to assert whether the keyboard is on screen
Answered By - Emmaaa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.