我的espresso检测测试无法点击ImageView:
使用id:com.example.test:id / btnNext'在视图'上执行'单击'时出错。
引起:android.support.test.espresso.PerformException:在视图'unknown'上执行'click(3次尝试后')时出错。
引起:android.support.test.espresso.InjectEventSecurityException:java.lang.SecurityException:注入另一个应用程序需要INJECT_EVENTS权限
但奇怪的是,在它之前只有三行能够点击另一个与我非常相似的ImageView。 并将该应用作为人类工作。
这是我最小的非工作示例,你看到错误吗?
@RunWith(AndroidJUnit4.class)
@LargeTest
public class TestTest {
@Rule
public ActivityTestRule<TestActivity> _activityRule = new ActivityTestRule<TestActivity>(
TestActivity.class);
@Test
public void testClicking() {
onView(withId(R.id.btnPrevious)).perform(click());
onView(withId(R.id.btnNext)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
onView(withId(R.id.btnNext)).check(matches(isClickable()));
onView(withId(R.id.btnNext)).perform(click()); // This line throws an exception
}
}
要测试的活动:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="@+id/lytFooter">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnPrevious"
app:srcCompat="@drawable/left_arrow_shadow"
android:layout_margin="1dp"
android:layout_gravity="center_vertical"
android:background="@drawable/lightbutton_selector"
android:focusable="true"/>
<TextView
android:id="@+id/txtBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/look_around"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
style="@style/CameraText" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prgVideoMode"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:max="100" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnNext"
app:srcCompat="@drawable/left_arrow_shadow"
android:layout_margin="1dp"
android:layout_gravity="center_vertical"
android:background="@drawable/lightbutton_selector"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
完整的stracktrace是80行长所以如果你有兴趣我在这里发布:http://pastebin.com/RWa91G63