Expresso 的 Android 测试首选项 fragment

标签 android testing android-preferences checkboxpreference expresso

我在通过 Expresso 测试我的代码时遇到问题。我写了这段代码:

public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}


public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preferences_xml">
<PreferenceCategory
  android:title="@string/application_settings"
  android:id="@+id/application_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_processing_private_data_preference"
    android:title="@string/title_processing_private_data_preference"
    android:summary="@string/summary_processing_private_data_preference"
    android:defaultValue="true"
    android:dependency="@string/key_recording_audio_preference"
    android:id="@+id/private_data_check_box_preference"/>
</PreferenceCategory>

 <PreferenceCategory
  android:title="@string/device_settings"
  android:id="@+id/device_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_recording_audio_preference"
    android:title="@string/title_recording_audio_preference"
    android:summary="@string/summary_recording_audio_preference"
    android:defaultValue="true"
    android:id="@+id/recording_audio_check_box_preference"/>

  </PreferenceCategory>
</PreferenceScreen>

我的测试规则:

@Rule
public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
        SettingsActivity.class);

我尝试先测试复选框:

onData(anything())
            .inAdapterView(allOf(
                    isDescendantOfA(withId(R.id.preferences_xml)),
                    withId(R.id.application_settings_preferences)))
            .atPosition(1)
            .check(matches(isDisplayed()));

测试总是失败,并出现 NoMatchingViewException:层次结构中没有找到匹配的 View :

http://prntscr.com/bv8xlb

我也尝试:

String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference);
   onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 

测试总是失败并出现 NullPointerException:

http://prntscr.com/bv8w04 .

onView(withId(R.id.working_in_background_check_box_preference)).check(matches(isDisplayed()));

也因 NoMatchingViewException 失败。

有人可以展示一个包含正确测试用例的示例吗?

最佳答案

(代表 OP 发布)

已解决:

onData(allOf(
            is(instanceOf(Preference.class)),
            withKey(key),
            withSummary(R.string.summary_working_in_background_preference),
            withTitle(R.string.title_working_in_background_preference)))
            .onChildView(withText(title))
            .check(matches(isCompletelyDisplayed()));

关于Expresso 的 Android 测试首选项 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478692/

相关文章:

Android、Openfire、Smack、XMPP

android - GoogleMap onCameraIdle 事件未触发

javascript - react-native-table-component 卡住可滚动表中的第一列和第一行

android:如何使设置导航像 native 设置应用程序一样

android为首选项屏幕设置分隔符填充

java - 无法实现 Android 设计支持库

json - hadoop - 验证加载到 hive 仓库中的 json 数据

asp.net-mvc - 使用 IRepository 进行 Entity Framework 测试 - 延迟加载问题

java - 测试组的 TestNG 执行顺序

android - 如何以编程方式更改自定义首选项布局的 ImageView ?