UiAutomator使用

简介: 安装Apache Ant 进入\Android-sdk\tools\目录 tools/android create uitest-project -n <name> -t 6 -p <path> -t后面是目标平台版本的id,可通过 android list targets查看    -p代表项目绝对路径 这里我的andorid.jar和uiautoma

安装Apache Ant
进入\Android-sdk\tools\目录

tools/android create uitest-project -n <name> -t 6 -p <path>

-t后面是目标平台版本的id,可通过 android list targets查看    -p代表项目绝对路径

这里我的andorid.jar和uiautomator.jar是从android-18导入的,所以选择对应的6

example:
android create uitest-project -n UiAutomator -t 6 -p D:\work\UiAutomator

工程目录下运行ant build命令生成在bin文件夹下生成UiAutomator.jar

adb push  C:\Users\Administrator\Desktop\UiAutomator.jar  /data/local/tmp/


运行测试脚本
adb shell uiautomator runtest UiAutomator.jar -c com.uia.example.my.Runner#method  (-e debug true开启远程debug)

或者安装个 "终端模拟器" (相当于linux终端)   登录后键入su获取root权限

执行uiautomator runtest UiAutomator.jar -c com.uia.example.my.Runner运行脚本


----------------UiAutomator打包第三方jar包和配置文件(文本文件)--------------------


build.xml配置如下



<?xml version="1.0" encoding="UTF-8"?>
<project name="UiAutomator" default="uidebug">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />
    
    <!-- 自定义类名变量 -->
 <property name="testcase" value="com.uia.example.my.RunnerN1"></property>
  <property name="configurationfile1" value="index.properties"></property>
  <property name="configurationfile2" value="publicnumber.txt"></property>
  <property name="configurationfile3" value="phoneId"></property>
  
    <!-- The ant.properties file can be created by you. It is only edited by the
         'android' tool to add properties to it.
         This is the place to change some Ant specific build properties.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         For other overridable properties, look at the beginning of the rules
         files in the SDK, at tools/ant/build.xml

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="ant.properties" />

    <!-- if sdk.dir was not set from one of the property file, then
         get it from the ANDROID_HOME env var.
         This must be done before we load project.properties since
         the proguard config can use sdk.dir -->
    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <!-- The project.properties file is created and updated by the 'android'
         tool, as well as ADT.

         This contains project specific properties such as project target, and library
         dependencies. Lower level build properties are stored in ant.properties
         (or in .classpath for Eclipse projects).

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <loadproperties srcFile="project.properties" />

    <!-- quick check on sdk.dir -->
    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <!--
        Import per project custom build rules if present at the root of the project.
        This is the place to put custom intermediary targets such as:
            -pre-build
            -pre-compile
            -post-compile (This is typically used for code obfuscation.
                           Compiled code location: ${out.classes.absolute.dir}
                           If this is not done in place, override ${out.dex.input.absolute.dir})
            -post-package
            -post-build
            -pre-clean
    -->
    <import file="custom_rules.xml" optional="true" />

    <!-- Import the actual build file.

         To customize existing targets, there are two options:
         - Customize only one target:
             - copy/paste the target into this file, *before* the
               <import> task.
             - customize it to your needs.
         - Customize the whole content of build.xml
             - copy/paste the content of the rules files (minus the top node)
               into this file, replacing the <import> task.
             - customize to your needs.

         ***********************
         ****** IMPORTANT ******
         ***********************
         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
         in order to avoid having your file be overridden by tools such as "android update project"
    -->
    <!-- version-tag: VERSION_TAG -->
    <import file="${sdk.dir}/tools/ant/uibuild.xml" />

</project>


项目libs文件夹存放第三方jar包
修改  sdk\tools\ant\uibuild.xml文件
uibuild.xml文件配置如下

