本人在使用UiAutomator做测试的时候,遇到一些控件需要长按一会儿,比如录音功能,需要按住几秒,官方api不太好用,所以自己写了一个长按的方法。分享如下,供大家参考。
/*
* 根据resourceid获取控件并长按
* /
public void longclickUiObectByResourceId(String id) throws UiObjectNotFoundException {
int x = getUiObjectByResourceId(id).getBounds().centerX();
int y = getUiObjectByResourceId(id).getBounds().centerY();
UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
}
/*
* 根据文本获取控件并长按
* /
public void longclickUiObectByText(String text) throws UiObjectNotFoundException {
int x = getUiObjectByText(text).getBounds().centerX();
int y = getUiObjectByText(text).getBounds().centerY();
UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
}
/*
* 根据坐标并长按
* /
public void longclickUiObectByText(int x, int y) throws UiObjectNotFoundException {
UiDevice.getInstance().swipe(x, y, x, y, 300);//最后一个参数单位是5ms
}
文章写作时间较早了,UiAutomator1基础的API进行封装的,还可以封装几个根据classname、index、或者description都可以,这里就不一一写了,最重要的最后一个,不常用但是很有用,根据坐标点进行长按。还有一个根据坐标的数组进行轨迹的描绘,请参考android uiautomator一个画心形图案的方法--代码的浪漫