Android 自动化测试(2)根据ID查找对象(java)

之前一篇文章写的是 Android 自动化测试(1)如何安装和卸载一个应用(java) ,下面再探索一下如果在普通java应用程序中,根据ID来查找对象


1、类库依赖:

The library dependencies are:
chimpchat.jar,common.jar,ddmlib.jar,guava-13.0.1.jar,sdklib.jar,hierchyviewer2lib.jar
They can all be found in the sdk/tools/lib subdirectory of the ADT bundle.


2、代码:

package com.robot.dream.test;

import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.adb.LinearInterpolator.Point;
import com.android.chimpchat.core.IChimpDevice;
import com.android.chimpchat.core.TouchPressType;
import com.android.chimpchat.hierarchyviewer.HierarchyViewer;
import com.android.hierarchyviewerlib.models.ViewNode;

public class TestRobotDream {

    public static Point getAbsoluteCenterOfView(ViewNode node) {
        Point point = getAbsolutePositionOfView(node);
        return new Point(point.getX() + node.width / 2, point.getY() + node.height / 2);
    }

    public static Point getAbsolutePositionOfView(ViewNode node) {
        int x = node.left;
        int y = node.top;
        for(ViewNode p = node.parent; p != null; p = p.parent) {
            x += p.left - p.scrollX;
            y += p.top - p.scrollY;
        }
        return new Point(x, y);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("start");
        IChimpDevice device = new AdbBackend().waitForConnection();
        System.out.println("device ok");
        
        device.shell("am start com.robot.dream/com.robot.dream.MainActivity");

        System.out.println("monkey test connected");
        HierarchyViewer view = device.getHierarchyViewer();
        ViewNode viewNode = view.findViewById("id/center_image");
        //Point p = HierarchyViewer.getAbsoluteCenterOfView(viewNode);

        Point p = getAbsoluteCenterOfView(viewNode);
        //EasyMonkeyDevice easyDevice = new EasyMonkeyDevice(null);

        System.out.println(" x: "+p.getX() + " y: "+p.getY());

        //By.id("");
        //By selector = By.id("");
        System.out.println("touch 1 ok");
        device.touch(p.getX(), p.getY(), TouchPressType.DOWN_AND_UP);

        System.out.println("touch 2 ok");
        device.touch(p.getX(), p.getY(), TouchPressType.DOWN_AND_UP);

        //easyDevice.touch(selector, TouchPressType.DOWN_AND_UP);
        device.dispose();

    }

}


3、局限性:

以上的自动化测试,这种方式是采用monkeyrunner对应的java, lib来实现的。

所以其本质上就是java版的monkey runner

它可以实现的功能,基本上monkey runner也都能实现,如果用java实现,文档少,材料少,有点得不偿失。

探索用java来实现,最大的收获,是对这些测试相关类的学习。


4、后面会介绍些 Android monkeyrunner &Android Unit Test &Android Code Coverage Test

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。