Using Espresso to click view inside RecyclerView item

后端 未结 9 1909
醉酒成梦
醉酒成梦 2020-12-02 05:36

How can I use Espresso to click a specific view inside a RecyclerView item? I know I can click the item at position 0 using:

onView(withId(R.i

9条回答
  •  自闭症患者
    2020-12-02 06:09

    I kept trying out various methods to find why @blade's answer was not working for me, to only realize that I have an OnTouchListener(), I modified the ViewAction accordingly:

    fun clickTopicToWeb(id: Int): ViewAction {
    
            return object : ViewAction {
                override fun getDescription(): String {...}
    
                override fun getConstraints(): Matcher {...}
    
                override fun perform(uiController: UiController?, view: View?) {
    
                    view?.findViewById(id)?.apply {
    
                        //Generalized for OnClickListeners as well
                        if(isEnabled && isClickable && !performClick()) {
                            //Define click event
                            val event: MotionEvent = MotionEvent.obtain(
                              SystemClock.uptimeMillis(),
                              SystemClock.uptimeMillis(),
                              MotionEvent.ACTION_UP,
                              view.x,
                              view.y,
                              0)
    
                            if(!dispatchTouchEvent(event))
                                throw Exception("Not clicking!")
                        }
                    }
                }
            }
        }
    

提交回复
热议问题