UIAutomator环境搭建

1.1 必备条件

  1. 整理用到的安装包

    1.Eclipse是解压缩包:eclipse-jee-mars-2-win32-x86_64;

    2.Android-sdk是解压缩包:android-sdk_r24.0.2-windows.zip;

    3.Ant是解压缩包:apache-ant-1.9.7-bin.zip;

    4.JDK是安装包:jdk-7u10-windows-x64.exe;

    5.ADT插件是地址,需在线安装:https://dl-ssl.google.com/android/eclipse/(若出错,将https改为http);

    附:我的所有文件都放在D盘下的javadev下

本文转发 https://blog.csdn.net/gld824125233/article/details/52601510

https://www.cnblogs.com/peng-lan/p/5923350.html

验证android插件ADT是否安装成功:安装成功之后,菜单栏上会显示一排Android SDK Manager和Android Virtual Device Manager安卓图标

如果安装之后菜单栏没有出现图标,我们按下面步骤进行操作:

检查ADT是否正确安装:打开Window—>Preferences属性,如果看到了名为Android的内容,说明ok.
显示安卓图标:在Eclipse —>window—> Perpective —>Customize Perpective(定制视图)—>ActionSet Availability 然后把以andorid 开头全部选项就可以了.

5.配置ant环境变量:

在后续构建时,我们需要通过ant去build,从我分享链接中或从网上下载:apache-ant-1.9.7-bin.zip。直接解压到D盘javadev下即可;

(1)ANT_HOME:D:javadevapache-ant-1.9.7
(2)Path: D:javadevapache-ant-1.9.7in  切记使用这种才有用,可不用配置第一步

(3)classpath: D:javadevapache-ant-1.9.7lib
验证ant:开始->运行->cmd,输入如下命令:ant

说下启动uiautomator viewer 在目录F:1StudyAndriod51zxw_2018-0102Sdk ools  cmd输入uiautomatorviewer 即可动态加载手机的页面

如何使用篇

1、cmd进入sdk ools 目录下,运行命令:android list

查看API 大于15的SDK的ID值,当前是1;

2、

创建bulid文件

仍然在sdk ools目录下,运行命令:

android create uitest-project -n <name> -t <android-sdk-ID> -p <path>

其中name为将来生成的jar包的名字,可以自己定义,android-sdk-ID为上一步骤看到的2,path是新建工程的路径名称

F:1StudyAndriod51zxw_2018-0102Sdk ools>android create uitest-project -n Aut
oRunner -t 1 -p C:UsersAdministratoreclipse-workspaceuiautomator

报错 环境变量配置的SDK版本太高

解决办法:

解决方式:下载一个tools的旧包覆盖sdk目录:

 下载地址:http://pan.baidu.com/s/1geG21wB   无密码

比如我覆盖sdk目录为:

C:UsersAdministratorAppDataLocalAndroidsdk 下的tools文件夹
报新的错误 暂时未解决  将工程拷贝到D盘OK

   android create uitest-project -n AutoRunner -t 1 -p D:uiautomator

运行命令后,将会在工程的根目录下生成build.xml文件

 

2.7编译生成jar

cmd进入项目的工程目录,然后运行ant build,使用ant编译生成jar,执行如下:

在bin目录下生成rar文件

 

报错:

Unable to locate tools.jar. Expected to find it in D:Program Files (x86)javal
ib ools.jar

解决办法:

将Windows环境下 C:Program FilesJavajdk1.8.0_151lib 中的 tools.jar 拷贝到D:Program Files (x86)javal
ib ools.jar 中即可。

2.8 push并运行jar

adb push <jar文件路径> data/local/tmp

实际执行命令为

adb push D:adt-bundle-windows-x86_64-20140702eclipseworkspaceChpJavaTwoinAutoRunner.jar data/local/tmp

 

运行jar文件

adb shell uiautomator runtest <jar文件名> -c <包名.类名>

实际运行命令为

adb shell uiautomator runtest AutoRunner.jar -c testpackage.TestClass(注意不要写错包名和类名)

可以看到手机会按照Runner中的步骤自动执行。

/***写成一个bat文件 太繁琐了步骤  抛弃*****/

#f:
#cd F:1StudyAndriod51zxw_2018-0102Sdk ools


#android create uitest-project -n AutoRunner -t 1 -p D:uiautomator


d:
cd D:uiautomator
call ant build

adb push D:uiautomatorinAutoRunner.jar data/local/tmp


adb shell uiautomator runtest AutoRunner.jar -c testpackage.TestClass

cmd

 参考https://blog.csdn.net/actionwind/article/details/51407631

android studio版本 参考https://blog.csdn.net/daihuimaozideren/article/details/78331673 ;https://www.jianshu.com/p/5b84dd220a92

/*****2019-3-10 今天来android studio 操作 UIAutomator的环境布置******/

如何启动UIAutomatorViewer

cmd切换到 F:1StudyAndriod51zxw_2018-0102Sdk ools 目录 后 输入uiautomatorviewer即可查看手机布局控件

