Android Uiautomator Automated Testing

Source: Internet
Author: User

Description: The Uiautomator interface is rich and easy to use and supports all Android event operations, and event operations are not dependent on control coordinates, and can be well-suited for UI testing by asserting and validating correctness . Uiautomator does not require testers to understand the code implementation details, which are functional and black-box testing. The test code is simple in structure, easy to write and low in learning curve. Based on JAVA, a single compilation can run on all Android devices.

Usage Restrictions: Uiautomator is a jar package that contains a set of UI test APIs and supports running test programs . The jar package is located in Sdk/platforms/android-*/uiautomator.jar. Be aware that your SDK version needs to be greater than and SDK tools versions need to be larger than .

Uiautomator steps to use:

1 . In Eclipse, create a new Java project "Calculatorautotest";

2, right-click Java Project, build path, configuration build path, library---add library, select JUnit, Next, select junit4-> complete;

3 . Add the necessary jar packages: Right-click Java Project, build path, configuration build path, add external jar-> selection sdk/platforms/android-*/ Directory of Uiautomator.jar and Android.jar, the path of * Please use their own download the latest version;

4, the new class Caltest inherits Uiautomatortestcase in the Java project;

Code: The automated test code currently used for the project.

//Perform automated testingPublicvoid Testdemo ()throwsuiobjectnotfoundexception{//uiscrollable Appview = new Uiscrollable (new Uiselector (). ClassName ("Android.view.View"). Instance (0));//UiObject Openapp = Appview.getchildbytext (new Uiselector (). ClassName (Android.widget.TextView.class.getName ()) , "Biohop");//Openapp.clickandwaitfornewwindow (); while(true) {UiObject testlinearlayout=NewUiObject (Newuiselector (). ResourceId ("Com.android.test:id/maintesting"));        Testlinearlayout.click (); Sleep (1000); UiObject Testingtonamelayout=NewUiObject (NewUiselector (). ResourceId ("Com.android.test:id/testingtoname_go_linearlayout"));        Testingtonamelayout.click (); Sleep (1000); UiObject Qrlayout=NewUiObject (NewUiselector (). ResourceId ("com.android. Test:id/datamatrixsure_linearlayout "));        Qrlayout.click (); Sleep (28000); UiObject Testresultlayout=NewUiObject (NewUiselector (). ResourceId ("com.android. Test:id/testingtimecountsure_linearlayout "));        Testresultlayout.click (); Sleep (1000); UiObject Testresultdialog=NewUiObject (NewUiselector (). ResourceId ("Com.android.test:id/systemsetdialog_close"));        Testresultdialog.click (); Sleep (1000); }    }

5 . Go to cmd command line manager: Enter the directory where the Sdk/tools is located, type

-D:

CD Android\android-sdk-windows\tools

6 . To view all version information of the SDK in the current SDK, in the Tools directory, type:

Android list target

7, The purpose is to know the corresponding version of the ID number, and not only uiautomator need sdk4.1 or above version of support, and if you need to UI recognition through the UI control ID, then the selected SDK requires more than the API level 18 version , so I chose the SDK version of id=7 here.

8 . Create a build script that packs the test code. Generate a build script with the following command:

<android-sdk>/tools/android Create uitest-project-n <name> t 1-p <path>

Where:<name> is the project name that contains the test code,<path> is the project file path that contains the test code. The 1 after-T is the Android SDK version ID. In cmd, type:

-Android Create Uitest-project-n calculatorautotest-t 7-p D:\workspace\CalculatorAutoTest

9 . On the command line, go to the folder where the Calculatorautotest project is located and compile the build.xml using the Ant tool, and the ant compilation tool will need to install and configure the environment variables yourself

Ant Build

10, the following also need to copy the jar file to the/data/local/tmp/directory of the phone, copy operation can be achieved through the command:

Adbpush d:/workspace/calculatorautotest/bin/calculatorautotest.jar/data/local/tmp/

11, the last run JAR file: The following "Caltest" for the class Caltest, with the package name to the package name is also written on

adb shell Uiautomator runtest calculatorautotest.jar-c caltest

\

12.Uiautomator Code parsing website: http://techgogogo.com/2015/01/uiautomator%E5%AE%9A%E4%BD%8Dandroid%E6%8E%A7%E4% bb%b6%e7%9a%84%e6%96%b9%e6%b3%95%e5%ae%9e%e8%b7%b5%e5%92%8c%e5%bb%ba%e8%ae%aeappium%e5%a7%8a%e5%a6%b9%e7%af%87 /

Configuration Reference website: http://www.it165.net/pro/html/201409/22071.html

13, using the command: adb shell uiautomator runtest calculatorautotest.jar-c caltest to test the program, you must connect the USB computer test program to run. Disconnect the USB connection and test how it works: Use the command: adb shell uiautomator runtest calculatorautotest.jar--nohup-c caltest

Android Uiautomator Automated Testing

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.