Android无线测试之—UiAutomator UiObject API介绍五
获取对象属性与属性的判断
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(); } }
2、获取对象父类与子类节点
返回值 | API | 说明 |
UiObject | getChild(UiSelector selector) | 获取对象的子类对象,可以递归获取子孙当中某个对象 |
UiObject | getFromParent(UiSelector selector) | 从父类获取子类,按照UiSelector获取兄弟类(递归) |
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="testGetChildAndGetFromParent"; androidId="1"; new UiAutomatorHelper(jarName,testClass,testName,androidId); } public void testGetChildAndGetFromParent() throws UiObjectNotFoundException{ 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)); //getChild() String fileName1=movies.getChild(new UiSelector().className("android.widget.TextView").index(0)).getText(); System.out.println("The file1 name is: "+fileName1); //getFromParent() //movies.getFromParent(new UiSelector().resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item").index(2)).click(); movies.getFromParent(new UiSelector().className("android.widget.LinearLayout").index(2)).click(); } }
3、属性的判断
返回值 | API | 描述 |
boolean | isCheckable() | 检查对象Checkable属性是否为true |
boolean | isChecked() | 检查对象Checked属性是否为true |
boolean | isClickable() | 检查对象Clickable属性是否为true |
boolean | isEnabled() | 检查对象Enabled属性是否为true |
boolean | isFocusable() | 检查对象Focusable属性是否为true |
boolean | isFocused() | 检查对象Focused属性是否为true |
boolean | isLongClickable() | 检查对象LongClickable属性是否为true |
boolean | isScrollable() | 检查对象Scrollable属性是否为true |
boolean | isSelected() | 检查对象Selected属性是否为true |
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="testTrueOrFalse"; androidId="1"; new UiAutomatorHelper(jarName,testClass,testName,androidId); } public void testTrueOrFalse() throws UiObjectNotFoundException{ UiDevice.getInstance().pressHome(); sleep(2000); UiObject apps=new UiObject(new UiSelector().descriptionContains("Apps")); apps.clickAndWaitForNewWindow(); UiObject setting=new UiObject(new UiSelector().text("Settings")); setting.click(); sleep(2000); //WiFi开启则点击关闭,关闭的则点击开启 UiObject wifi=new UiObject(new UiSelector().className("android.widget.Switch").instance(0)); if(!wifi.isChecked()){ wifi.click(); System.out.println("WiFi's checked is "+wifi.isChecked()); } else wifi.click(); System.out.println("WiFi's checked is "+wifi.isChecked()); } }