在androidtest下创建文件Ui2Test.java

package com.example.myapplication;

import android.app.Instrumentation;
import android.os.RemoteException;
import android.view.KeyEvent;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; import java.io.IOException; @RunWith(AndroidJUnit4ClassRunner.class)
public class Ui2Test {
public Instrumentation mInstrumentation;
public UiDevice mUidevice; @Before
public void setUp() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mUidevice = UiDevice.getInstance(mInstrumentation);
}
@Test
public void DemoTest() throws IOException, InterruptedException {
//资源id的形式
mUidevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
mUidevice.executeShellCommand("am start -n com.miui.calculator/.cal.CalculatorActivity");
Thread.sleep(1000);
mUidevice.findObject(By.res("com.miui.calculator:id/btn_5_s")).click();
//文本的方式
mUidevice.findObject(By.text("7")).click();
//通过描述
mUidevice.findObject(By.desc("减")).click();
mUidevice.executeShellCommand("am force-stop com.miui.calculator"); //关闭应用,只传入packagename就好了。 //可以通过链式同时选定多个条件
// mUidevice.findObject(By.res("").checked(false)).click();
// //焦点类
// mUidevice.findObject(By.res("").focused(true)).setText("1234"); }
// @Test
// public void calculatorTest() throws InterruptedException {
// //滑动解锁,10个步长为50ms。
// mUidevice.swipe(519,1505,519,306,10);
// mUidevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
// Thread.sleep(2000); //等待2s
// }
}

build.gradle的配置

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
} dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//UiAutomator Testing 添加测试依赖
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}

目前uiautomator生成的是apk文件 所以直接可以运行apk就好了

/Users/chenxiangan/AndroidStudioProjects/MyApplication/app/build/outputs/apk/debug/app-debug.apk
adb shell am instrument -w -r -e debug false -e class 'com.example.myapplication.Ui2Test#DemoTest' com.example.myapplication.test/androidx.test.runner.AndroidJUnitRunner

uiautomator代码例子--java的更多相关文章

  1. 编写高质量代码:改善Java程序的151个建议 --[117~128]

    编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...

  2. 编写高质量代码:改善Java程序的151个建议 --[78~92]

    编写高质量代码:改善Java程序的151个建议 --[78~92] HashMap中的hashCode应避免冲突 多线程使用Vector或HashTable Vector是ArrayList的多线程版 ...

  3. 编写高质量代码:改善Java程序的151个建议 --[65~78]

    编写高质量代码:改善Java程序的151个建议 --[65~78] 原始类型数组不能作为asList的输入参数,否则会引起程序逻辑混乱. public class Client65 { public ...

  4. 编写高质量代码:改善Java程序的151个建议 --[52~64]

    编写高质量代码:改善Java程序的151个建议 --[52~64] 推荐使用String直接量赋值 Java为了避免在一个系统中大量产生String对象(为什么会大量产生,因为String字符串是程序 ...

  5. Android C代码回调java方法

    本文将讲述下列三种C代码回调java方法 1.c代码回调java空方法 2.c代码回调java int类型参数方法 3.c代码回调javaString类型参数方法 方法都差不多,先看c代码回调java ...

  6. 30个php操作redis常用方法代码例子

    From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...

  7. 博友的 编写高质量代码 改善java程序的151个建议

    编写高质量代码 改善java程序的151个建议 http://www.cnblogs.com/selene/category/876189.html

  8. 一段代码了解Java中char和int的转换

    题目要求: 将输入的大写字母转成对应小写的后5个,如A转换后为f:如果转换后大于z则从a重新计,即多出1就转成a,多出2就转成b以此类推. Java代码: ```java private static ...

  9. 编写高质量代码改善java程序的151个建议——导航开篇

    2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...

随机推荐

  1. 安卓开发之cache 的使用(图片查看器案例)

    package com.lidaochen.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; i ...

  2. Fortify漏洞之Open Redirect(开放式重定向)

    继续对Fortify的漏洞进行总结,本篇主要针对  Open Redirect(开放式重定向)  的漏洞进行总结,如下: 1.1.产生原因: 通过重定向,Web 应用程序能够引导用户访问同一应用程序内 ...

  3. rabbitmq笔记(一)rabbitmq简介及基础

    一.消息组件 如果从消息组件来讲主要划分位两类: 1.JMS组件:ActiveMQ(慢): 2.AMQP组件(协议):性能是最高的, 而AMQP有两个主要的开源: 1)RabbitMQ:使用最广泛,速 ...

  4. nginx全局配置和性能优化

    nginx目录结构和命令 1.ls /apps/nginx/:         html是测试页,sbin是主程序 2.ls /apps/nginx/sbin/:  nginx 只有一个程序文件 3. ...

  5. 运输层8——TCP运输连接管理

    目录 1. TCP的连接建立 2. TCP的连接释放 写在前面:本文章是针对<计算机网络第七版>的学习笔记 运输层1--运输层协议概述 运输层2--用户数据报协议UDP 运输层3--传输控 ...

  6. ReLU激活函数

    参考:https://blog.csdn.net/cherrylvlei/article/details/53149381 首先,我们来看一下ReLU激活函数的形式,如下图: 单侧抑制,当模型增加N层 ...

  7. 神经网络(2)---neurons and the brain

    神经网络是一个历史悠久的课题,当初提出是为了让机器能够模仿人的大脑一样工作.现在神经网络在处理很多机器学习的问题上发挥了很重要的作用. 神经网络的背景(我们可以利用神经网络做些什么) 大脑可以处理视觉 ...

  8. Java8新特性--函数式编程

    在jdk8中什么是函数式接口: 1.被@FunctionalInterface注解修饰的. 2.接口里边只有一个非default的方法. 满足以上2个条件的即为函数式接口,ps:即使一个接口没有被@F ...

  9. Python基本的语法知识

    1. 编程语言的介绍 低级语言:机器语言--------计算机内部只能接受二进制代码,故用0或者1表示的指令称为机器指令,全部的机器指令构成了计算机的机器语言. 汇编语言--------实质上跟机器语 ...

  10. 洛谷P1346 电车【最短路】

    题目:https://www.luogu.org/problemnew/show/P1346 题意:n个路口,每个路口有好几条轨道,默认指向给出的第一个路口. 如果要换到另外的轨道去需要按一次开关.问 ...