Issue
I want override espresso perform(click()) for clicking on specific position. I did it in next way:
public void goToCatgory (int position){
ViewAction categoryClick = new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
RecyclerView categoriesRecycler = (RecyclerView) view;
SimpleBindableAdapter<Category> adapter = (SimpleBindableAdapter<Category>) categoriesRecycler.getAdapter();
int headersCount = adapter.getHeadersCount();
actionOnItemAtPosition(position + headersCount ,click());
}
};
categoryList.perform(categoryClick);
}
When I execute it, I get NullPointerException:
java.lang.NullPointerException
at android.support.test.espresso.core.internal.deps.guava.base.Preconditions.checkNotNull(Preconditions.java:882)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:216)
at android.support.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6238)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
When I run it in debug, I saw that overrided perform() method don't execute. But I don't understand what I do wrong. Thanks.
Solution
That's because your ViewAction
's constraint returns null
. Before it performs an action, it checks for its constraint, which should never be null, so try to return a matcher in the constraint:
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
Maybe you should also return a string to describe the action.
If you think you have got the right position for clicking but it's not working, it's because you have not called the right function to perform:
actionOnItemAtPosition(position + headersCount ,click()).perform(uiController, view);
Answered By - Aaron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.