Configurator类介绍与使用

邓鸿信
2023-12-01
  • 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);
}
 类似资料: