Configurator用于设置脚本动作的默认延时(比如两次点击之间,系统时加入延时的)
Cofigurator功能:
API:
延时项 | 默认延时 | 功能描述 | API |
---|---|---|---|
动作 | 3s | 设置延时 | setActionAcknowledgmentTimeout(long timeout) |
获取默认延时 | getActionAcknowledgmentTimeout() | ||
键盘输入 | 0s | 设置延时 | setKeyInjectionDelay(long delay) |
获取默认延时 | getKeyInjectionDelay() | ||
滚动 | 200ms | 设置延时 | setScrollAcknowledmentTimeout(long timeout) |
获取默认延时 | getScrollAcknowledgmentTimeout() | ||
空闲 | 10s | 设置延时 | setWaitForIdleTimeout(long timeout) |
获取默认延时 | getWaitForIdleTimeout() | ||
查找组件 | 10s | 设置延时 | setWaitForSelectorTimeout(long timeout) |
获取默认延时 | getWaitForSelectorTimeout() |
public void testAction(){
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
//获取默认动作延时
long actionAcknowledgmentTimeout = Configurator.getInstance().getActionAcknowledgmentTimeout();
System.out.println("默认动作延时"+actionAcknowledgmentTimeout);
int x = uiDevice.getDisplayWidth();
int y = uiDevice.getDisplayHeight();
uiDevice.swipe(x-50, y/2, 50, y/2, 10);
//设置动作延时
Configurator.getInstance().setActionAcknowledgmentTimeout(1);
uiDevice.swipe(x-50, y/2, 50, y/2, 10);
}
/**
* 设置输入延时
*/
public void testInput() throws UiObjectNotFoundException {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject edit = new UiObject(new UiSelector().className("android.widget.EditText"));
edit.setText("wjxjwxjw");
Configurator.getInstance().setKeyInjectionDelay(1000);
edit.setText("gyjgjy");
}
/**
* 滚动延时
*/
public void testSroll() throws UiObjectNotFoundException {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiScrollable uiScrollable = new UiScrollable(new UiSelector().className("android.widget.AbsListView"));
uiScrollable.flingToBeginning(10);
Configurator.getInstance().setScrollAcknowledgmentTimeout(2000);
uiScrollable.flingToEnd(10);
}
/**
* 查找对象
*/
public void testObject() throws UiObjectNotFoundException {
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
//肯定找不到这个对象,看看是否默认为10秒
UiScrollable uiScrollable = new UiScrollable(new UiSelector().className("wkjx"));
//设置为2秒
Configurator.getInstance().setWaitForSelectorTimeout(2000);
uiScrollable.click();
}
eg:
/**
* 实现多次点击API
* @param num
* @param x
* @param y
*/
public void quicklyClick(int num, int x, int y){
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
long actionTime = Configurator.getInstance().getActionAcknowledgmentTimeout();
Configurator.getInstance().setActionAcknowledgmentTimeout(0);
for(int i = 0;i<num;i++){
uiDevice.click(x,y);
}
Configurator.getInstance().setActionAcknowledgmentTimeout(actionTime);
}
/**
* 实现自定义字符串
* @param input
*/
public void setTextforKeyCode(String input){
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
long actTime = Configurator.getInstance().getActionAcknowledgmentTimeout();
Configurator.getInstance().setActionAcknowledgmentTimeout(500);
//输入字符串
for(int i = 0;i<input.length();i++) {
char c = input.charAt(i);
//如果是数字
if(c>=48 && c<=57){
//Ascll码转成keycode
uiDevice.pressKeyCode(c-41);
}else if(c>=97 && c<=122){
//小写字母
uiDevice.pressKeyCode(c-68);
}else if(c>=65 && c<=90){
//大写字母
uiDevice.pressKeyCode(c-36, 1);
}
}
//将延时改回去,避免之后的测试用例受影响
Configurator.getInstance().setActionAcknowledgmentTimeout(actTime);
}