"Turn" Android automated Test (uiautomator) Brief introduction

Source: Internet
Author: User
Tags gettext

One bug-raised issues

If there is a bug in the development process: "Constantly switching the language of the phone to display the phenomenon of flower screen." How do we verify this problem? I think the best way should be automated testing.
So what are the tasks that automated testing can accomplish?
Simply put, those repetitive testing work can be done by automating:
1, set the language of the phone
2. Add, delete, and bookmark contacts
3, dial-up, hang-off
4, even send text messages, collect text messages

If you need the above features, then start the journey of automation.

Second, Android Automation test Simple Introduction

Android automated testing is mainly divided into Monkeyrunner, Rubotium, Uiautomator, Monkey (in my opinion this does not count) and so on. Main Features:
1, Monkeyrunner: The advantages: The simplest operation, can record the test script, visual operation; Disadvantages: the main generating coordinates of the automated operation, the transplant is not strong, the most limited function;
2, Rubotium: Mainly for an apk for automated testing, APK can have the source code, also can not source code, powerful, the disadvantage is for the apk operation, and need to re-sign the APK (with tools), so the operation is relatively complex;
3, Uiautomator: The advantages: All operations can be automated, easy to operate; Disadvantage: Android version needs to be above 4.0, cannot operate according to the control ID, relative function is more limited, but also enough;
4, Monkey: Accurately speaking, this is not an automated test, because it can only produce random events, can not follow the established steps to operate;
From the above introduction can have this conclusion: Test an APK, you can choose Rubotium, the test process may involve multiple apk, choose Uiautomator; Some simple tests, choose Monkeyrunner;

This article mainly introduces how to use Uiautomator.

III. Environment Construction 3.1, necessary conditions:

1. JDK
2. SDK (API above 15)
3. Eclipse (Install ADT plugin)
4. ANT (for compiling build jar)

3.2. Brief steps:

1. Install the JDK and add environment variables.
After installation, it is important to add environment variables by Java_home, that is, to build the Java_home variable first, then add%java_home%\bin in Path;
2. Add the SDK environment variable.
Be sure to set up the Android_home first and then add the%android_home%\tools to the path;
3. Install Eclipse and install the ADT plugin.
4. Install Ant tools and add environment variables.

It is also important to establish the%ant_home% variable first and then add the%ant_home%\bin in path

Iv. detailed Operation 4.1, establishment of the project

Create a new Java Project with Eclipse, note that it's not Android project!

4.2. Add the JUnit Library

4.3. Add Android Library

Locate the path android-sdk\platforms\android-17\ below Android.jar and Uiautomator.jar add in:

All libraries should look like this when they are added:

4.4. Add the package in SRC and add the class file

The contents of the file are:

[Java]View Plaincopy
  1. Package com;
  2. Import Com.android.uiautomator.core.UiObject;
  3. Import com.android.uiautomator.core.UiObjectNotFoundException;
  4. Import com.android.uiautomator.core.UiScrollable;
  5. Import Com.android.uiautomator.core.UiSelector;
  6. Import Com.android.uiautomator.testrunner.UiAutomatorTestCase;
  7. Public class Runner extends Uiautomatortestcase {
  8. public void Testdemo () throws uiobjectnotfoundexception {
  9. Getuidevice (). Presshome ();
  10. //Enter the settings menu
  11. UiObject Settingapp = new UiObject (new Uiselector (). Text ("Settings"));
  12. Settingapp.click ();
  13. //Hibernate 3 seconds
  14. try {
  15. Thread.Sleep (3000);
  16. } catch (Interruptedexception E1) {
  17. //TODO auto-generated catch block
  18. E1.printstacktrace ();
  19. }
  20. //Enter language and Input method settings
  21. Uiscrollable settingitems = new Uiscrollable ( new Uiselector (). Scrollable (true));
  22. UiObject Languageandinputitem = Settingitems.getchildbytext (
  23. new Uiselector (). Text ("Language & Input"), "Language & Input", true);
  24. Languageandinputitem.clickandwaitfornewwindow ();
  25. }
  26. }

