This post is published at Android Weekly.
I wrote Espresso tests for TextSwitcher and faced with some problems. In this post i will describe the source of the problem and the solution. Let me firstly describe what the app does. There is a button and and a text. When you click the button the text changes to another text with an animation. I used TextSwitcher with an animation to make it happen.
Below is the MainActivity:
Followings are the layouts:
Starting with with Android 2.2, you can record Espresso tests just by using the app and Android Studio creates test code for you. This is a great feature because sometimes i cant figure out how to write Espresso tests for some views like Dialogs etc.. and you need to dig into documentation and stack overflow. This feature will give you hint to write such test codes. I tried Espresso Recording for the first time with a test that contains TextSwitcher. I recorded the test and ran the test. It failed My first impression with Espresso Recording was not good. Test first asserts that a text exists with text “Hello World”, then click the button and assert that text changed to “Bye World”.
I looked at the test code generated with Android Studio:
This test code failed with the following error:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: com.example.test.myapplication:id/textview and with text: is “Bye World” and Child at position 0 in parent (with id: com.example.test.myapplication:id/text_switcher and Child at position 1 in parent an instance of android.widget.RelativeLayout) and is displayed on the screen to the user)
I didnt look further details of the error. The generated code seems to me a bit complicated and wrote it again. So that it will be simple to find the error. Following is the simplified version of the test code:
This test code also failed with the following error
android.support.test.espresso.AmbiguousViewMatcherException: ‘(with id: com.example.test.myapplication:id/textview and with text: is “Hello World”)’ matches multiple views in the hierarchy.
Problem views are marked with ‘****MATCHES****’ below.
When i looked at the error, i noticed that there are two TextViews. However, there was only one in the layout.xml. I looked at the TextSwitcher source code. It extends from ViewSwitcher and there is the following code in ViewSwitcher:
As you see in the code, ViewSwitcher creates two instances of the child view. This means two TextViews are added in my view. So test code cant decide which TextView to use. Solution is to help test code to choose one of the views. One of the TextViews’s visibility is gone and other’s is visible. So you can choose the visible one in the test code. I updated the test code as the following:
With this test code, my test passed. You can also correct the Android Studio Espresso Recorder test code by correcting the child position index. I figured out this solution later. However, i learnt the internal of the ViewSwitcher with this error.