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

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/.../MainActivityTest.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