我是靠谱客的博主 感性咖啡,最近开发中收集的这篇文章主要介绍uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
学习收藏
参考链接:
http://blog.csdn.net/swordgirl2011/article/details/50993157
1. 基础动作
1.1. 相关API介绍
API | 说明 |
clear() | 清楚编辑框内的内容 |
click() | 点击一个对象 |
clickAndWait(EventCondition<R> condition, long timeout) | 点击一个对象然后等待在超时的时间内条件满足则通过,否则抛出异常 |
drag(Point dest, int speed) | 自定义速度拖拽这个对象到指定位置 |
drag(Point dest) | 拖拽这个对象到指定位置 |
longClick() | 长按某个对象 |
scroll(Direction direction, float percent) | 对该对象执行一个滚动操作 |
scroll(Direction direction, float percent, int speed) | 自定义速度,对该对象执行一个滚动操作 |
setText(String text) | 设置文本内容 |
legacySetText(String text) | 通过发送keycode,设置文本内容 |
1.2 简单示例
- @Test
- public void testCase05() throws InterruptedException {
- /**
- * 发现UiObject2中的setText()方法还是无法直接输入中文,需要借助外部方法库.
- */
- UiObject2 editText = mDevice.findObject(By.clazz(EditText.class));
- editText.setText(Utf7ImeHelper.e("测试"));
- UiObject2 appIcon = mDevice.findObject(By.text("联系人"));
- Point desPoint = new Point();
- desPoint.x = 654;
- desPoint.y = 1066;
- appIcon.drag(desPoint, 2000);
- UiObject2 appBtn = mDevice.findObject(By.text("联系人"));
- appBtn.longClick();
- UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
- listView.scroll(Direction.DOWN, 0.8f, 3000);
- UiObject2 smsBtn = mDevice.findObject(By.text("短信"));
- smsBtn.clickAndWait(Until.newWindow(), 2000);
- }
2. 手势动作模拟
2.1 相关API
API | 说明 |
pinchClose(float percent, int speed) | 自定义速度执行收缩手势 |
pinchClose(float percent) | 执行收缩手势 |
pinchOpen(float percent, int speed) | 自定义速度执行展开手势 |
pinchOpen(float percent) | 执行展开手势 |
fling(Direction direction) | 执行一个扫动手势,Direction代表为起点方向 |
fling(Direction direction, int speed) | 自定义速度,执行一个扫动手势 |
swipe(Direction direction, float percent, int speed) | 执行一个滑动操作,可自定义滑动距离和速度 |
swipe(Direction direction, float percent) | 执行一个滑动操作 |
setGestureMargin(int margin) | 以像素为单位,设置手势边缘 |
setGestureMargins(int left, int top, int right, int bottom) | 以像素为单位,设置手势边缘 |
2.2 简单示例
- @Test
- public void testCase06() throws InterruptedException {
- UiObject2 pic = mDevice.findObject(By.res("com.miui.gallery:id/single_view_other"));
- pic.pinchOpen(0.8f, 2000);
- Thread.sleep(1000);
- pic.pinchClose(0.8f, 2000);
- UiObject2 contactList = mDevice.findObject(By.res("android:id/list"));
- contactList.fling(Direction.DOWN);
- Thread.sleep(1000);
- contactList.fling(Direction.UP,3000);
- UiObject2 listView = mDevice.findObject(By.res("android:id/list"));
- listView.swipe(Direction.UP,0.5f, 3000);
- Thread.sleep(1500);
- listView.setGestureMargin(100);
- listView.swipe(Direction.DOWN,0.5f, 3000);
- }
3.1相关API
API | 说明 |
findObject(BySelector selector) | 搜索在这个对象之下的所有元素,并返回第一个与搜索条件匹配的 |
findObjects(BySelector selector) | 搜索在这个对象之下的所有元素,并返回所有与搜索条件匹配的 |
getChildCount() | 返回这个对象直属子元素的数量 |
getChildren() | 返回这个对象下的直接子元素的集合 |
getParent() | 返回该对象的父类 |
equals(Object object) | 比较两个对象是否相等 |
hashCode() | 获取对象的哈希码 |
hasObject(BySelector selector) | 返回该对象是否存在 |
recycle() | 回收该对象 |
wait(UiObject2Condition<R> condition, long timeout) | 等待条件被满足 |
wait(SearchCondition<R> condition, long timeout) | 等待条件被满足 |
3.2 简单示例
- @Test
- public void testCase07(){
- UiObject2 list = mDevice.findObject(By.res("android:id/list"));
- UiObject2 child = list.findObject(By.clazz(TextView.class));
- Log.i("testCase07", child.getText());
- List<UiObject2> lisCollect = mDevice.findObjects(By.clazz(TextView.class));
- int count = lisCollect.size();
- Log.i("testCase07", String.valueOf(count));
- for (UiObject2 obj:lisCollect) {
- Log.i("testCase07", obj.getText());
- }
- List<UiObject2> childList = list.getChildren();
- int childCount = childList.size();
- Log.i("testCase07", String.valueOf(childCount));
- for (UiObject2 obj:childList) {
- Log.i("testCase07", obj.getText());
- }
- UiObject2 childElement = mDevice.findObject(By.text("联系人"));
- childElement.getParent().click();
- }
参考链接:
http://blog.csdn.net/mqqzt/article/details/49981207
基础动作模拟API
返回 | API | 说明 |
---|---|---|
void | clear() | 清除编辑框中的内容 |
void | click() | 点击一个对象 |
<R> R | clickAndWait(EventCondition<R> condition, long timeout) | 点击一个对象,然后等待在超时时间内条件成立则通过,否则抛出异常 |
void | drag(Point dest, int speed) | 自定义速度拖拽一个对象到指定位置,速度:像素/秒 |
void | drag(Point dest) | 拖拽一个对象到指定位置 |
void | longClick() | 长时间点击对象 |
boolean | scroll(Direction direction, float percent) | 滚动操作 |
boolean | scroll(Direction direction, float percent, int speed) | 自定义速度的滚动操作 |
void | setText(String text) | 设置文本内容 |
手势动作API
返回 | API | 说明 |
---|---|---|
void | pinchClose(float percent, int speed) | 自定义速度关闭手势 |
void | pinchOpen(float percent, int speed) | 自定义速度打开手势 |
void | pinchOpen(float percent) | 打开手势 |
boolean | fling(Direction direction) | 滑动手势 |
boolean | fling(Direction direction, int speed) | 自定义速度滑动手势 |
void | swipe(Direction direction, float percent, int speed) | 自定义速度滑动手势 |
void | swipe(Direction direction, float percent) | 自滑动手势 |
组件属性API
返回 | API | 说明 |
---|---|---|
String | getApplicationPackage() | 返回应用包名 |
String | getClassName() | 返回对象类名 |
String | getContentDescription() | 返回内容描述 |
String | getResourceName() | 返回资源id |
String | getText() | 返回文本 |
Rect | getVisibleBounds() | 返回对象可见范围内的屏幕坐标 |
Point | getVisibleCenter() | 返回可见范围的中心 |
boolean | isCheckable() | 返回Checkable属性 |
boolean | isChecked() | 返回Checked属性 |
boolean | isClickable() | 返回Clickable属性 |
boolean | isEnabled() | 返回Enabled属性 |
boolean | isFocusable() | 返回Focusable属性 |
boolean | isFocused() | 返回isFocused属性 |
boolean | isLongClickable() | 返回LongClickable属性 |
boolean | isScrollable() | 返回Scrollable属性 |
boolean | isSelected() | 返回Selected属性 |
层级关系API
返回 | API | 说明 |
---|---|---|
UiObject2 | findObject(BySelector selector) | 在该对象层级之下,返回第一个与条件匹配的对象 |
List<UiObject2> | findObjects(BySelector selector) | 在该对象层级之下,返回所有匹配的对象 |
List<UiObject2> | getChildren() | 返回该对象的所有子元素的集合 |
UiObject2 | getParent() | 返回该对象的父元素 |
int | getChildCount() | 返回该对象的直接子元素的数量 |
条件判断API
返回 | API | 说明 |
---|---|---|
boolean | equals(Object object) | 比较两个对象是否相等 |
int | hashCode() | 获取对象的hashCode |
boolean | hasObject(BySelector selector) | 返回对象是否存在 |
<R> R | wait(UiObject2Condition<R> condition, long timeout) | 等待的条件得到满足 |
<R> R | wait(SearchCondition<R> condition, long timeout) | 等待的条件得到满足 |
示例代码:
<code class="hljs avrasm has-numbering">package mqqzt<span class="hljs-preprocessor">.myuitest</span>5<span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.app</span><span class="hljs-preprocessor">.Instrumentation</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.graphics</span><span class="hljs-preprocessor">.Point</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.InstrumentationRegistry</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.runner</span><span class="hljs-preprocessor">.AndroidJUnit</span>4<span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.By</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.Direction</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.UiDevice</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.UiObject</span>2<span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.support</span><span class="hljs-preprocessor">.test</span><span class="hljs-preprocessor">.uiautomator</span><span class="hljs-preprocessor">.Until</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.Log</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.widget</span><span class="hljs-preprocessor">.ListView</span><span class="hljs-comment">;</span> import android<span class="hljs-preprocessor">.widget</span><span class="hljs-preprocessor">.TextView</span><span class="hljs-comment">;</span> import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.Before</span><span class="hljs-comment">;</span> import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.Test</span><span class="hljs-comment">;</span> import org<span class="hljs-preprocessor">.junit</span><span class="hljs-preprocessor">.runner</span><span class="hljs-preprocessor">.RunWith</span><span class="hljs-comment">;</span> import java<span class="hljs-preprocessor">.util</span><span class="hljs-preprocessor">.List</span><span class="hljs-comment">;</span> <span class="hljs-comment">/** * Created by mqqzt-yang on 2015/11/22. */</span> @RunWith(AndroidJUnit4<span class="hljs-preprocessor">.class</span>) public class UiObject2Test { UiDevice uiDevice<span class="hljs-comment">;</span> Instrumentation instrumentation<span class="hljs-comment">;</span> @Before public void setUp(){ instrumentation = InstrumentationRegistry<span class="hljs-preprocessor">.getInstrumentation</span>()<span class="hljs-comment">;</span> uiDevice = UiDevice<span class="hljs-preprocessor">.getInstance</span>(instrumentation)<span class="hljs-comment">;</span> } @Test public void testSetText(){ UiObject2 mmsSendObject = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.mms:id/embedded_text_editor"</span>))<span class="hljs-comment">;</span> mmsSendObject<span class="hljs-preprocessor">.setText</span>(<span class="hljs-string">"我的东西在哪里?"</span>)<span class="hljs-comment">;</span> <span class="hljs-keyword">sleep</span>(<span class="hljs-number">1000</span>)<span class="hljs-comment">;</span> mmsSendObject<span class="hljs-preprocessor">.clear</span>()<span class="hljs-comment">;</span> } @Test public void testDrag(){ UiObject2 settingsObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Settings"</span>))<span class="hljs-comment">;</span> Point p = new Point()<span class="hljs-comment">;</span> p<span class="hljs-preprocessor">.x</span> = <span class="hljs-number">116</span><span class="hljs-comment">;</span> p<span class="hljs-preprocessor">.y</span> = <span class="hljs-number">497</span><span class="hljs-comment">;</span> settingsObj<span class="hljs-preprocessor">.drag</span>(p, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> <span class="hljs-keyword">sleep</span>(<span class="hljs-number">2000</span>)<span class="hljs-comment">;</span> settingsObj<span class="hljs-preprocessor">.clickAndWait</span>(Until<span class="hljs-preprocessor">.newWindow</span>(), <span class="hljs-number">5000</span>)<span class="hljs-comment">;</span> } @Test public void testScroll(){ UiObject2 listViewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(ListView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span> listViewObj<span class="hljs-preprocessor">.scroll</span>(Direction<span class="hljs-preprocessor">.DOWN</span>, <span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> } @Test public void testPinch(){ UiObject2 viewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.gallery3d:id/gl_root_view"</span>))<span class="hljs-comment">;</span> viewObj<span class="hljs-preprocessor">.pinchClose</span>(<span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> <span class="hljs-keyword">sleep</span>(<span class="hljs-number">2000</span>)<span class="hljs-comment">;</span> viewObj<span class="hljs-preprocessor">.pinchOpen</span>(<span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> } @Test public void testFling(){ UiObject2 msg = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.contacts:id/message"</span>))<span class="hljs-comment">;</span> msg<span class="hljs-preprocessor">.fling</span>(Direction<span class="hljs-preprocessor">.LEFT</span>, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> } @Test public void testSwipe(){ UiObject2 msg = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.contacts:id/message"</span>))<span class="hljs-comment">;</span> msg<span class="hljs-preprocessor">.swipe</span>(Direction<span class="hljs-preprocessor">.RIGHT</span>, <span class="hljs-number">0.8</span>f, <span class="hljs-number">500</span>)<span class="hljs-comment">;</span> } @Test public void testWifiSwitch(){ UiObject2 wifiSwitchObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.res</span>(<span class="hljs-string">"com.android.settings:id/switch_widget"</span>))<span class="hljs-comment">;</span> if(!wifiSwitchObj<span class="hljs-preprocessor">.isChecked</span>()){ wifiSwitchObj<span class="hljs-preprocessor">.click</span>()<span class="hljs-comment">;</span> } } @Test public void testChild(){ UiObject2 listViewObj = uiDevice<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(ListView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span> UiObject2 child = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.clazz</span>(TextView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.getText</span>())<span class="hljs-comment">;</span> List<UiObject2> listChild = listViewObj<span class="hljs-preprocessor">.findObjects</span>(By<span class="hljs-preprocessor">.clazz</span>(TextView<span class="hljs-preprocessor">.class</span>))<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, listChild<span class="hljs-preprocessor">.size</span>() + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> for(UiObject2 uiObject2 : listChild){ Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, uiObject2<span class="hljs-preprocessor">.getText</span>())<span class="hljs-comment">;</span> } List<UiObject2> getChild = listViewObj<span class="hljs-preprocessor">.getChildren</span>()<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, getChild<span class="hljs-preprocessor">.size</span>() + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> for(UiObject2 uiObject2 : getChild){ Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, uiObject2<span class="hljs-preprocessor">.getClassName</span>())<span class="hljs-comment">;</span> } Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, listViewObj<span class="hljs-preprocessor">.getParent</span>()<span class="hljs-preprocessor">.getClassName</span>())<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, listViewObj<span class="hljs-preprocessor">.getChildCount</span>() + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> UiObject2 childEqual = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>))<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.equals</span>(childEqual) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.hasObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>)) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> Log<span class="hljs-preprocessor">.i</span>(<span class="hljs-string">"mqqzt"</span>, child<span class="hljs-preprocessor">.hasObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms12"</span>)) + <span class="hljs-string">""</span>)<span class="hljs-comment">;</span> childEqual<span class="hljs-preprocessor">.recycle</span>()<span class="hljs-comment">;</span> childEqual = listViewObj<span class="hljs-preprocessor">.findObject</span>(By<span class="hljs-preprocessor">.text</span>(<span class="hljs-string">"Alarms"</span>))<span class="hljs-comment">;</span> childEqual<span class="hljs-preprocessor">.click</span>()<span class="hljs-comment">;</span> } public void <span class="hljs-keyword">sleep</span>(int <span class="hljs-keyword">sleep</span>){ try { Thread<span class="hljs-preprocessor">.sleep</span>(<span class="hljs-keyword">sleep</span>)<span class="hljs-comment">;</span> } catch (InterruptedException e) { e<span class="hljs-preprocessor">.printStackTrace</span>()<span class="hljs-comment">;</span> } } }</code>
最后
以上就是感性咖啡为你收集整理的uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157的全部内容,希望文章能够帮你解决uiautomator 之uiautomator 2.0 UiObject2学习收藏学习收藏http://blog.csdn.net/swordgirl2011/article/details/50993157所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复