Android: Using UI Automator or Espresso for automated UI testing of Cordova/Ionic apps?

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.

The only thing I could Google until now is this here, which seems to help a bit but adds so many questions as well:

Does anyone of you use UI Automator or Espresso?

So this actually works as intended:

  1. 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/.../ and adds some stuff to build.gradle with dependencies and config.
  2. 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”.
  3. Now you can overwrite the test in the generated file with your own tests and do whatever you want.

If your tests need additional permissions, have a look at Cordova Android: Request Permissions only if ...?

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.

1 Like

This is now solved as well:

Question someone had about exactly this:
Strange workaround that solved it:
And now my real solution after piecing it together:

Culprit is the eclipse like structure of the Cordova Android project.

Where do you store your tests in the git repo of your ionic project?