Help us understand the problem. What is going on with this article?

PreferenceFragmentCompatをEspressoを使ってテストする

More than 1 year has passed since last update.

Googleで検索するとすぐにいくつか情報が見つかるのですが、次のようなものが多いです。

onData(
    allOf(
        `is`(instanceOf(Preference::class.java)),
        withKey("setting_key_runpo")
    )
).perform(click())

しかしこの方法だと色々記述を変えて試してもNoMatchingViewExceptionとなってしまいます。

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class: class android.widget.AdapterView

こちらを参考にすると、どうやら今は内部でRecyclerViewを使っているので、onDataを経由する方法は使えないようです。
そこでこのようにテストを書くことで無事に実行できるようになりました。

onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(0))
onView(withText("表示されている文字")).check(matches(isDisplayed()))

R.id.recycler_view は固定でこの値です。
キーでアクセスができないので少し不便ですが、とりあえずテストがかけるようになったので良しとしましょう。

runners
スポーツで世界を良くしたいエンジニアチーム。応援navi、.finisher、run&といった製品開発をしています!
https://www.wantedly.com/projects/167082
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした