单元测试 -- UIAutomator 2.0
来源:互联网 发布:gui图形界面编程 编辑:程序博客网 时间:2024/06/05 18:03
UI 自动化测试
听说可以模拟屏幕操作, 感觉挺有意思的, 有机会就学了一下;
// 今天试了下, 模拟点击屏幕, 可惜一秒只可以点击5~6次, (ノ ̄(エ) ̄)ノ
添加依赖
androidTestCompile 'com.android.support.test:runner:0.4' // Set this dependency to use JUnit 4 rules androidTestCompile 'com.android.support.test:rules:0.4' // Set this dependency to build and run Espresso tests androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' // Set this dependency to build and run UI Automator tests androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' //defaultConfig内添加 android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }
import android.content.Context;import android.support.test.InstrumentationRegistry;import android.support.test.runner.AndroidJUnit4;import android.support.test.uiautomator.By;import android.support.test.uiautomator.UiDevice;import android.support.test.uiautomator.UiObject2;import android.support.test.uiautomator.Until;import android.util.Log;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import static org.junit.Assert.assertNotNull;/** * Created by cold on 16/9/10. */@RunWith(AndroidJUnit4.class)//@RunWith别忘了public class UITest { private String TAG = "UITest"; private UiDevice uiDevice; private Context gContext; private Context currContext; private final long timeout = 2000; @Before public void init() { gContext = InstrumentationRegistry.getContext(); currContext = InstrumentationRegistry.getTargetContext(); uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); Log.w(TAG, "init"); } @Test public void testUI() { uiDevice.findObject(By.text("设置")).click();//从display区域中找到包含"设置"的控件, 点击 UiObject2 o1 = uiDevice.wait(Until.findObject(By.text("蓝牙")), timeout);//wait 等待 timeout之后在执行第一个参数 assertNotNull(o1); o1.click(); }}
运行的时候, 在该类上右击run’类名’
API
UiDevice
- pressBack() 模拟按下返回按钮
- pressHome() 模拟按下主菜单键
- sleep() 熄屏
- wakeUp() 唤醒屏幕
- isScreenOn() 检查屏幕是否开启
- getDisplayWidth() 获取display区域宽度
- getDisplayHeight() 获取display区域高度
- click(int x, int y) 点击指定坐标
- getCurrentPackageName() 获取当前应用包名
- getCurrentActivityName() 获取当前 Activity 名称
- findObject(BySelector selector) 通过 BySelector 找到 Object
- wait(SearchCondition condition, long timeout) 等待 timeout 后执行 condition
- swipe(int startX, int startY, int endX, int endY, int steps) 模拟滑动屏幕
- drag(int startX, int startY, int endX, int endY, int steps) 模拟拖动
BySelector
规则
- checkable(boolean isCheckable)
- checked(boolean isChecked)
- clazz(String packageName, String className)
- clazz(String className)
- depth(int min, int max)
- desc(String contentDescription)
- pkg(String applicationPackage)
- res(String resourcePackage, String resourceId)
By
By的方法都是static方法, 用来生成一个BySelector;
- desc(String contentDescription)
- pkg(Pattern applicationPackage)
- ……
InstrumentationRegistry
- getContext() 测试程序的Context
- getTargetContext() 目标程序的Context
UiObject2
- click() 模拟单击这个对象
- click(long duration) 点击时间
- clickAndWait(EventCondition condition, long timeout)
- findObject(BySelector selector)
- getChildCount() 获取子view数量
- getClassName()
- getParent()
- isCheckable()
- isClickable()
- isFocused()
- recycle() 回收这个对象
- wait(UiObject2Condition condition, long timeout)
Until
Until类里都是静态方法 返回 UiObject2Condition<> 对象
- clickable(boolean isClickable)
- focused(boolean isFocused)
- ………
指令
adb shell uiautomator dump 生成当前页面的xml格式ui层次描述至默认路径adb shell uiautomator dump [file] 生成当前页面的xml格式ui层次描述至指定路径adb shell pm list instrumentation 查看device上已存在的uiautomator
有关测试的指令
adb shell am instrument -w 无参数运行所有测试adb shell am instrument -w -e func true 有参数运行所有功能测试adb shell am instrument -w -e unit true 有参数运行所有单元测试adb shell am instrument -w -e class 运行一个独立的测试例子// demoRunning all tests: adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunnerRunning all small tests: adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunnerRunning all medium tests: adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunnerRunning all large tests: adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunnerFilter test run to tests with given annotation: adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunnerFilter test run to tests without given annotation: adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner// 运行单个用例, 用例信息通过"adb shell pm list instrumentation"查询Running a single testcase: adb shell am instrument -w -e class com.android.foo.FooTest com.android.foo/android.test.InstrumentationTestRunnerRunning a single test: adb shell am instrument -w -e class com.android.foo.FooTest#testFoo com.android.foo/android.test.InstrumentationTestRunnerRunning multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/android.test.InstrumentationTestRunnerRunning all tests in a java package: adb shell am instrument -w -e package com.android.foo.subpkg com.android.foo/android.test.InstrumentationTestRunnerIncluding performance tests: adb shell am instrument -w -e perf true com.android.foo/android.test.InstrumentationTestRunnerTo debug your tests, set a break point in your code and pass: -e debug trueTo run in 'log only' mode -e log true This option will load and iterate through all test classes and methods, but will bypass actual test execution. Useful for quickly obtaining info on the tests to be executed by an instrumentation command.To generate EMMA code coverage: -e coverage true Note: this requires an emma instrumented build. By default, the code coverage results file will be saved in a /data//coverage.ec file, unless overridden by coverageFile flag (see below)To specify EMMA code coverage results file path: -e coverageFile /sdcard/myFile.ec in addition to the other arguments.
我的个人博客
0 0
- 单元测试 -- UIAutomator 2.0
- uiautomator 之uiautomator 2.0 UiObject2学习收藏
- uiautomator 2.0 dependencies
- Uiautomator 2.0 VS 1.0
- uiautomator
- uiautomator
- uiautomator
- UIAutomator
- Uiautomator
- android_测试分析(monkey,monekyrunner,uiautomator,Robotium,单元测试,启动性能)cts测试
- Uiautomator 2.0之BySelector类学习小记
- Uiautomator 2.0之Until类学习小记
- Uiautomator 2.0之UiObject2类学习小记
- Uiautomator 2.0之Configrator类学习小记
- Uiautomator 2.0之UiWatcher类学习小记
- uiautomator 2.0 长按keycode物理按键
- 单元测试
- 单元测试
- RR 和RC隔离问题
- Java学习总结第二十八天Java泛型(一)
- hdu1874 畅通工程续 Bellman-Ford算法SPFA
- TCP协议中的三次握手和四次挥手
- 20个常备正则表达式
- 单元测试 -- UIAutomator 2.0
- 20. Valid Parentheses
- android 一个可以自定义的进度条控件
- 类与类的关系
- 基础机器学习算法
- js数组复制问题
- Java中类与类的关系
- 【OpenCV】基于OpenCV的双目视觉测试
- centos7+redmine+gitolite+nginx 安装步骤