问题
I have one android system with two displays(Display1, display2) connected for my app. my app has an activity that is running on display2. I was trying to use android espresso library to test my activity on display2.
Below is my test code:
onView(withId(R.id.button_test)).check(matches(isDisplayed())); //This line worked fine, it means espresso found my view successfully.
onView(withId(R.id.button_test)).withFailureHandler(new FailureHandler() {
@Override
public void handle(Throwable error, Matcher<View> viewMatcher){
if(error != null){
}
}
}).check(matches(isDisplayed())).perform(click());
I always got an exception:
androidx.test.espresso.PerformException: Error performing 'single click - At Coordinates: 78, 119 and precision: 16, 16' on view 'MyDrawableView{id=2131361875, res-name=button_info, desc=Toggle, visibility=VISIBLE, width=64, height=64, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=true, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@f6fa020, tag=null, root-is-layout-requested=false, has-input-connection=false, x=47.0, y=88.0}'.
It seems the coordinates(78, 119) is located on Display1. I also checked ActivityTestRule source code and found that:
instrumentation = InstrumentationRegistry.getInstrumentation();
this instrumentation was using Display1 context to perform the click event. my question is that: How should I specify instrumentation context? or if you have any better idea on this test.
Thanks so much for your time.
来源:https://stackoverflow.com/questions/58886458/how-can-i-specify-android-instrumentation-context-when-i-run-my-activitytestrule