I decided to use Espresso for this test. Up to now, I sort of avoided UI tests, because they were always reasonably complex to create and for the most part I was able to test business logic with plain unit tests. I knew a lot of effort had been put into Android on the testing front and had watched a good presentation from Google I/O 2016 on Espresso, so I decided to give it a try.
The test plan
- Scroll the recycler view to the appropriate position
- Check to see that the favorite status on the card was currently "not favorited"
- Click the "favorite" button to toggle the favorite status
- Check to see that the favorite status was updated to "favorited"
Getting started with Espresso for RecyclerView
As you can see there are two steps. First is positioning the RecyclerView and then next is interacting with it. Positioning is optional if the particular row is on screen. However, I discovered that if the test needs to actually perform a scroll, you need to do that as a separate action or the click would fail more times than not.
The catch
Creating the solution
With a little more effort I was able to figure out how to create an action that took a view matcher and an action. The view matcher would search for the descendant view starting at the specific view at position X in the RecyclerView. After finding the view it would perform the action specified. Now that I had my re-usable actions, my test case look like:
I realized others would benefit from these actions to I created a library and published it to GitHub. While the library solves problems with RecyclerView, it has no direct dependencies on RecyclerView so it could solve other situations where you need to interact with a descendant view.
You may find the project here: EspressoDescendantActions
Let me know how it works for you and if you find any bugs.
It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait to read. I am impressed with your work and skill. Thanks. list in kotlin
ReplyDelete