一、Configurator类介绍与API解释
1.Configurator类介绍
1)用于设置脚本动过的默认延时
2)功能
a.可调节两个模拟动作间的默认时间间隔
b.可调节输入文本的输入时间间隔
c.可调节每次滚动的时间间隔
2.相关API
延时项 |
默认延时 |
功能描述 |
API |
动作 | 3s | 设置延时 | setActionAckonwledgmentTimeout(long timeout) |
获取默认延时 | getActionAckonwledgmentTimeout() | ||
键盘输入 | 0s | 设置延时 | setKeyInjectionDelay(long delay) |
获取默认延时 | getKeyInjectionDelay() | ||
滚动 | 200ms | 设置延时 | setScrollAcknowledgmentTimeout(long timeout) |
获取默认延时 | getScrollAcknowledgmentTimeout() | ||
空闲 | 10s | 设置延时 | setWaitForIdleTimeout(long timeout) |
获取默认延时 | getWaitForIdleTimeout() | ||
查找组件 | 10s | 设置延时 | setWaitForSelectorTimeout(long timeout) |
获取默认延时 | getWaitForSelectorTimeout() |
例如:
public void testDemo1() throws UiObjectNotFoundException{ //1.ActionAckonwledgmentTimeout() long action=Configurator.getInstance().getActionAcknowledgmentTimeout(); //输出默认延时 System.out.println("动作默认延时为: "+action); //获取屏幕高度和宽度 int y=UiDevice.getInstance().getDisplayHeight(); int x=UiDevice.getInstance().getDisplayWidth(); //默认延时下动作间隔时间感受 UiDevice.getInstance().swipe(x-10, y/2, x-200, y/2, 10); UiDevice.getInstance().swipe(x-10, y/2, x-200, y/2, 10); sleep(2000); //设置延时 Configurator.getInstance().setActionAcknowledgmentTimeout(100000); //使用设置延时下动作间隔时间感受 UiDevice.getInstance().swipe(x-10, y/2, x-200, y/2, 10); UiDevice.getInstance().swipe(x-10, y/2, x-200, y/2, 10); //使用完毕后调回默认延时,切记!!! Configurator.getInstance().setActionAcknowledgmentTimeout(action); //2.KeyInjectionDelay()' long print=Configurator.getInstance().getKeyInjectionDelay(); //输出键盘输入默认延时 System.out.println("键盘输入默认延时为: "+print); //获取输入框 UiObject set=new UiObject(new UiSelector().resourceId("com.android.mms:id/embedded_text_editor")); //感受默认延时下输入间隔 set.setText("aabbccd"); sleep(2000); set.clearTextField(); //修改默认延时为1秒 Configurator.getInstance().setKeyInjectionDelay(1000); //感受修改延时后输入间隔 set.setText("aabbccd"); sleep(2000); set.clearTextField(); //切记改回原始时间 Configurator.getInstance().setKeyInjectionDelay(print); //3.ScrollAcknowledgmentTimeout() long scroll=Configurator.getInstance().getScrollAcknowledgmentTimeout(); //输出默认延时 System.out.println("滚动默认延时: "+scroll); //获取列表集合 UiScrollable scroll1=new UiScrollable(new UiSelector().className("android.widget.ListView")); //感受默认滚动延时间隔 scroll1.flingToEnd(5); //设置默认滚动时间间隔为2S Configurator.getInstance().setScrollAcknowledgmentTimeout(2000); //感受设置后的滚动时间间隔 scroll1.flingToEnd(5); //切记使用完毕改回为默认时间间隔 Configurator.getInstance().setScrollAcknowledgmentTimeout(scroll); //4.WaitForIdleTimeout() long wait=Configurator.getInstance().getWaitForIdleTimeout(); System.out.println("空闲默认延时为: "+wait); //5.WaitForSelectorTimeout() long selector=Configurator.getInstance().getWaitForSelectorTimeout(); //输出默认时间间隔 System.out.println("查找组件默认延时为 :"+selector); //随意查找一个对象 UiScrollable scroll2=new UiScrollable(new UiSelector().className("11321")); scroll2.click(); //修改默认时间间隔 Configurator.getInstance().setWaitForSelectorTimeout(1000); //感受修改后的时间间隔 UiScrollable scroll3=new UiScrollable(new UiSelector().className("11321")); scroll3.click(); }
3.实例演示
1)模拟双击与快速多单击动作
//声明方法 public void quicklyClick(int num,int x,int y){ //获取动作间隔时间 long actionTime=Configurator.getInstance().getActionAcknowledgmentTimeout(); //设置动作间隔时间 Configurator.getInstance().setActionAcknowledgmentTimeout(0); //设置操作步骤 for(int i=0;i<=num;i++){ UiDevice.getInstance().click(x, y); } //最后别忘了恢复默认时间间隔,避免影响后面其他的用例 Configurator.getInstance().setActionAcknowledgmentTimeout(actionTime); } //调用方法 public void testDemo2(){ //获取屏幕高和宽 int y=UiDevice.getInstance().getDisplayHeight(); int x=UiDevice.getInstance().getDisplayWidth(); //调用上面声明的方法体 quicklyClick(4, x/2,y/2); }
2)使用keyCode快速输入
//设置方法 public void quicklyKeyCode(String input){ //获取动作间隔时间 long action=Configurator.getInstance().getActionAcknowledgmentTimeout(); //设置动作间隔时间 Configurator.getInstance().setActionAcknowledgmentTimeout(1); //方法中用到的数字来自androidKeyCode表 for (int i=0;i<input.length();i++){ char c=input.charAt(i); //判断输出 if (c>48&&c<=57){ UiDevice.getInstance().pressKeyCode(c-41); }else if(c>=97&&c<=122){ UiDevice.getInstance().pressKeyCode(c-68); }else if (c>=65&&c<=90){ UiDevice.getInstance().pressKeyCode(c-36,1); } } //千万别忘记恢复默认 Configurator.getInstance().setActionAcknowledgmentTimeout(action); } //调用方法 public void testDemo3(){ quicklyKeyCode("Jian110"); }