您好,登錄后才能下訂單哦!
在使用MonkeyRunner的時候我們經常會用到Chimchat下面的HierarchyViewer模塊來獲取目標控件的一些信息來輔助我們測試,但在MonkeyRunner的官網上是沒有看到相應的API的描述的,上面只有以下三個類的API引用信息(http://developer.android.com/tools/help/MonkeyDevice.html)
targetDevice = MonkeyRunner.waitForConnection() ''' public ViewNode findViewById(String id) * @param id id for the view. * @return view with the specified ID, or {@code null} if no view found. ''' viewer = targetDevice.getHierarchyViewer() button = viewer.findViewById('id/title') text = viewer.getText(button) print text.encode('utf-8')
''' public ViewNode findViewById(String id, ViewNode rootNode) * Find a view by ID, starting from the given root node * @param id ID of the view you're looking for * @param rootNode the ViewNode at which to begin the traversal * @return view with the specified ID, or {@code null} if no view found. ''' iconMenuView = viewer.findViewById('id/icon_menu') button = viewer.findViewById('id/title',iconMenuView) print "Button Text:",text.encode('utf-8')
''' public static Point getAbsoluteCenterOfView(ViewNode node) * Gets the absolute x/y center of the specified view node. * * @param node view node to find position of. * @return absolute x/y center of the specified view node. */ ''' point = viewer.getAbsoluteCenterOfView(button) print "Button Absolute Center Position:",point
''' public static Point getAbsoluteCenterOfView(ViewNode node) * Gets the absolute x/y center of the specified view node. * * @param node view node to find position of. * @return absolute x/y center of the specified view node. */ ''' point = viewer.getAbsoluteCenterOfView(button) print "Button Absolute Center Position:",point
''' public String getFocusedWindowName() * Gets the window that currently receives the focus. * * @return name of the window that currently receives the focus. ''' window = viewer.getFocusedWindowName() print "Window Name:",window.encode('utf-8')
其實就是獲得當前打開的窗口的packageName/activityName,輸出與HierarchyViewer工具檢測到的信息一致,所以猜想其用到同樣的方法。
輸出:
''' public boolean visible(ViewNode node) * Gets the visibility of a given element. * @param selector selector for the view. * @return True if the element is visible. ''' isVisible = viewer.visible(button) print "is visible:",isVisible就是查看下控件是否可見,沒什么好解析的了。
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice from com.android.monkeyrunner.easy import EasyMonkeyDevice,By from com.android.chimpchat.hierarchyviewer import HierarchyViewer from com.android.hierarchyviewerlib.models import ViewNode, Window from java.awt import Point #from com.android.hierarchyviewerlib.device import #Connect to the target targetDevice targetDevice = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need this targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList") #time.sleep(2000) #invoke the menu options MonkeyRunner.sleep(6) targetDevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP); ''' public ViewNode findViewById(String id) * @param id id for the view. * @return view with the specified ID, or {@code null} if no view found. ''' viewer = targetDevice.getHierarchyViewer() button = viewer.findViewById('id/title') text = viewer.getText(button) print text.encode('utf-8') ''' public ViewNode findViewById(String id, ViewNode rootNode) * Find a view by ID, starting from the given root node * @param id ID of the view you're looking for * @param rootNode the ViewNode at which to begin the traversal * @return view with the specified ID, or {@code null} if no view found. ''' iconMenuView = viewer.findViewById('id/icon_menu') button = viewer.findViewById('id/title',iconMenuView) print "Button Text:",text.encode('utf-8') ''' public String getFocusedWindowName() * Gets the window that currently receives the focus. * * @return name of the window that currently receives the focus. ''' window = viewer.getFocusedWindowName() print "Window Name:",window.encode('utf-8') ''' public static Point getAbsoluteCenterOfView(ViewNode node) * Gets the absolute x/y center of the specified view node. * * @param node view node to find position of. * @return absolute x/y center of the specified view node. */ ''' point = viewer.getAbsoluteCenterOfView(button) print "Button Absolute Center Position:",point ''' public static Point getAbsolutePositionOfView(ViewNode node) * Gets the absolute x/y position of the view node. * * @param node view node to find position of. * @return point specifying the x/y position of the node. ''' point = viewer.getAbsolutePositionOfView(button) print "Button Absolute Position:", point ''' public boolean visible(ViewNode node) * Gets the visibility of a given element. * @param selector selector for the view. * @return True if the element is visible. ''' isVisible = viewer.visible(button) print "is visible:",isVisible
/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.chimpchat.hierarchyviewer; import com.android.ddmlib.IDevice; import com.android.ddmlib.Log; import com.android.hierarchyviewerlib.device.DeviceBridge; import com.android.hierarchyviewerlib.device.ViewServerDevice; import com.android.hierarchyviewerlib.models.ViewNode; import com.android.hierarchyviewerlib.models.Window; import org.eclipse.swt.graphics.Point; /** * Class for querying the view hierarchy of the device. */ public class HierarchyViewer { public static final String TAG = "hierarchyviewer"; private IDevice mDevice; /** * Constructs the hierarchy viewer for the specified device. * * @param device The Android device to connect to. */ public HierarchyViewer(IDevice device) { this.mDevice = device; setupViewServer(); } private void setupViewServer() { DeviceBridge.setupDeviceForward(mDevice); if (!DeviceBridge.isViewServerRunning(mDevice)) { if (!DeviceBridge.startViewServer(mDevice)) { // TODO: Get rid of this delay. try { Thread.sleep(2000); } catch (InterruptedException e) { } if (!DeviceBridge.startViewServer(mDevice)) { Log.e(TAG, "Unable to debug device " + mDevice); throw new RuntimeException("Could not connect to the view server"); } return; } } DeviceBridge.loadViewServerInfo(mDevice); } /** * Find a view by id. * * @param id id for the view. * @return view with the specified ID, or {@code null} if no view found. */ public ViewNode findViewById(String id) { ViewNode rootNode = DeviceBridge.loadWindowData( new Window(new ViewServerDevice(mDevice), "", 0xffffffff)); if (rootNode == null) { throw new RuntimeException("Could not dump view"); } return findViewById(id, rootNode); } /** * Find a view by ID, starting from the given root node * @param id ID of the view you're looking for * @param rootNode the ViewNode at which to begin the traversal * @return view with the specified ID, or {@code null} if no view found. */ public ViewNode findViewById(String id, ViewNode rootNode) { if (rootNode.id.equals(id)) { return rootNode; } for (ViewNode child : rootNode.children) { ViewNode found = findViewById(id,child); if (found != null) { return found; } } return null; } /** * Gets the window that currently receives the focus. * * @return name of the window that currently receives the focus. */ public String getFocusedWindowName() { int id = DeviceBridge.getFocusedWindow(mDevice); Window[] windows = DeviceBridge.loadWindows(new ViewServerDevice(mDevice), mDevice); for (Window w : windows) { if (w.getHashCode() == id) return w.getTitle(); } return null; } /** * Gets the absolute x/y position of the view node. * * @param node view node to find position of. * @return point specifying the x/y position of the node. */ public static Point getAbsolutePositionOfView(ViewNode node) { int x = node.left; int y = node.top; ViewNode p = node.parent; while (p != null) { x += p.left - p.scrollX; y += p.top - p.scrollY; p = p.parent; } return new Point(x, y); } /** * Gets the absolute x/y center of the specified view node. * * @param node view node to find position of. * @return absolute x/y center of the specified view node. */ public static Point getAbsoluteCenterOfView(ViewNode node) { Point point = getAbsolutePositionOfView(node); return new Point( point.x + (node.width / 2), point.y + (node.height / 2)); } /** * Gets the visibility of a given element. * * @param selector selector for the view. * @return True if the element is visible. */ public boolean visible(ViewNode node) { boolean ret = (node != null) && node.namedProperties.containsKey("getVisibility()") && "VISIBLE".equalsIgnoreCase( node.namedProperties.get("getVisibility()").value); return ret; } /** * Gets the text of a given element. * * @param selector selector for the view. * @return the text of the given element. */ public String getText(ViewNode node) { if (node == null) { throw new RuntimeException("Node not found"); } ViewNode.Property textProperty = node.namedProperties.get("text:mText"); if (textProperty == null) { // give it another chance, ICS ViewServer returns mText textProperty = node.namedProperties.get("mText"); if (textProperty == null) { throw new RuntimeException("No text property on node"); } } return textProperty.value; } }
作者 | 自主博客 | 微信 | CSDN |
天地會珠海分舵 | http://techgogogo.com | 服務號:TechGoGoGo 掃描碼:
| 向AI問一下細節 免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。 猜你喜歡最新資訊相關推薦
相關標簽AI
助 手
汽车|
永德县|
志丹县|
大方县|
垦利县|
淮北市|
瓦房店市|
富蕴县|
定襄县|
云南省|
克东县|
哈巴河县|
古丈县|
拜城县|
平度市|
阿图什市|
恩平市|
水富县|
荔波县|
左贡县|
西贡区|
抚宁县|
桦甸市|
鹤岗市|
安乡县|
普宁市|
余庆县|
龙陵县|
淮阳县|
章丘市|
景德镇市|
威信县|
太白县|
怀远县|
茂名市|
宁晋县|
建水县|
博爱县|
晋中市|
全南县|
韶关市|
|