<?xml version="1.0" encoding="UTF-8"?>
<project name="android_rules" default="debug">

    <!--
        This build file is imported by the project build file. It contains
        all the targets and tasks necessary to build Android projects, be they
        regular projects, library projects, or test projects.

        At the beginning of the file is a list of properties that can be overridden
        by adding them to your ant.properties (properties are immutable, so their
        first definition sticks and is never changed).

        Follows:
        - custom task definitions,
        - more properties (do not override those unless the whole build system is modified).
        - macros used throughout the build,
        - base build targets,
        - debug-specific build targets,
        - release-specific build targets,
        - instrument-specific build targets,
        - test project-specific build targets,
        - install targets,
        - help target
    -->

    <!-- ******************************************************* -->
    <!-- **************** Overridable Properties *************** -->
    <!-- ******************************************************* -->

    <!-- You can override these values in your build.xml or ant.properties.
         Overriding any other properties may result in broken build. -->

    <!-- Tells adb which device to target. You can change this from the command line
         by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
         the emulator. -->
    <property name="adb.device.arg" value="" />

    <!-- filename only of the output file. Cannot be a path -->
    <property name="out.filename" value="${ant.project.name}.jar" />

    <!-- compilation options -->
    <property name="java.encoding" value="UTF-8" />
    <property name="java.target" value="1.5" />
    <property name="java.source" value="1.5" />
    <property name="java.compilerargs" value="" />

    <!-- Verbosity -->
    <property name="verbose" value="false" />

    <!-- ******************************************************* -->
    <!-- ********************* Custom Tasks ******************** -->
    <!-- ******************************************************* -->

    <!-- jar file from where the tasks are loaded -->
    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/ant-tasks.jar" />
    </path>

    <!-- Custom tasks -->
    <taskdef resource="anttasks.properties" classpathref="android.antlibs" />

    <!-- Emma configuration -->
    <property name="emma.dir" value="${sdk.dir}/tools/lib" />
    <path id="emma.lib">
        <pathelement location="${emma.dir}/emma.jar" />
        <pathelement location="${emma.dir}/emma_ant.jar" />
    </path>
    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
    <!-- End of emma configuration -->


    <!-- ******************************************************* -->
    <!-- ******************* Other Properties ****************** -->
    <!-- ******************************************************* -->
    <!-- overriding these properties may break the build
         unless the whole file is updated -->

    <!-- Input directories -->
    <property name="source.dir" value="src" />
    <property name="source.absolute.dir" location="${source.dir}" />
    <property name="jar.libs.dir" value="libs" />
    <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />

    <!-- Output directories -->
    <property name="out.dir" value="bin" />
    <property name="out.absolute.dir" location="${out.dir}" />
    <property name="out.classes.absolute.dir" location="${out.dir}/classes" />

    <property name="out.file" value="${out.absolute.dir}/${out.filename}" />

    <!-- tools location -->
    <property name="android.tools.dir" location="${sdk.dir}/tools" />
    <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />
    <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
    <condition property="bat" value=".bat" else=""><os family="windows" /></condition>
    <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />

    <!-- Intermediate files -->
    <property name="dex.file.name" value="classes.dex" />
    <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
    <property name="resource.package.file.name" value="${ant.project.name}.ap_" />

    <!-- whether we need to fork javac.
         This is only needed on Windows when running Java < 7 -->
    <condition else="false" property="need.javac.fork">
        <and>
            <matches pattern="1\.[56]" string="${java.specification.version}"/>
            <not>
                <os family="unix"/>
            </not>
        </and>
    </condition>

    <macrodef name="run-tests-helper">
        <attribute name="emma.enabled" default="false" />
        <element name="extra-instrument-args" optional="yes" />
        <sequential>
            <echo level="info">Running tests ...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg line="${adb.device.arg}" />
                <arg value="shell" />
                <arg value="am" />
                <arg value="instrument" />
                <arg value="-w" />
                <arg value="-e" />
                <arg value="coverage" />
                <arg value="@{emma.enabled}" />
                <extra-instrument-args />
                <arg value="${project.app.package}/${test.runner}" />
            </exec>
        </sequential>
    </macrodef>

    <!-- ******************************************************* -->
    <!-- ******************** Build Targets ******************** -->
    <!-- ******************************************************* -->

    <!-- Basic Ant + SDK check -->
    <target name="-check-env">
        <checkenv />
    </target>

    <!-- empty default pre-clean target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-pre-clean"/>

    <!-- clean target -->
    <target name="clean" depends="-check-env, -pre-clean"
            description="Removes output files created by other targets.">
        <delete dir="${out.absolute.dir}" verbose="${verbose}" />
    </target>

    <!-- Pre build setup -->
    <target name="-build-setup" depends="-check-env">
        <getbuildtools name="android.build.tools.dir" />
        <property name="dx" location="${android.build.tools.dir}/dx${bat}" />

        <echo level="info">Resolving Build Target for ${ant.project.name}...</echo>
        <!-- load project properties, resolve Android target, library dependencies
             and set some properties with the results.
             All property names are passed as parameters ending in -Out -->
        <getuitarget compileClassPathOut="project.target.class.path" />

        <echo level="info">----------</echo>
        <echo level="info">Creating output directories if needed...</echo>
        <mkdir dir="${out.absolute.dir}" />
        <mkdir dir="${out.classes.absolute.dir}" />

    </target>

    <!-- empty default pre-compile target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-pre-compile"/>

    <!-- Compiles this project's .java files into .class files. -->
    <target name="compile" depends="-build-setup, -pre-compile">
        <javac encoding="${java.encoding}"
                source="${java.source}" target="${java.target}"
                debug="true" extdirs="" includeantruntime="false"
                destdir="${out.classes.absolute.dir}"
                bootclasspathref="project.target.class.path"
                verbose="${verbose}"
                fork="${need.javac.fork}">
            <src path="${source.absolute.dir}" />
            <compilerarg line="${java.compilerargs}" />

             <classpath >
                <fileset dir="${jar.libs.dir}" includes="*.jar" />
            </classpath>
        </javac>
        
      <!--复制配置文件 -->  
        <copy todir="${out.absolute.dir}">
            <fileset dir="${source.absolute.dir}">
              <include name="${configurationfile1}" />
             <include name="${configurationfile2}" />
              <include name="${configurationfile3}" />
            </fileset>
        </copy>
        
    </target>

    <!-- empty default post-compile target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-post-compile"/>

    <!-- Converts this project's .class files into .dex files -->
    <target name="-dex" depends="compile, -post-compile">
        <dex executable="${dx}"
                output="${intermediate.dex.file}"
                nolocals="@{nolocals}"
                verbose="${verbose}">

                <fileset dir="${jar.libs.dir}">
                <include name="*.jar" />
                </fileset>
            <path path="${out.classes.absolute.dir}"/>
        </dex>
    </target>

	
    
    
    <!-- empty default post-dex target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-post-dex"/>

    <target name="-jar" depends="-dex, -post-dex" >
        <jar destfile="${out.file}">
            <fileset file="${intermediate.dex.file}" />
            <fileset file="${out.absolute.dir}/${configurationfile1}" />
            <fileset file="${out.absolute.dir}/${configurationfile2}" />
              <fileset file="${out.absolute.dir}/${configurationfile3}" />
        </jar>
    </target>

    <!-- empty default post-jar target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-post-jar"/>

    <target name="build" depends="-jar, -post-jar" />

    <target name="install" description="Install the test package">
         <exec executable="${adb}" failonerror="true">
            <arg line="${adb.device.arg}" />
            <arg value="push" />
            <arg value="${out.file}" />
            <arg value="/data/local/tmp" />
        </exec>
    </target>

    <target name="test" description="Runs tests">
        <!-- todo: fix this -->
      <!--   <fail message="Launching tests from Ant not supported yet" /> -->

         <exec executable="${adb}" failonerror="true">
            <arg line="${adb.device.arg}" />
            <arg value="shell" />
            <arg value="uiautomator" />
            <arg value="runtest" />
            <arg value="${out.filename}" />
            <arg value="-e" />
            <arg value="class" />
            <arg value="${testcase}" />
                
         <!--    <arg value="-e" />
            <arg value="debug" />
            <arg value="true" /> -->
        </exec>
    </target>

    <!-- 自定义target -->
    <target name="uidebug" depends="clean,build,install,test">
        
        
    </target>
    <target name="help">
        <!-- displays starts at col 13
              |13                                                              80| -->
        <echo>Android Ant Build. Available targets:</echo>
        <echo>   help:      Displays this help.</echo>
        <echo>   clean:     Removes output files created by other targets.</echo>
        <echo>   build:     Builds the test library.</echo>
        <echo>   install:   Installs the library on a connected device or</echo>
        <echo>              emulator.</echo>
        <echo>   test:      Runs the tests.</echo>
        <echo></echo>
        <echo>It is possible to mix targets. For instance:</echo>
        <echo>   ant build install test</echo>
        <echo>This will build, install and run the test in a single command.</echo>
    </target>