在此之前先普及一下uiautomator的常用API:

  • UiDevice:

    设备对象,通过UiDevice的getInstance(instrumentation)方法获取,可以通过UiDevice实例来检测设备的各种属性,比如获取屏幕的方向、尺寸等,还可以通过UiDevice实例来执行设备级别的操作,比如点击Home键、返回键等:

    // 点击Home键
    uiDevice.pressHome();
    
    // 点击返回键
    uiDevice.pressBack();
    
    ...
    
  • UiSelector

    用于获取某些符合条件的UI控件对象,可以通过资源id、描述等熟悉获取:

    // 通过资源id获取
    new UiSelector().resourceId("com.yang.designsupportdemo:id/CollapsingToolbarLayout");
    
    // 通过描述文件获取
    new UiSelector().description("Navigate up")
    
    // 通过className获取
    new UiSelector().className("android.support.v7.widget.RecyclerView")
    
    ...
    
  • UiObject

    代表一个UI控件,通过uiDevice的findObject(UiSelector)方法获取,获取到UiObject实例后,就可以对UI控件进行相关的操作,比如点击、长按等:

    // 点击应用返回按钮
    UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
    back.click();
    
  • UiCollection

    代表UI控件集合,相当于ViewGroup,比如界面中有多个CheckBox时,可以通过类名获取到当前界面下的所有CheckBox,然后通过控件id获取指定的CheckBox对象:

    // 获取指定的CheckBox对象
    UiCollection uiCollection = new UiCollection(new UiSelector().className("类名"));
    UiObject checkBox = uiCollection.getChild(new UiSelector().resourceId(""));
    
  • UiScrollable

    代表可滚动的控件,比如打开设置的关于手机选项:

    // 滑动列表到最后,点击About phone选项
    UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
    UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone");
    about.click();

【步骤1】新建一个Android工程 不需要创建Activity

【步骤2】配置gradle(app) //引入uiautomator androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.0'
【步骤3】创建TestCase 
  • 在src/androidTest/java目录下创建测试类
  • 【步骤4】运行,右键你的测试类,Run
  • 【补充】如果后期还需要运行测试用例,可以通过如下的adb命令调用

    adb shell am instrument -w -r   -e debug false -e class com.cxq.uiautomatordemo.UiTest com.cxq.uiautomatordemo.test/android.test.InstrumentationTestRunner

最后贴上我的代码  测试apk见上篇文章  appium环境搭建

---------------------------------------------------------------------


package com.example.uiautomator;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import junit.framework.TestCase;

/**
* Created by Administrator on 2019/3/10.
*/

public class UiTest extends TestCase {
private String mPackageName="com.example.android.contactmanager";

public void testA() throws UiObjectNotFoundException{
// 获取设备对象
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
UiDevice uiDevice = UiDevice.getInstance(instrumentation);
// 获取上下文
Context context = instrumentation.getContext();

// 启动测试App
Intent intent = context.getPackageManager().getLaunchIntentForPackage(mPackageName);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

// 验证当前显示 的应用包名为时钟

UiObject settingsValidation = uiDevice.findObject(new UiSelector().packageName(mPackageName));
// 如果不存在则出错提示
assertTrue("Unable to start app", settingsValidation.exists());

// // 打开CollapsingToolbarLayout
// String resourceId = "com.yang.designsupportdemo:id/CollapsingToolbarLayout";
// UiObject collapsingToolbarLayout = uiDevice.findObject(new UiSelector().resourceId(resourceId));
// collapsingToolbarLayout.click();

for (int i = 0; i < 5; i++) {
// // 向上移动
// uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(),
// uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, 10);

// 向下移动
uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2,
uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), 10);
}

//模拟点击点击ADD contact按钮,并等待界面起来
    UiObject addContact = uiDevice.findObject(new UiSelector().resourceId("com.example.android.contactmanager:id/addContactButton"));
    addContact.clickAndWaitForNewWindow();


// 点击应用返回按钮
// UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up"));
// back.click();

// 点击设备返回按钮
uiDevice.pressBack();
}

}

/**************************启动和关闭APP***************************************/
package com.example.uiautomator;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

/**
* Created by Administrator on 2019/3/10.
* 样例中,提供了两种APP启动方式,一种为Intent方式(无需知道LaunchActivityName),
* 一种为命令行方式
*/
@RunWith(AndroidJUnit4.class)
public class SalaryShowAppTest {
private final String TAG=getClass().getName();
private String mPackageName="com.example.android.contactmanager";
private String mPackageActivity="com.example.android.contactmanager";

@Test
public void case1(){
UiDevice mDevice=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());//获取设备用例

try {
if(!mDevice.isScreenOn()){
mDevice.wakeUp();//唤醒屏幕
}
} catch (RemoteException e) {
e.printStackTrace();
}
mDevice.pressHome(); //按home键

startAPP(mPackageName); //启动app
mDevice.waitForWindowUpdate(mPackageName, 5 * 2000);//等待app
closeAPP(mDevice,mPackageName);//关闭app
}

private void startAPP(String sPackageName){
Log.i(TAG, "openAPP: ");
Context mContext = InstrumentationRegistry.getContext();
//通过Intent启动app
Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(sPackageName);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(myIntent);
}

private void closeAPP(UiDevice uiDevice,String sPackageName){
Log.i(TAG, "closeAPP: ");
try {
uiDevice.executeShellCommand("am force-stop "+sPackageName);//通过命令行关闭app
} catch (IOException e) {
e.printStackTrace();
}
}

private void startAPP(UiDevice uiDevice,String sPackageName, String sLaunchActivity){
try {
uiDevice.executeShellCommand("am start -n "+sPackageName+"/"+sLaunchActivity);//通过命令行启动app
} catch (IOException e) {
e.printStackTrace();
}
}
}
原文地址:https://www.cnblogs.com/smartwen666/p/10502888.html