Many know Arquillian as a great integration, functional, acceptance testing platform. Until recently, I thought of it solely as a great Java EE tool, but an Arquillian extension, known as Droidium, allows you to use Arquillian to help drive your Android testing. I spent some time tonight trying to get it set up for Cub Tracker and thought I’d share what (little) I have so far.
Rather than use Ant as the Android SDK currently does, we’re going to build this test app using Maven. My POM currently looks something like this:
This file is also copy and paste from the repo, with no changes made. A word of warning, though. If you look at the Droidium test project, you will see two .apks checked in: selendroid-server-0.4.2.apk, and selendroid-test-app-0.4.2.apk. The first is for the Selendroid project, which Droidium is built around. The second is the application we intend to test. I’m always bothered by libraries (jars, apks, etc) checked into source control, so I thought I’d be clever and have the build download that APK from Maven Central. Long story short, it doesn’t work as expected. The APK in Central and that found in the Droidium repo are not the same thing, so just paly along and check in this file (or script its download at build time :).
The @Deployment method is pretty simple; we just point to the APK of the app to test. Whereas the Droidium test checked that into source control, in my context, the file is built as part of the larger process, so I provide a relative path to the APK. Finally, in my test, all I have it doing here is clicking on the "Add Scout" menu. Interestingly, rather than specify the text of the menu, I look up the widget at runtime via its ID. As you should know, though, Android IDs are numeric, but I’m passing a string, so it seems that Selendroid is smart enough to take "menu_add_scout" and find R.id.menu_add_scout (which is probably just simple reflection, but still. That’s pretty cool. :). The Add Scout activity should show, the test waits 5 seconds so I can see that it actually did something, and then everything shuts down: the test, the emulator. Everything. I can manually start and stop the emulator if I want, in which case Droidium doesn’t shut it down, but, just like Arquillian can start and stop your app server instance for you, it can do the same for your Android emulator. And that’s pretty cool too.
This just scratches the surface, of course, as now Selendroid needs to be explored and understood, but that’s a different topic. Hopefully, what I’ve presented here will be enough to get you going with Droidium so you can quit worrying about emulator management and focus on writing tests, which is what Arquillian is all about. :)