The above project path is in E:\workspace\AutoRunner, and the class full name is COM. Runner, as for the specific role we do not care now.

4.5. Find the SDK ID

CMD into the \android-sdk\tools\ directory, run the command:
Android List
View the ID value of the SDK with API greater than 15, which is currently 6;

4.6. Create a Build file

Still under the \android-sdk\tools\ directory, run the command:
Android Create Uitest-project-n <name>-T <android-sdk-ID>-P <path>
Like what:
Android Create Uitest-project-n autorunner-t 6-p E:\workspace\AutoRunner
The above name is the name of the jar package that will be generated in the future and can be defined by itself, Android-sdk-id is the path to Eclipse's new project as seen above, and the Build.xml file will be generated at the root of the project after the command is run. If not generated, check the steps above.

4.7, build Jar

CMD enters the project catalog and then runs the ant build, which will generate the jar using ant build, which will prompt you:

The jar file is then generated in the Bin directory.

4.8. Push and run the jar

ADB push <jar file path > data/local/tmp
adb shell uiautomator runtest <jar file name >-C < class name in project, including package name >
Like what:
ADB push E:\workspace\AutoRunner\bin\AutoRunner.jar data/local/tmp
adb shell uiautomator runtest autorunner.jar-c com. Runner

Then you can see that the phone will follow the steps in runner automatically. The effect is to go to the settings menu and then go to the language and input menu

Five, Code Analysis

We introduce some of the most important objects.

5.1. Uidevice Object

The Getuidevice () method can be used to get a Uidevice object that can perform some action on the device:
Click (int x, int y)
----Click in the pixel where (x, y) represents
Pressback ()
Pressdelete ()
Pressenter ()
Presshome ()
Pressmenu ()
Presssearch ()
----Click on the appropriate button
WakeUp ()
----When the phone is in the off-screen state, wake the screen and unlock it.
Swipe (StartX, Starty, EndX, EndY, steps)
----swipe on the phone, from (Startx,starty) to (Endx,endy). Steps indicates that the sliding distance is divided into several steps, the fewer the number, the greater the sliding amplitude.
Setorientationleft ()
Setorientationright ()
----Rotate the phone in the appropriate direction.
Setorientationnatural ()
----Turn the phone back to normal.

5.2. Uiselector Object

This object can be understood as a conditional object that describes a condition that is often used in conjunction with UIObject to obtain a certain (some) qualifying control object.
Checked (Boolean val)
----describes a relationship where the check state is Val.
ClassName (ClassName)
----Describes an object relationship with a class named ClassName
Clickable (Boolean Val)
----similar to checked, describing the relationship of the clickable state to Val
Description (DESC)
----Don't explain.
Descriptioncontains (DESC)
----Similar to description
Focusable (Boolean val)
----similar to checked
Index (Index)
----as described by the index of the current object in the parent object set
PackageName (String name)
----Use package name as condition description
Selected (Val)
----describe a selection relationship
Text (text)
----The most common kind of relationship, using the text on the control to find the current control, it is important to note that all controls found using the Text property must be in English. In other words, finding controls in Chinese is not supported!
Textcontains (text)
----is similar to text
Textstartswith (text)
----is similar to text

5.3. UIObject Object

This object can be understood as the object of the control. Generally a UIObject object can be obtained through the following form:
UiObject Mitem = new UiObject (new Uiselector (). Text ("中文版"));
That is, with a uiselector you can get a control.
Click ()
----Click Controls
Clickandwaitfornewwindow ()
----Click on a control and wait for the window to refresh
Longclick ()
----Long Press
Cleartextfield ()
----clear text, primarily for edit boxes
Getchildcount ()
----This method can be seen, in fact, UIObject can also be a collection of controls
Getpackagename ()
----Get the package name of the control
Getselector ()
----Get the selection criteria for the current control
GetText ()
----Get the text on the control
Ischeckable ()
IsChecked ()
Isclickable ()
Islongclickable ()
Isscrollable ()
Isscrollable ()
IsSelected ()
----Determine if you have a property

