我重写了 espresso ViewAction 中的 perform 方法,但它不起作用,该方法甚至在代码执行时不调用
I override perform method in espresso ViewAction but it don't work, this method even don't calling when code executing
我想覆盖 espresso perform(click()) 以点击特定位置。我用下一种方式做到了:
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);
}
当我执行它时,我得到 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.access0(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction.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)
当我在调试中 运行 它时,我看到重写的 perform() 方法没有执行。但我不明白我做错了什么。
谢谢
那是因为你的 ViewAction
的约束 returns null
。在它执行一个动作之前,它会检查它的约束,它不应该是空的,所以尝试 return 约束中的匹配器:
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
也许您还应该 return 一个描述操作的字符串。
如果您认为点击的位置正确但不起作用,那是因为您没有调用正确的函数来执行:
actionOnItemAtPosition(position + headersCount ,click()).perform(uiController, view);
我想覆盖 espresso perform(click()) 以点击特定位置。我用下一种方式做到了:
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);
}
当我执行它时,我得到 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.access0(ViewInteraction.java:63)
at android.support.test.espresso.ViewInteraction.call(ViewInteraction.java:153)
at android.support.test.espresso.ViewInteraction.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)
当我在调试中 运行 它时,我看到重写的 perform() 方法没有执行。但我不明白我做错了什么。 谢谢
那是因为你的 ViewAction
的约束 returns null
。在它执行一个动作之前,它会检查它的约束,它不应该是空的,所以尝试 return 约束中的匹配器:
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
也许您还应该 return 一个描述操作的字符串。
如果您认为点击的位置正确但不起作用,那是因为您没有调用正确的函数来执行:
actionOnItemAtPosition(position + headersCount ,click()).perform(uiController, view);