uiautomator代码例子--java
2024-09-01 12:55:42
在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
最新文章
- cocos2dx 3.x(移动修改精灵坐标MoveTo与MoveBy)
- 在thinkphp框架模板中引用session
- 【转】 C++中的new VS C语言中的malloc
- boost编译批处理脚本
- SQL备份(全)
- iOS 用CocoaPods做iOS程序的依赖管理
- Java对象序列化入门
- Hacker(八)----NET命令
- 【总结】OJ练习,进行的一些编程语言方面总结
- GameUnity 2.0 文档(二) 纸片人系统
- ASP.NET WebApi 使用Swagger生成接口文档
- 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
- 使用box-shadow进行画图(性能优化终结者)
- 【Java每日一题】20170220
- 【C++】C++中const与constexpr的比较
- redis mongodb mysql 三大数据库的更简单的批量操作。批量任务自动聚合器。
- 手机不弹toast解决方法
- Redis连接的客户端(connected_clients)数过高或者不减的问题解决方案
- Atitit 图像处理类库 halcon11  安装与环境搭建attilax总结
- java基础73 dom4j修改xml里面的内容(网页知识)
热门文章
- iOS - SceneKit 3D引擎初探
- RFC destination fails with error Incomplete Logon Data after system copy
- stm32 引脚映射 和 ADC
- mysql表设计注意点
- java,单文件和多文件上传代码范例
- 美化WebApi,使其统一返回Json格式
- 通过getResourceAsStream方法获取项目下的指定资源
- 个性化召回算法实践(二)——LFM算法
- Mybatis3.0_动态SQL元素:foreach-遍历集合_
- 新闻系统——VS制作自己的模板