Does anyone have any pointers how to add Espresso or UI Automator tests to a Cordova Android project?
I want to test the UI and instrument screenshot creation, but have problems with the pretty old project format of the Android project that Cordova creates.
Open the Cordova Android project (platforms/android) in Android Studio, hit Run -> “Record Espresso Test” and add an Assertion that the WebView is there, finish. This creates the folders and file necessary (src/androidTest/java/.../MainActivityTest.java) and adds some stuff to build.gradle with dependencies and config.
Move these changes out of build.gradle into build-extra.gradle (that the main build.gradle imports) so they are contained together and not inside the build.gradle that Cordova “manages”.
Now you can overwrite the test in the generated file with your own tests and do whatever you want.
Note: There is some strangeness going on that you have to use compile instead of androidTestCompile to require the dependencies, which is probably caused by the sourceSets of the default generated Cordova build.gradle, but I just swallowed that and didn’t investigate further as I don’t really care if debug has a few more deps and not only the test.