5.4. Uicollection Object

This object can be understood as a collection of objects. Because Uiselector is described, it is possible that there are more than one set of controls that satisfy the condition, so it can be used to generate Uicollection:
Uicollection muicollection = new Uicollection (new Uiselector (). Text ("Settings"));
Getchild (selector)
----Select a UIObject object from the collection again through Uiselector
Getchildbydescription (Childpattern, text)
----Select UIObject again with text from a matching pattern
Getchildbytext (Childpattern, text)
----is similar to the above.
Getchildcount ()
----Get the number of controls in the current collection

5.5. Uiscrollable Object

Uiscrollable can generate a scrolling action object, its biggest function is to implement scrolling to find an element. For example, in the "Settings" menu, "language and Input Method" This menu is lower, need to scroll before you can see (find), so the use of uiscrollable:

[Java]View Plaincopy
    1. Uiscrollable settingitems = new Uiscrollable ( new Uiselector (). Scrollable (true));
    2. UiObject Languageandinputitem = Settingitems.getchildbytext (
    3. New Uiselector (). Text ("Language & Input"), "Language & Input",
    4. true);

The above form allows you to scroll through the controls that display "Language & Input", which is the setting for "language and Input Method".

5.6. Wait for operation and add log method

If it is for a standard UIObject object, you can wait for an event after clicking on the Clickandwaitfornewwindow method, but if additional wait time is required, especially for Getuidevice (). Presshome ( This operation, which may take a long time to get more events for the next operation, can be done using the thread's Sleep method:

[Java]View Plaincopy
    1. Wait 3 seconds
    2. try {
    3. Thread.Sleep (3000);
    4. } catch (Interruptedexception E1) {
    5. E1.printstacktrace ();
    6. }

The method of adding log can also be implemented by Java standard println:
SYSTEM.OUT.PRINTLN ("This used to print some log!!!" + setlanitem.gettext ());
The above log will be printed through the CMD window when the jar is run.

六、一个 a relatively complete test case

The following is a relatively coherent test case to string the above knowledge point, the case is to do is to enter the system settings menu, and then select the "Language and input" menu, and then go to the "Language Settings" menu, and then click on the first item, the current language is set to "Simplified Chinese":

[Java]View Plaincopy
    1. Public void Setchineselan () throws uiobjectnotfoundexception {
    2. //Before entering the operation, first use the home button to enter the Standby interface
    3. Getuidevice (). Presshome ();
    4. //Enter the "System Settings" menu. You can also click the menu button to achieve
    5. UiObject Settingapp = new UiObject (new Uiselector (). Text ("Settings"));
    6. Settingapp.click ();
    7. //wait 3 seconds
    8. try {
    9. Thread.Sleep (3000);
    10. } catch (Interruptedexception E1) {
    11. E1.printstacktrace ();
    12. }
    13. //Scroll to find and go to the "Language and IME Settings" menu
    14. Uiscrollable settingitems = new Uiscrollable (
    15. new Uiselector (). Scrollable (true));
    16. UiObject Languageandinputitem = Settingitems.getchildbytext (
    17. new Uiselector (). Text ("Language & Input"), "Language & Input",
    18. true);
    19. Languageandinputitem.clickandwaitfornewwindow ();
    20. //Find clickable items for "Chinese" (because it is currently in English environment)
    21. UiObject Setlanitem = new UiObject (new Uiselector (). Text ("中文版"));
    22. Setlanitem.clickandwaitfornewwindow ();
    23. //log Output
    24. System.out.println ("setlanitem-->" + setlanitem.getpackagename ());
    25. //Because Chinese is not recognized, we use coordinates here to select "Simplified Chinese"
    26. Getuidevice (). Click ( 250);
    27. //Click the Back button to return to the standby screen
    28. Getuidevice (). Pressback ();
    29. Getuidevice (). Pressback ();
    30. Getuidevice (). Pressback ();

"Turn" Android automated Test (uiautomator) Brief introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.