UI 自动化测试
听说可以模拟屏幕操作, 感觉挺有意思的, 有机会就学了一下;
// 今天试了下, 模拟点击屏幕, 可惜一秒只可以点击5~6次, (ノ ̄(エ) ̄)ノ
添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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" } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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
- 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)
- ……
- getContext() 测试程序的Context
- getTargetContext() 目标程序的Context
- 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类里都是静态方法 返回 UiObject2Condition<> 对象
- clickable(boolean isClickable)
- focused(boolean isFocused)
- ………
指令
1 2 3 4 5 |
adb shell uiautomator dump 生成当前页面的xml格式ui层次描述至默认路径 adb shell uiautomator dump [file] 生成当前页面的xml格式ui层次描述至指定路径 adb shell pm list instrumentation 查看device上已存在的uiautomator |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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 运行一个独立的测试例子 // demo Running all tests: adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner Running all small tests: adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunner Running all medium tests: adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunner Running all large tests: adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunner Filter test run to tests with given annotation: adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner Filter 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.InstrumentationTestRunner Running a single test: adb shell am instrument -w -e class com.android.foo.FooTest#testFoo com.android.foo/android.test.InstrumentationTestRunner Running multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/android.test.InstrumentationTestRunner Running all tests in a java package: adb shell am instrument -w -e package com.android.foo.subpkg com.android.foo/android.test.InstrumentationTestRunner Including performance tests: adb shell am instrument -w -e perf true com.android.foo/android.test.InstrumentationTestRunner To debug your tests, set a break point in your code and pass: -e debug true To 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 Comments