Configurator

贲铭
2023-12-01

示例代码:

package com.mqqzt.john.myuitest;

import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.util.Log;
import android.widget.ListView;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by john on 2015/11/24.
 */
@RunWith(AndroidJUnit4.class)
public class ConfiguratorTest {
    UiDevice uiDevice;
    Instrumentation instrumentation;

    @Before
    public void setUp(){
        instrumentation = InstrumentationRegistry.getInstrumentation();
        uiDevice = UiDevice.getInstance(instrumentation);
    }

    @Test
    public void testAction(){
        long actionTimeout = Configurator.getInstance().getActionAcknowledgmentTimeout();
        Log.i("mqqzt", "ActionTimeout:" + actionTimeout);
        //设置两个动作之间的时间间隔
        Configurator.getInstance().setActionAcknowledgmentTimeout(0);
        int x = uiDevice.getDisplayWidth();
        int y = uiDevice.getDisplayHeight();
        uiDevice.swipe(x - 50, y / 2, 50, y / 2, 20);
        uiDevice.swipe(x - 50, y / 2, 50, y / 2, 20);
        Configurator.getInstance().setActionAcknowledgmentTimeout(actionTimeout);
    }

    @Test
    public void testScroll(){
        long scrollTimeout = Configurator.getInstance().getScrollAcknowledgmentTimeout();
        Log.i("mqqzt", "ScrollTimeout:" + scrollTimeout);
        //设置两次滚动之间的时间间隔,对UiObject2对象无效
        Configurator.getInstance().setScrollAcknowledgmentTimeout(5000);
        UiObject2 listViewObj = uiDevice.findObject(By.clazz(ListView.class));
        listViewObj.scroll(Direction.DOWN, 0.8f);
        listViewObj.scroll(Direction.DOWN, 0.8f);
        Configurator.getInstance().setScrollAcknowledgmentTimeout(scrollTimeout);
    }

    @Test
    public void testKeyInject(){
        long keyInjectTimeout = Configurator.getInstance().getKeyInjectionDelay();
        Log.i("mqqzt", "KeyInjectTimeout:" + keyInjectTimeout);
        //设置输入文字之间的时间间隔
        Configurator.getInstance().setKeyInjectionDelay(1000);
        UiObject2 editObj = uiDevice.findObject(By.res("com.android.mms:id/embedded_text_editor"));
        editObj.setText("ieejfsjfjsdf");
        Configurator.getInstance().setKeyInjectionDelay(keyInjectTimeout);
    }

    @Test
    public void testSelector(){
        long waitForSelectorTimeout = Configurator.getInstance().getWaitForSelectorTimeout();
        Log.i("mqqzt", "WaitForSelectorTimeout:" + waitForSelectorTimeout);
        //设置寻找控件的超时时间
        Configurator.getInstance().setWaitForSelectorTimeout(1000);
        UiObject2 uiObject2 = uiDevice.findObject(By.text("dkjsdkjfskldjf"));
        uiObject2.click();
        Configurator.getInstance().setWaitForSelectorTimeout(waitForSelectorTimeout);
    }

    @Test
    public void testDoubleClick(){
        int x = uiDevice.getDisplayWidth();
        int y = uiDevice.getDisplayHeight();
        multiClick(2, x / 2, y / 2);
    }

    @Test
    public void testKeycodeInput(){
        keycodeInput("12abAB");
    }


    public void multiClick(int num, int x, int y){
        long actionTimeout = Configurator.getInstance().getActionAcknowledgmentTimeout();
        Configurator.getInstance().setActionAcknowledgmentTimeout(0);
        for(int i = 0; i < num; i++){
            uiDevice.click(x, y);
        }
        Configurator.getInstance().setActionAcknowledgmentTimeout(actionTimeout);
    }


    public void keycodeInput(String input){
        long keyInjectTimeout = Configurator.getInstance().getActionAcknowledgmentTimeout();
        Configurator.getInstance().setActionAcknowledgmentTimeout(0);
        for (int i = 0; i < input.length(); i++){
            char c = input.charAt(i);
            if(c >= 48 && c <= 57){
                uiDevice.pressKeyCode(c - 41);
            }else if (c >= 65 && c <= 90){
                uiDevice.pressKeyCode(c - 36, 1);
            }else if (c >= 97 && c <= 122){
                uiDevice.pressKeyCode(c - 68);
            }
        }
        Configurator.getInstance().setActionAcknowledgmentTimeout(keyInjectTimeout);
    }
}
 类似资料:

相关阅读

相关文章

相关问答