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

EspressoでRecyclerViewの中身を比較

More than 3 years have passed since last update.

EspressoでRecyclerViewの中身を比較

Espressoに標準で用意されていないので、自分でmatherを作る必要があります

  • RecyclerViewに用意されているfind系のメソッドにはこのようなものがあります
    • findChildViewUnder(float x, float y)
    • findContainingItemView(View view)
    • findContainingViewHolder(View view)
    • findViewHolderForAdapterPosition(int position)
    • findViewHolderForItemId(long id)
    • findViewHolderForLayoutPosition(int position)
    • findViewHolderForPosition(int position)

今回はRecyclerViewの findViewHolderForAdapterPosition(int position)を使用します
適宜 使いやすいメソッドを使用して下さい

ViewHolder

class MyViewHolder(v: View) : RecyclerView.ViewHolder(v) {
            var title: TextView           
            ...
}

matcherのメソッドを作成

  fun hasText(position: Int, text: String) = object : TypeSafeMatcher<View>() {
        var innerText: String? = null

        override fun describeTo(description: Description) {
            description.appendText(String.format(text + "is not equal to %s.", text))
        }

        override fun matchesSafely(view: View): Boolean {
            if (view !is RecyclerView) return false

            val holder = view.findViewHolderForAdapterPosition(position) as MyViewHolder
            innerText = holder.title.text.toString()
            return holder.description.text == text
        }
    }

使用側

@Test
fun testRecyclerView() {
    val position = 0
    val text = "hoge"
    onView(withId(R.id.recyclerView)).check(matches(hasText(position, text)))
}
AAkira
Android Engineer, Kotlin
http://aakira.app
cyberagent
サイバーエージェントは「21世紀を代表する会社を創る」をビジョンに掲げ、インターネットテレビ局「AbemaTV」の運営や国内トップシェアを誇るインターネット広告事業を展開しています。インターネット産業の変化に合わせ新規事業を生み出しながら事業拡大を続けています。
http://www.cyberagent.co.jp/
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
ユーザーは見つかりませんでした