Android 自己的自動化測試(2)依據ID尋找對象(java)

來源:互聯網
上載者:User

標籤:

前一篇文章是寫 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 Auto Test&Android Code Coverage Test

Android 自己的自動化測試(2)依據ID尋找對象(java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.