I.introduction of Uiautomator
The Android SDK provides the following tools to support automated functional interface testing:
1. Uiautomatorviewer: An image tool that scans and analyzes the UI components of the application under test.
2. Uiautomator: Java class Library that contains the engine that creates the custom functional interface Test API and automates the run of test cases.
Advantages: Can be automated for all operations, easy to operate;
Cons: Android version needs to be above 4.0.
Second,Uiautomator Common API to understand
The following is the most basic uiautomator most commonly used to three classes, here is simply to introduce the role of these three classes, follow-up I will also write a detailed description of the relevant essay.
1, Uidevice: Used to obtain the various state of the device.
Get device Information: screen resolution, optional status, light off screen ...
Operation: Key, coordinate operation, slide, drag 、......
Listener functions
2, Uiselector: The component is positioned through the various properties of the components and the node relationship.
Android Basics involved: Properties of Android components, Android component
3. UiObject: Represents a Component object with many methods and properties that simulate the actual operation of the phone.
Editing of text
Gesture manipulation
Get Child class
Determine if an object exists
......
Third, the use of uiautomator to achieve automation
(i) environmental construction
1. Configuring the Java Development environment
(1) Installing the JDK
(2) Configuring environment variables
installation directory for JAVA_HOME:JDK
Classpath:.%java_home%\lib;%java_home%\lib\tools.jar
Path:;%java_home%\bin;%java_home%\jre\bin
[Verify that the configuration is successful]cmd:java-version
2. Configure the Android environment
(1) Configuring Android_sdk_home environment variables
(2) Configure the path path
Path of the Android_sdk_home:android-sdk-windows
PATH:.; %android_sdk_home%\platform-tools;%android_sdk_home%\tools;
[Verify that the configuration is successful]CMD:ADB
3. Configuring the ANT Environment
(1) Configuring Ant_home environment variables
(2) Configure the path path
ant_home:c:\develop\apache-ant-1.9.4
Path:;%ant_home%\bin
[Verify that the configuration is successful]cmd:ant-version
4. Development tools
Eclipse (Don't use android-studio here, look down and know why.) )
(ii) creating test Cases
1. Eclipse Create Java Project, Project/libs/--> import Android.jar and Uiautomator.jar Toolkit, execute build path operation to import two toolkits into library.
Description: The path of two toolkits: \sdk\platforms\android-20.
2, create the test class in the project, inherit the Uiautomatortestcase class.
3, create a test method (function) in the class, here a test method is generally equivalent to a test case we are going to execute, we will use the relevant API of Uiautomator in this test method to implement the use case automation.
Because Uiautomator is using the JUNIT3 standard, the method name of the test case must be named after the Junit3 rule with the beginning of test.
e.g.
(iii) compilation and Operation
1. Implementation steps
Open Cmd.exe execute the following command to complete the compilation and operation of the automation code.
(1) Create a build file
>CD <project-workspace-path>
>android Create Uitest-project-n <jars>-T 1-p <workspace path>
Description
Only used to refer to the cmd command line
-N: Specifies the name of the jar package that is finally released by the current project.
-T: Compile the version of the SDK using the Targetid number in your PC, which can be queried via the Android List command. (There is a pit, general transplant code will step on, but step on the memory deeper, follow-up will have an essay description.) )
<project-workspace-path>: The working space path of the current project. You can view the project right/properties/resource/location by eclipse/.
(2) Modifying the build file
Enter the workspace path for the project, open the Build.xml file, and modify the second line of help to build.
(3) Run the build file, start compiling, build the jar package
>ant-buildfile <build.xml File path >
Description
When the compilation is complete, the DOS window will output the compile information: line 5th [jar]building jar:c:\...\ the jar name you specified. jar. The specified jar package is generated, and in the next step the jar package is imported to the phone to run the test case.
(4) Push file (incoming phone with the jar package)
>ADB Push <path_to_output_jar>/data/local/tmp/
(5) Run the test
>ADB Shell Uiautomator runtest <jars>--nohup-c < package name. Class Name >#[test Method]-E <NAME> <VALUE>
Description
Jars: The <jars> specified in step (1).
The package name where the-c:test method resides. class name; You can use multiple-C, such as-C test1-c test2-c test3 ... To specify the following test1-->test2-->test3 ... Sequential execution of test methods.
--nohup: Specify this parameter to disconnect the PC from running the test case.
-E: Pass in a key-value pair into the test program, which can be used for some parameters that need to be changed. (This parameter can be omitted and subsequently supplemented by its usage)
2, the Code implementation of automatic compilation and operation
Follow the above steps, it is not feel like to compile and run the code once is a very troublesome thing. Don ' t worry, here's how to automate the compilation and operation of code, freeing hands from manually executing commands.
(1) Execute cmd command
String cmd = "cmd/c + cmd command to execute";
Process p = runtime.getruntime (). exec (CMD); Yes, it's that simple.
(2) record the console output log (log is a very important thing, more useful than the screenshot recording site, to provide a more direct basis for the development of positioning problems. )
InputStream input = P.getinputstream (); Get the correct information output stream for process
InputStream errorinput = p.geterrorstring (); Get error message output stream for process
The goal of this article: a simple understanding of uiautomator, there is not much to wordy those conceptual things. Learn how to use Uiautomator to perform automated tests.
Finally, this article is relatively simple, the follow-up will continue to supplement, good sleepy, goodnight ~
Android Automation Uiautomator (i)