UiAutomator
Added January 23, 2014
Translate
Share
Tweet
Share
I want to tell you about a wonderful tool to automatically test the UI of Android* applications.
The name of this tool is UiAutomator. You can find all the latest documentation
here: http://developer.android.com/tools/help/uiautomator/index.html and http://developer.androi
d.com/tools/testing/testing_ui.html
Advantages:
Events can be linked with Android UI controls. For example, click on a button with text
that says Ok, instead of clicking a coordinate location (x=450, y=550).
Can run several times and on different devices without changing any Java* code
Can use hardware buttons on devices
Disadvantages:
Hard to use with OpenGL* and HTML5 applications because these apps have no Android
UI components.
Script development
To introduce working with UiAutomator, I want to show a simple program. This program is the
standard Android Messaging application and sends SMS messages to any phone number.
1. Create a new Java project in Eclipse. We will call our project: SendMessage
2. Right click on your project in the Project Explorer and click on the Properties item
3. In Properties, select Java Build Path and add the required libraries:
Click on the Add Library > JUnit and there JUnit3 choose to add support for JUnit
Create a script
Create a project in a previously created new file with the Java class. Call it SendMessage. This
class is inherited from class UiAutomatorTestCase and using keys Ctrl + Shift + o (for Eclipse),
add the required libraries.
This function is simple. We press the Home button and once on the main window, open the menu
and look for the icon with the application. Click on it and start the application.
This function finds and presses the button for writing a new application, enters a phone number
for whom to send a text message to, and presses the send button. The phone number and text
pass through the function arguments:
To add the ability to pass parameters to the script we can specify the number of where we want to
send the message, as well as text messages. The function test () initializes the default settings,
and if any of the parameters were sent messages via the command line, the substitution of the
default settings would be:
01 // Default parameters
02 String toNumber = "123456";
03 String text = "Test message";
04
05 String toParam = getParams().getString("to");
06 String textParam = getParams().getString("text");
07 if (toParam != null) {
08 // Remove spaces
09 toNumber = toParam.trim();
10 }
11 if (textParam != null) {
12 text = textParam.trim();
13 }
Thus we will be able to pass parameters from the command line in the script using the small key
-e, the first name of the parameter, and the second value. For example, my application sends the
number to send " 777777 :-e to 777777
There are some pitfalls. For example, this application doesnt understand some characters and
fails. It is impossible to convey just text with some characters , it does not perceive them and
fails. Here are some of them: space, &, <, > , (,) , ", ' , as well as some Unicode characters. I
replace these characters when applying them to script a string, such as a space line :
blogspaceblog. So when the script starts UiAutomator, we use a script that will handle our input
parameters. We add the function test (), where we check whether there are options, parse
parameters, and replace them with real characters. Here is a sample code along with
demonstrating what we inserted earlier:
01 if (toParam != null) {
02 toParam = toParam.replace("blogspaceblog", " ");
03 toParam = toParam.replace("blogamperblog", "&");
04 toParam = toParam.replace("bloglessblog", "<");
05 toParam = toParam.replace("blogmoreblog", ">");
06 toParam = toParam.replace("blogopenbktblog", "(");
07 toParam = toParam.replace("blogclosebktblog", ")");
08 toParam = toParam.replace("blogonequoteblog", "'");
09 toParam = toParam.replace("blogtwicequoteblog", """);
10 toNumber = toParam.trim();
11 }
12 if (textParam != null) {
13 textParam = textParam.replace("blogspaceblog", " ");
14 textParam = textParam.replace("blogamperblog", "&");
15 textParam = textParam.replace("bloglessblog", "<");
16 textParam = textParam.replace("blogmoreblog", ">");
17 textParam = textParam.replace("blogopenbktblog", "(");
18 textParam = textParam.replace("blogclosebktblog", ")");
19 textParam = textParam.replace("blogonequoteblog", "'");
20 textParam = textParam.replace("blogtwicequoteblog", """);
21 text = textParam.trim();
22 }
Exit to the main menu of the application
This function is the simplest of all those that we have implemented. I simply press a button in a
loop back until it shows the button to create a new message.
o On Windows*:
set ANDROID_HOME=<path_to_your_sdk>
o On UNIX*:
export ANDROID_HOME=<path_to_your_sdk>
3. Go to the directory with the project file, build.xml, which was generated in step 1, and
run the command:
ant build
4. Copy the compiled JAR file to the device using the adb push:
adb push <path_to_output_jar> /data/local/tmp/
In our case, it is:
adb push <project_dir>/bin/SendMessage.jar /data/local/tmp/