</project>


相关实践学习
使用CLup和iSCSI共享盘快速体验PolarDB for PostgtreSQL
在Clup云管控平台中快速体验创建与管理在iSCSI共享盘上的PolarDB for PostgtreSQL。
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
2月前
|
安全 开发工具 Android开发
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
65 0
|
8月前
|
开发工具 Android开发
06-appium-uiautomatorviewer使用-Android
06-appium-uiautomatorviewer使用-Android
|
开发工具 Android开发
Flutter Unable to locate Android SDK.
Flutter Unable to locate Android SDK.
282 0
Flutter Unable to locate Android SDK.
|
JavaScript Shell Android开发
安装使用Frida在Android上进行hook
我们对Android应用进行hook最常用的就是Xposed,它相对来说更加完善,而且有强大的社区和丰富的插件。而Frida则于Xposed不同,它是一款轻量级的Hook框架,可用于多平台,相同的是它依然需要root环境。本文就以Android为例来详细说说如何安装并使用它。
470 0
|
Android开发 C++ Java
07.Android Studio下Ndk开发(使用fmod播放声音)
(创建于2017/12/28) 已实现 注意几点: 1.main下cpp文件夹放置c文件和相关头文件 2.main下JniLibs文件夹放置第三方so文件 3.
1265 0
|
测试技术 开发工具 Android开发