2016-10-22 65 views
0

我想测试PreferenceScreen上的复选框。Android Espresso:无法找到与唯一的Android的CheckBoxPreference:ID

PreferenceScreen包含两个CheckBoxPreferences,每个都有唯一的android:id。

<CheckBoxPreference 
     android:id="@+id/first_checkbox" 
     android:key="first_checkbox" 
     android:title="First checkbox" /> 

    <CheckBoxPreference 
     android:id="@+id/second_checkbox" 
     android:key="second_checkbox" 
     android:title="Second checkbox" /> 

下面是R.java文件中的ID值:

public static final class id { 
    public static final int first_checkbox=0x7f0a0000; 
    public static final int second_checkbox=0x7f0a0001; 
} 

在我的测试:

ViewInteraction cbxFirst = onView(withId(R.id.first_checkbox)); 

我看到:

NoMatchingViewException: No views in hierarchy found matching: with id: 
    com.test.fragmentpreference:id/first_checkbox 

当我试图通过 'android.R.id.checkbox' 而不是 'R.id.first_checkbox' 搜索:

ViewInteraction cbxFirst = onView(withId(android.R.id.checkbox)); 

我收到:

AmbiguousViewMatcherException: 'with id: android:id/checkbox' matches multiple views in the hierarchy. 

我的问题是:我如何使用'first_checkbox'id来测试第一个CheckBoxPreference?

+0

如何使用像onView(withText(“First checkbox”));'? –

+0

@Kamran Ahmed我可以通过'onView(withText(“First checkbox”));''但我的目标是了解如何使用唯一的android:id来访问特定的视图,因为在许多情况下,使用'withText()'时,如果视图很多,文本略有不同,则更容易出错。 –

+0

我同意,我只是想了解更多关于这个问题。在正常情况下,它应该已经工作......我不确定“CheckBoxPreference”是否工作不同,理想情况下不应该。 –

回答