UIAutomator2.0详解(UIDevice篇----触屏操作3)

本篇将记录触屏的最后4个方法,用于点击,拖拽和滑动。

先列举一下方法
(1)public boolean click(int x, int y),点击
(2)public boolean swipe(int startX, int startY, int endX, int endY, int steps) ,滑动
(3)public boolean swipe(Point[] segments, int segmentSteps),滑动
(4)public boolean drag(int startX, int startY, int endX, int endY, int steps),拖拽

可见,四个方法都是基于坐标(Point)的。
其中public boolean swipe(Point[] segments, int segmentSteps)基于坐标数组。
这里,需要做着重说明的是参数steps和segmentSteps。

steps:是指,分多少次完成这次动作。每次移动花费的时间是固定的,都为5ms。
segmentSteps:是指,点与点之间分多少次完成这次动作。同steps。

我们还是用一个示例来演示方法的功能。

代码如下:

@Test
    public void FunctionKeyTest4(){
        Log.i(TAG, "Start Test");
        mDevice.waitForIdle(timeOut);

        int h=mDevice.getDisplayHeight();
        int w=mDevice.getDisplayWidth();

        int left=200;
        int right=w-200;
        int step=50;
        Log.i(TAG, "left = "+left+" Right = "+right);

        //(1)From Right to Left
        Log.i(TAG, "from ("+right+","+h/2+") to ("+left+","+h/2+")");
        result=mDevice.swipe(right, h/2, left, h/2, step);
        Log.i(TAG, "Swipe result (Right to Left) = "+result);
        mDevice.waitForIdle(timeOut);

        //(2)From Left to Right
        Log.i(TAG, "from ("+left+","+h/2+") to ("+right+","+h/2+")");
        result=mDevice.swipe(left, h/2, right, h/2, step);
        Log.i(TAG, "Swipe result (Left to Right) = "+result);
        mDevice.waitForIdle(timeOut);

        //(3)Drag
        Log.i(TAG, "Dragfrom (300,1000) to (700,1000)");
        result=mDevice.drag(left, h/2, right, h/2, step);
        Log.i(TAG, "Drag result (Left to Right) = "+result);
        mDevice.waitForIdle(timeOut);

        //(4)From Left to Right
        Point[] pointArray=new Point[]{
                new Point(200,h/2),new Point(300,h/2),new Point(400,h/2),new Point(500,h/2),new Point(600,h/2),new Point(700,h/2),
        };
        result=mDevice.swipe(pointArray, step);
        Log.i(TAG, "Swipe by Array, result (Left to Right) = "+result);
        mDevice.waitForIdle(timeOut);

        //(5)Click
        result=mDevice.click(300, 1000);
        Log.i(TAG, "Click = "+result);
        mDevice.waitForIdle(timeOut);

    }

运行结果如下:

11-07 17:12:04.433 I/com.breakloop.u2demo.uidevice.FingerTest: Start Test
11-07 17:12:04.930 I/com.breakloop.u2demo.uidevice.FingerTest: left = 200 Right = 880
11-07 17:12:04.930 I/com.breakloop.u2demo.uidevice.FingerTest: from (880,906) to (200,906)
11-07 17:12:05.947 I/com.breakloop.u2demo.uidevice.FingerTest: Swipe result (Right to Left) = true
11-07 17:12:06.770 I/com.breakloop.u2demo.uidevice.FingerTest: from (200,906) to (880,906)
11-07 17:12:07.738 I/com.breakloop.u2demo.uidevice.FingerTest: Swipe result (Left to Right) = true
11-07 17:12:08.592 I/com.breakloop.u2demo.uidevice.FingerTest: Dragfrom (300,1000) to (700,1000)
11-07 17:12:10.026 I/com.breakloop.u2demo.uidevice.FingerTest: Drag result (Left to Right) = true
11-07 17:12:15.647 I/com.breakloop.u2demo.uidevice.FingerTest: Swipe by Array, result (Left to Right) = true
11-07 17:12:16.579 I/com.breakloop.u2demo.uidevice.FingerTest: Click = true

运行效果如下:

从运行效果看,drag并没有体现出来,跟手动的拖拽明显不同。
手动的拖拽,首先需要长按。而drag只是点击,因此无法实现拖动,从而在效果上更像是滑动。不排除是在主界面拖拽的原因。

你可能感兴趣的:(android测试,Android自动化测试)