基础动作模拟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) | 等待的条件得到满足 |
示例代码:
package mqqzt.myuitest5
import android.app.Instrumentation
import android.graphics.Point
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import android.support.test.uiautomator.By
import android.support.test.uiautomator.Direction
import android.support.test.uiautomator.UiDevice
import android.support.test.uiautomator.UiObject2
import android.support.test.uiautomator.Until
import android.util.Log
import android.widget.ListView
import android.widget.TextView
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.List
@RunWith(AndroidJUnit4.class)
public class UiObject2Test {
UiDevice uiDevice
Instrumentation instrumentation
@Before
public void setUp(){
instrumentation = InstrumentationRegistry.getInstrumentation()
uiDevice = UiDevice.getInstance(instrumentation)
}
@Test
public void testSetText(){
UiObject2 mmsSendObject = uiDevice.findObject(By.res("com.android.mms:id/embedded_text_editor"))
mmsSendObject.setText("我的东西在哪里?")
sleep(1000)
mmsSendObject.clear()
}
@Test
public void testDrag(){
UiObject2 settingsObj = uiDevice.findObject(By.text("Settings"))
Point p = new Point()
p.x = 116
p.y = 497
settingsObj.drag(p, 500)
sleep(2000)
settingsObj.clickAndWait(Until.newWindow(), 5000)
}
@Test
public void testScroll(){
UiObject2 listViewObj = uiDevice.findObject(By.clazz(ListView.class))
listViewObj.scroll(Direction.DOWN, 0.8f, 500)
}
@Test
public void testPinch(){
UiObject2 viewObj = uiDevice.findObject(By.res("com.android.gallery3d:id/gl_root_view"))
viewObj.pinchClose(0.8f, 500)
sleep(2000)
viewObj.pinchOpen(0.8f, 500)
}
@Test
public void testFling(){
UiObject2 msg = uiDevice.findObject(By.res("com.android.contacts:id/message"))
msg.fling(Direction.LEFT, 500)
}
@Test
public void testSwipe(){
UiObject2 msg = uiDevice.findObject(By.res("com.android.contacts:id/message"))
msg.swipe(Direction.RIGHT, 0.8f, 500)
}
@Test
public void testWifiSwitch(){
UiObject2 wifiSwitchObj = uiDevice.findObject(By.res("com.android.settings:id/switch_widget"))
if(!wifiSwitchObj.isChecked()){
wifiSwitchObj.click()
}
}
@Test
public void testChild(){
UiObject2 listViewObj = uiDevice.findObject(By.clazz(ListView.class))
UiObject2 child = listViewObj.findObject(By.clazz(TextView.class))
Log.i("mqqzt", child.getText())
List<UiObject2> listChild = listViewObj.findObjects(By.clazz(TextView.class))
Log.i("mqqzt", listChild.size() + "")
for(UiObject2 uiObject2 : listChild){
Log.i("mqqzt", uiObject2.getText())
}
List<UiObject2> getChild = listViewObj.getChildren()
Log.i("mqqzt", getChild.size() + "")
for(UiObject2 uiObject2 : getChild){
Log.i("mqqzt", uiObject2.getClassName())
}
Log.i("mqqzt", listViewObj.getParent().getClassName())
Log.i("mqqzt", listViewObj.getChildCount() + "")
UiObject2 childEqual = listViewObj.findObject(By.text("Alarms"))
Log.i("mqqzt", child.equals(childEqual) + "")
Log.i("mqqzt", child.hasObject(By.text("Alarms")) + "")
Log.i("mqqzt", child.hasObject(By.text("Alarms12")) + "")
childEqual.recycle()
childEqual = listViewObj.findObject(By.text("Alarms"))
childEqual.click()
}
public void sleep(int sleep){
try {
Thread.sleep(sleep)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}