获取对象属性与属性的判断
1、获取对象属性相关API
| 返回值 | API | 说明 |
| Rect | getBounds() | 获取对象矩形坐标,矩形左上角坐标与右下角坐标 |
| int | getChildCount() | 获得下一级子类数量 |
| String | getClassName() | 获得对象类名属性的类名文本 |
| String | getCountDescription() | 获得对象描述属性的描述文本 |
| String | getPackageName() | 获得对象包名属性的包名文本 |
| String | getText() | 获得对象文本属性的文本 |
| Rect | getVisibleBounds() | 返回可见视图的范围,如果视图的部分是可见的,只有可见部分报告的范围 |
package com.test.uiobject; import android.graphics.Rect; import android.view.KeyEvent; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class Demo extends UiAutomatorTestCase { /** * @param args */ public static void main(String[] args) { String jarName,testClass,testName,androidId; jarName="demo"; testClass="com.test.uiobject.Demo"; testName="testGet"; androidId="1"; new UiAutomatorHelper(jarName,testClass,testName,androidId); } public void testGet() throws UiObjectNotFoundException{ UiDevice.getInstance().pressHome(); sleep(2000); UiObject message=new UiObject(new UiSelector().text("Messaging")); message.clickAndWaitForNewWindow(); UiObject createMessage=new UiObject(new UiSelector().resourceId("com.android.mms:id/action_compose_new")); createMessage.clickAndWaitForNewWindow(); UiObject messageContent=new UiObject(new UiSelector().resourceId("com.android.mms:id/embedded_text_editor")); String text=messageContent.getText(); System.out.println("Text is: "+text); String className=messageContent.getClassName(); System.out.println("ClassName is: "+className); String description=messageContent.getContentDescription(); System.out.println(description); String packageName=messageContent.getPackageName(); System.out.println(packageName); Rect rect=messageContent.getBounds(); System.out.println(rect); //childCount() UiDevice.getInstance().pressHome(); sleep(2000); UiObject apps=new UiObject(new UiSelector().descriptionContains("Apps")); apps.clickAndWaitForNewWindow(); UiObject fileManager=new UiObject(new UiSelector().text("File Manager")); fileManager.clickAndWaitForNewWindow(); UiObject movies=new UiObject(new UiSelector().resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item").index(4)); System.out.println("Movies have "+movies.getChildCount()+" childs"); movies.click(); } }