2016-10-22 40 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」是否工作不同,理想情況下不應該。 –

回答