Call view methods when testing by Espresso and Kotlin in Android

When you write UI test with espresso it's common when you need to call a method on a testing view, and it’s very easy, all you have to do is define a new View Action like below:

This method has two problems, first, it makes the code ugly! and second, you have to define this for every single method!

I’m here to tell you that there is a much better way that solves both these issues, the calling code would be like this and it works on any view even custom views.

To make this code work all you have to do is to define a generic function that returns a ViewAction like below:

This function uses generic types so it can use the type to cast the view so when you’re testing you don’t have to cast the view yourself, you simply call the methods on the view.

Second, it uses a cool feature of Kotlin that allows you to pass a function as a parameter to another function, and in perform section it simply calls that function, so you can pass any function to this method and it simply runs it for you!

Note: When this method fails for any reason the error message is not very intuitive by default, its something like this:

I don’t usually care about this because even without the error message it gives me enough data to debug the issue but if you want a better message you can use this ViewAction like this:

That turns the error to be like this:

I hope this helps you write cleaner code for testing, Let me know what you think in the comments section below.

Android Infrastructure Engineer @Spotify Check out my LinkedIn profile http://linkedin.com/in/alzahm/ or email me a.ahmadi.dev@gmail.com

The (retired) Pub(lication) for Android & Tech, focused on Development