UI Automation on Android (Windows Special ;) )
15/06/2014 21:31
2) Next let's get the GNU core-utils for Windows. A set of nifty little programs that you will be thankful you installed when you realise 'ls' doesn't work in windows and you have to use 'dir' instead. Found here : gnuwin32.sourceforge.net/packages/coreutils.htm
3) Lastly I do most of my editing in the console. So get vim for windows. Sigh.. or Emacs. Vim: www.vim.org/download.php
And we're done with the setup dawgs. Let's get automatin.
Writing an Automated UI Test Case for Android......on Windows
Hello guys, it's been a long time since I wrote one of these. Also I'm going to apologize in advance for posting screenshots of code. The website's editor doesn't have a code option. For your convenience I will also post links to pastebin containing all the code.
Why Windows you may ask. Most of the Android SDK is made to run on Linux anyway. Even the examples on the developer page cater to Linux or Mac users. The reason is simple. Because we can.
So before we get into the actual UI automation part let's get our environment up and running. A LOT of this exercise will be done from the console. So let's get rid of that unsightly cmd with a better console and let's pimp it out with the GNU core-utils package for windows.
At this point it must be noted that Cycgwin works just fine but operating outside of cygdrive is a pain. Also I'm writing this article on GVim and copy pasting to my website's text box. So excuse typos and poor indentation.
The rest of the article assumes you have the Android SDK + ADT bundle downloaded from the Android developers page and configured it.(while you're at it add the folders 'tools' and 'platform-tools' in the sdk directory to your computer's PATH variable)
1) My console of choice is this project called simply 'Console' (sourceforge.net/projects/console/). It really improves cmd and gives you the option to use tabs.
2) Next let's get the GNU core-utils for Windows. A set of nifty little programs that you will be thankful you installed when you realise 'ls' doesn't work in windows and you have to use 'dir' instead. Found here : gnuwin32.sourceforge.net/packages/coreutils.htm
3) Lastly I do most of my editing in the console. So get vim for windows. Sigh.. or Emacs. Vim: www.vim.org/download.php
And we're done with the setup dawgs. Let's get automatin.
First things first try to get your hands on an Android device. Automating the emulator is akin to playing games on SecondLife. Hook it up to your box using a standard issue data cable.
Now type 'adb' devices'. If nothing happens type'adb root' and then 'adb devices'.
If still nothing happens check if you've enabled development settings and ADB on your phone. This can be achieved by opening Settings, tapping on the build number until the phone displays the message 'You are now a developer'. Now the option 'Development Options' should be open to you.
Tap Development Options and enable 'ADB' and 'USB Debugging. Now try 'adb root' and 'adb devices' again. It should work now. If it still doesn't either your phone or your cable is fucked.
What gets displayed is you device's serial number.
Now that all that's out of the way, fire up Eclipse (also known as Android Developer Tools) from the adt bundle. Create a new Java Project. Name it anything you want. I have named mine "MyUITest".
Now right click on the project in project explorer and select properties. Now navigate to 'Java Build path" and click on the "Libraries" tab. Now select add library and click on "JUnit". Select "JUnit3" in the drop down menu. Now go to "Add external JARs" in the same window and select the "android" and "uiautomator" JAR files found in adt bundle folder>sdk>platforms>android<platform number>. Remember to add both. Now you're good to go.
Create a new class in the project and name it Class1. If you're unsure as to what code to write here I'll provide some sample code (at the end)which should work on most nexus devices and devices running stock Android UI.
If you use Samsung TouchWiz or some other UI you can figure it out yourself. It's not too complicated. Anyway now fire up your new pimped out console and type 'uiautomatorviewer'. Something should open up in a bit. If nothing does add the Android SDK to your system path. Anyhoo before we proceed if you are going to use my sample code scroll down and open it up in another tab now.
The second button from the left in the top left corner of the uiautomatorviewer window generates a screenshot of whatever is on your screen right now. You can manually select the UI components to view their properties. The app drawer button for example has the property text set to "Apps".
What my test suite does is open up the Calculator app. Add 7 and 1 and display the result as 8.
The line of code in my example corresponding to detecting and clicking on the app drawer is
"UiObject Applications = new UiObject(new UiSelector().text("Apps"));
Applications.clickAndWaitForNewWindow();"
UiObject is a class which can refer to any UI element on the screen. The other line is self explanatory. Now what you realize is that by using UiObject, UiSelector and various resource IDs which are obtained from uiautomatorviewer you can automate almost any UI related task.
Now let's get to the boring part of actually bundling the test and running it. In the console enter the command "android create uitest-project -n <intended name for test suite> -t <target, which can be discerned by typing android list targets> -p <path to the directory in your eclipse workspace which contains your java project>"
For me it was "android create uitest-project -n MyUITest2 -t 2 -p"D:\Android projects\MyUITest". A 'build.xml' file would have been created. Now open the directory in the terminal and use 'ant build' to build the JAR file.
The last step in the tutorial is type in "adb push bin\<name of JAR>.jar /data/". For me it was <adb push bin\MyUITest2.jar /data/>. Now all you have to do is run your UI Test Suite by entering the command "adb shell uiautomator runtest /data/<name of your JAR> -c Class1". For me it was <adb shell uiautomator runtest /data/MyUITest2.jar -c MyUITester>.
Watch the magic. Hope this worked for you.
pastebin link to sample code: pastebin.com/XcPvLAB8