开发者社区> 神巧合> 正文

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>


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Flutter Unable to locate Android SDK.
Flutter Unable to locate Android SDK.
146 0
Flutter build failed android:AAPT2 aapt2-3.2.1-4818971-osx
Flutter build failed android:AAPT2 aapt2-3.2.1-4818971-osx
35 0
安装使用Frida在Android上进行hook
我们对Android应用进行hook最常用的就是Xposed,它相对来说更加完善,而且有强大的社区和丰富的插件。而Frida则于Xposed不同,它是一款轻量级的Hook框架,可用于多平台,相同的是它依然需要root环境。本文就以Android为例来详细说说如何安装并使用它。
249 0
Android测试工具 UIAutomator入门与介绍(上)
  UI Automator 测试工具定义以及用途
111 0
Android测试工具 UIAutomator入门与介绍(下)
  UI Automator 测试工具定义以及用途
101 0
07.Android Studio下Ndk开发(使用fmod播放声音)
(创建于2017/12/28) 已实现 注意几点: 1.main下cpp文件夹放置c文件和相关头文件 2.main下JniLibs文件夹放置第三方so文件 3.
1198 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载