在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. 【转载】C#指定文件夹下面的所有内容复制到目标文件夹下面

    在涉及到文件夹操作的过程中,有时候需要将文件夹下的所有内容复制拷贝到另一个文件夹,在C#的开发中有时候会遇到这个功能需求将指定文件夹下所有的内容复制到另一个文件夹,这个过程需要遍历所有的文件和目录.此 ...

  2. 【fiddler】fiddler基础

    一.浏览器设置 一般情况下,fiddler会自动修改IE浏览器的设置,捕捉到IE浏览器的搜有通讯.其他浏览器需要手动设置 如chrome 打开chrome->设置->高级->系统-& ...

  3. java List<String>的初始化 的一个小问题

    今天在处理生成excel的时候用到了java的list,但是需要直接赋值固定的几个变量,如果先初始化然后add的方法: List<String> name = new ArrayList( ...

  4. klia linux tools 使用方法整理

    第一部分  信息收集工具 1.Zenmap 和nmap 作用是一样的,只是使用的操作方式不一样, Zenmap使用 的GUI,nmap 是基于命令行的.在两个使用的命令是一样的. 使用SYN扫描  就 ...

  5. zabbix 3.2.2 server web展示如何显示中文 (三)

    1.确认zabbix是否开启了中文支持功能(/var/www/html/zabbix/include/locales.inc.php) 2.登录zabbix后,点击可爱的小公主吧 Admin(zabb ...

  6. Codeforces Round #510 (Div. 2) C. Array Product

    题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...

  7. 个性化召回算法实践(一)——CF算法

    协同过滤推荐(Collaborative Filtering Recommendation)主要包括基于用户的协同过滤算法与基于物品的协同过滤算法. 下面,以movielens数据集为例,分别实践这两 ...

  8. TListView控件的ReadOnly属性的一个Bug

    不知道是不是ListView的 ReadOnly属性的一个bug 1.Form上一个ListView,如图设置 2.在FormCreate事件中写如下代码:     ListView1->Rea ...

  9. python_反射:应用

    class User(object): def denglu(self): print('欢迎来到登录页面!') def zhuce(self): print('欢迎来到注册页面!') def you ...

  10. concat以及group_concat的用法

    concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null ...