Android无线测试之—UiAutomator UiObject API介绍五
2024-09-27 01:40:33
获取对象属性与属性的判断
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();
} }
Demo.java
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();
} }
Demo.java
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());
} }
Demo.java
最新文章
- HTTP连接管理
- React-webpack开发需要的那些坑
- Web方式预览Office/Word/Excel/pdf文件解决方案
- 使用WordPress模板搭建博客系统
- window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马
- Windows 窗体—— 键盘输入工作原理
- php获取内容中第一张图片地址
- phpcms插件开发初步规范
- Android内存泄漏监测(MAT)及解决办法
- MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 二
- ibatis 自动生成map,bean,dao
- Mybatis源码解析-DynamicSqlSource和RawSqlSource的区别
- ceph-deploy出错UnableToResolveError Unable to resolve host
- 讨论MMU
- kafka实战kerberos
- abaqus2016安装过程中出现error:unable to add abaqus command directory to PATH variable
- python基础学习笔记 - 备忘
- Spring AOP 和 AspectJ
- 新装的arcgis10.5特别卡
- delphi 动态加载dll