Recently, I need to test RecyclerView and I am having problems with it.
First of all, I found out that Espresso already “supports” RecyclerViews by providing us with the RecyclerViewActions located in espresso-contrib . So I decided to use it, and my dependencies look like this:
dependencies { androidTestCompile 'junit:junit:4.12' androidTestCompile 'com.squareup.spoon:spoon-client:1.2.1' androidTestCompile 'com.jraska:falcon-spoon-compat:0.3.1' androidTestCompile 'com.android.support:support-annotations:23.1.1' androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1' androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1' androidTestCompile "org.mockito:mockito-core:1.10.19" androidTestCompile "com.google.dexmaker:dexmaker:1.2" androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" }
But since only I am trying to run my test after changing the espresso kernel to espresso-contrib. I see this error:
Test running failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'
I tried to do this. I saw a lot of topics where people gave answers like this one . So I excluded appcompat, supportv4 and recyclerview-v7. For instance:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') { exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' exclude module: 'recyclerview-v7' }
And the tests begin, but ... I cannot use any RecyclerViewActions. Because there are no methods after the recyclerview-v7 exception, which is recommended almost everywhere I have seen on the Internet. When I try to use - RecyclerViewActions.actionOnItemAtPosition - the method that is the core for me, I get this error:
java.lang.NoSuchMethodError: No virtual method findViewHolderForPosition(I)Landroid/support/v7/widget/RecyclerView$ViewHolder; in class Landroid/support/v7/widget/RecyclerView; or its super classes (declaration of 'android.support.v7.widget.RecyclerView' appears in /data/app/com.myapp.debug1-1/base.apk) at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:288) at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Therefore, I would prefer to use this recyclerview-v7 instead of excluding it. I am changing my espresso-contrib import to:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') { exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' }
Repeat the test run, but ... I get an error somewhere during the tests:
android.view.InflateException: Binary XML file line #36: Error inflating class android.support.design.widget.NavigationView
Therefore, I use the latest libraries in my gradle:
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:support-v13:23.1.1' compile 'com.android.support:cardview-v7:23.1.1' compile 'com.android.support:design:23.1.1'
And NavigationView uses the latest RecyclerView 23.1.1. I am trying to find out what is wrong, so I display espresso-contrib dependencies using. / gradlew app: dependencies in the console, and I see:
+--- com.android.support.test.espresso:espresso-contrib:2.2.1 | +--- com.android.support.test.espresso:espresso-core:2.2.1 | | +--- com.squareup:javawriter:2.1.1 | | +--- com.android.support.test:runner:0.4.1 (*) | | +--- com.android.support.test:rules:0.4.1 (*) | | +--- javax.inject:javax.inject:1 | | +--- org.hamcrest:hamcrest-library:1.3 | | | \--- org.hamcrest:hamcrest-core:1.3 | | +--- org.hamcrest:hamcrest-integration:1.3 | | | \--- org.hamcrest:hamcrest-library:1.3 (*) | | +--- com.google.code.findbugs:jsr305:2.0.1 | | +--- javax.annotation:javax.annotation-api:1.2 | | \--- com.android.support.test.espresso:espresso-idling-resource:2.2.1 | +--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:2.0 | | \--- org.hamcrest:hamcrest-core:1.3 | \--- com.android.support:recyclerview-v7:23.0.1 | \--- com.android.support:support-annotations:23.0.1 -> 23.1.1
Ok, so espresso-contrib-2.2.1, which is the newest, uses com.android.support:recyclerview-v7:23.0.1 - not the latest version of recyclerView and causes an error. I decided to update it inside espresso-contrib by adding:
androidTestCompile 'com.android.support:recyclerview-v7:23.1.1'
I reuse applications: dependencies, and see the changes:
\--- com.android.support:recyclerview-v7:23.0.1 -> 23.1.1 (*)
I run the tests again. And I'm not mistaken in NavigationView anymore - I think he solved it, but ... another error appears:
android.view.InflateException: Binary XML file line #21: Error inflating class android.support.v7.widget.Toolbar
And now I'm a little out of ideas. When you look at espresso-contrib dependencies, it does not use anything that the toolbar could use, in my opinion. The toolbar is part of appcompat-v7, so I'm trying to add it in the same way as I updated recyclerView to the newest version. Therefore, I add:
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
But it doesn’t help me. And I'm stuck.
Have you encountered these problems? Do you have a workaround on how to click on recyclerView? How to check if an element in recyclerView has text or something like that? All the solutions I found on the Internet use the findViewHolderForPosition method, which I cannot use after the recyclerview-v7 module exception. For instance:
https://github.com/dannyroa/espresso-samples/tree/master/RecyclerView/app/src/androidTest/java/com/dannyroa/espresso_samples/recyclerview
or
https://gist.github.com/chemouna/00b10369eb1d5b00401b
or more.