Android app Development automated test framework UI Automator Use tutorial _android

Source: Internet
Author: User
Tags home screen


Android's automated testing has many frameworks, where UI Automator is the official black-box UI-related automated test tool (GitHub homepage: Case used in Java, and today we practiced the sample program in the official document, There are still some small problems to be summed up.
Environment Preparation:
1.JDK (yes, you are not mistaken, basic Android development environment necessary), as well as the corresponding environment variable configuration, will not be able to own Baidu under
2.Android Studio (IDE honors personal will)
3.android SDK and Configuration
4.ANT (mainly for build our script, generate jar package)
Ant's build is mainly in several steps:
1. Download ant installation files and unzip the installation;
2. New system environment variable ant_home, parameter value is your ant installation directory;
3. Add the ant installation directory's Bin folder to the PATH environment variable, like mine is C:\cod\apache-ant-1.9.6\bin.
4. After the configuration, test, enter ant-version at the command line, if display your installed ant version information, prove the environment variable configuration success



Working with processes
1, use ADT to create a Java project
Add JUnit to the Android.jar and Uiautomator.jar in the Android platforms you use when you create the project






2. Create a new package (I'll just call com here)
3, and then create a class under this package, enter the following Java code, the code is all the official document code, in addition to the top package


package com;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Runer extends UiAutomatorTestCase {

  public void testDemo () throws UiObjectNotFoundException {
   
   // Simulate a short press on the HOME button.
   getUiDevice (). pressHome ();
   
   // We're now in the home screen. Next, we want to simulate
   // a user bringing up the All Apps screen.
   // If you use the uiautomatorviewer tool to capture a snapshot
   // of the Home screen, notice that the All Apps button's
   // content-description property has the value “Apps”. We can
   // use this property to create a UiSelector to find the button.
   UiObject allAppsButton = new UiObject (new UiSelector ()
     .description ("Apps"));
   
   // Simulate a click to bring up the All Apps screen.
   allAppsButton.clickAndWaitForNewWindow ();
   
   // In the All Apps screen, the Settings app is located in
   // the Apps tab. To simulate the user bringing up the Apps tab,
   // we create a UiSelector to find a tab with the text
   // label "Apps".
   UiObject appsTab = new UiObject (new UiSelector ()
     .text ("Apps"));
   
   // Simulate a click to enter the Apps tab.
   appsTab.click ();

   // Next, in the apps tabs, we can simulate a user swiping until
   // they come to the Settings app icon. Since the container view
   // is scrollable, we can use a UiScrollable object.
   UiScrollable appViews = new UiScrollable (new UiSelector ()
     .scrollable (true));
   
   // Set the swiping mode to horizontal (the default is vertical)
   appViews.setAsHorizontalList ();
   
   // Create a UiSelector to find the Settings app and simulate
   // a user click to launch the app.
   UiObject settingsApp = appViews.getChildByText (new UiSelector ()
     .className (android.widget.TextView.class.getName ()),
     "Settings");
   settingsApp.clickAndWaitForNewWindow ();
   
   // Validate that the package name is the expected one
   UiObject settingsValidation = new UiObject (new UiSelector ()
     .packageName ("com.android.settings"));
   assertTrue ("Unable to detect Settings",
     settingsValidation.exists ());
   UiObject reportBug = new UiObject (new UiSelector (). Text ("Sound"));
   reportBug.clickAndWaitForNewWindow ();
   UiObject soundValidation = new UiObject (new UiSelector ()
   .text ("Volumes"));
  assertTrue ("Unable to detect Sound",
      soundValidation.exists ());
   
   getUiDevice (). pressHome ();
   
 }
} 
4. Use Ant tool to generate Build.xml
I'm here when I'm using ADT's own ant plugin.
Build.xml:26:class not found:javac1.8


Online check, is the plug-in and my Java environment does not match, download the latest ant plug-in can be http://ant.apache.org/bindownload.cgi






Download this tar.gz package, unzip it, and then add the Apache-ant-1.9.4\bin directory to the environment variable path
Then cmd to the tools directory of the Android SDK, use the Andrlid List command, and remember what you're going to run in the simulator (and the platforms you just imported Android.jar and Uiautomator.jar packages)
Use under CMD


Android Create Uitest-project-n <name>-T <android-sdk-ID>-P <path>


-N for the generated jar package name, as defined by itself,
-T for the values above, I'm here 1.
-P is the output path, and here is the path to the Java project you just created


Android Create Uitest-project-n autorunner-t 1-p D:\myAndroidStudy\androidTest


Then cmd into D:\myAndroidStudy\androidTest, using the ant build command to generate Autorunner.jar files
5. Push this Autorunner.jar file into the simulator


ADB push autorunner.jar/data/local/tmp


6. Use


 
 


Make the Runer class run






In my code, there's an official one-click on the "sound" operation and click the Home button


 UiObject reportbug = new UiObject (new Uiselector (). Text ("Sound"));
   Reportbug.clickandwaitfornewwindow ();
   UiObject soundvalidation = new UiObject (new Uiselector ()
   . Text ("volumes"));
  Asserttrue ("Unable to detect Sound", 
      soundvalidation.exists ()); 
   
   Getuidevice (). Presshome ();
 Image





This is actually just a simple toy code, it does not make any sense, but the official as a guide, which also uses some of the most common interface. Further study in the future Uiautomator



Summarize
Advantages:
1. All operations can be automated, easy to operate;
2. Do not need to be tested to sign the program, and, you can test all the equipment on the program, such as ~ an app, such as ~ dial, such as ~ send information and so on
3. For control positioning, a little bit simpler than robotium
Disadvantages:
1.uiautomator requires more than 16 of Android level to be available, because there are uiautomator tools in the API level 16 and above
2. If you want to use the Resource-id positioning control, you need level 18 and above to
3. Support for Chinese is not good (does not mean not support, Third-party jar can be implemented)
4. Personal feeling, the control positioning is not as clear as the robotium level, only personal feelings, user action injection or a bit of point area





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.