"Uiautomator Study notes" 1. Environment configuration and compilation run

Source: Internet
Author: User

Overview
    • Uiautomator Compliance with JUNIT3 standards
Configuring the Authoring Environment
    1. Configuring the Java Development environment
      • JAVA_HOME:JDK root directory
      • Path: Add%java_home%\bin;%java_home%\jre\bin;
      • CLASSPATH:.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;
    2. Andrid Environment Configuration
      • ANDROID_HOME:SDK root directory
      • Path: Configure the Tools directory in the Android SDK (verified by Android List command) and the Platform-tools directory (successfully added via ADB command) to Path
    3. Ant Environment Configuration
      • Ant_home:ant root directory
      • Path: Configure the%ant_home%/bin directory to path (verify the Add success with the ANT command)
New Test Project
    1. New Java Project (note not Android engineering)
    2. Add Android.jar and Uiautomator.jar to the build Path
      • New Libs directory in the project to hold dependent packages
      • Copy the two files in the%ANDROID_HOME\%PLATFORMS\ANDROID-XX directory to the Libs directory
        • Android.jar
        • Uiautomator.jar
      • Add two jar packages to BuildPath
    3. New test class, inheriting uiautomatortestcase
    4. To write a use case, the method must begin with test. Such as:

       Public void Testdemo () {    uidevice.getinstance (). Presshome ();}
Compiling and running test code
  1. Create a Build file
    • Execution at the command line: Android create uitest-project-n-T 1-p
      • N: The name of the jar package that needs to be published, such as: Demo
      • T: The ID number of the SDK version that needs to be compiled, can be obtained by using the Android List command
      • P: Current Project root directory
    • After execution, refresh the project directory in Eclipse, you will find more Build.xml files
  2. Modify the Build file
    • Open Build.xml File
    • Modify <project name= "Demo" default= "Help" > To build
  3. Start compiling
    • Under the command line, go to the project directory and execute the ant command
    • When the compilation is complete, look at the result of the command line output in the jar package path, such as D:\android\workspace\Java_TestAndroid_UiAutomator\bin\demo.jar
  4. Push jar package to the phone
    • ADB push <jar_path>/data/local/tmp
    • For example: adb push D:\android\workspace\Java_TestAndroid_UiAutomator\bin\/data/local/tmp
  5. Run Tests

    • adb shell Uiautomator runtest <jars>-C < package name >.< class name >[#test name]

        • Jars: Specifies the name of the jar package to run, under/data/local/tmp
        • C <CLASSES>
          • Run all use case formats under a class: Package_name.class_ Name
          • tests specific methods: Package_name.class_name#method_name
          • can specify multiple C, which can be used to specify test case order
          • if not specified, runs the entire JAR package by default All use cases
        • nohup: Specify this parameter to disconnect the PC from running test Cases
        • e : Pass in a key-value pair to the test program, which can be used for incoming arguments that require changes

          • Add code that uses parameters in a test case, such as getting a mobile number

             public  void      Testdemo () {uidevice.getinstance (). Presshome ();    Bundle bundle  = Getparams ();    String Phone  = bundle.getstring ("Phone" );
               SYSTEM.OUT.PRINTLN ( Phone: "+ phone);}  
          • passed the-e parameter when executing: adb shell uiautomator runtest demo.jar-c com.test.test#testdemo-e phone 1380000000
    • For example: adb shell uiautomator runtest demo.jar-c com.test.Test
  6. You can then see the effect of the test case execution in the emulator
Running commands and quick commissioning
    • The steps described above cannot be debugged directly in eclipse
    • command debugging cumbersome and efficient under
    • Resolution: Convert the debugging steps to a script
    • Create build file, modify build file, start compilation, push file, run test
Implementation steps
    1. Open Https://github.com/fan2597/UiAutomatorHelper
    2. clone project to local, import to eclipse
    3. Copy Uiautomatorhelper.java to the test project
    4. Create a new main function in the test class, such as

       public  static  void   Main (string[] args) {String jarname  =" Demo "; //    jar Package name  String TestClass = "Com.test.Test"; //     test class full name  String testcasename = "Testdemo"; //     test case name     String Androidid = "1" ;  new   Uiautomatorhelper (Jarname,
       TestClass, Testcasename, Androidid);}  
    5. Run the Java project, and then

Uiautomator Learning Note 1. Environment configuration and compilation run

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.