获取对象属性与属性的判断

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

最新文章

  1. HTTP连接管理
  2. React-webpack开发需要的那些坑
  3. Web方式预览Office/Word/Excel/pdf文件解决方案
  4. 使用WordPress模板搭建博客系统
  5. window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马
  6. Windows 窗体—— 键盘输入工作原理
  7. php获取内容中第一张图片地址
  8. phpcms插件开发初步规范
  9. Android内存泄漏监测(MAT)及解决办法
  10. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 二
  11. ibatis 自动生成map,bean,dao
  12. Mybatis源码解析-DynamicSqlSource和RawSqlSource的区别
  13. ceph-deploy出错UnableToResolveError Unable to resolve host
  14. 讨论MMU
  15. kafka实战kerberos
  16. abaqus2016安装过程中出现error:unable to add abaqus command directory to PATH variable
  17. python基础学习笔记 - 备忘
  18. Spring AOP 和 AspectJ
  19. 新装的arcgis10.5特别卡
  20. delphi 动态加载dll

热门文章

  1. 【Python 数据分析】jieba文本挖掘
  2. 【PHP】导入、导出Excel表格(有使用PHPExcel和不使用的两个版本)
  3. bootstrap学习笔记 多媒体对象
  4. jq 遍历元素 筛选
  5. SQL中的join操作总结(非常好)
  6. python selenum ---如何定位一组元素
  7. 使用Apache Jmeter进行并发压力测试
  8. linux下的ssh与ssh客户端
  9. firefox配置
  10. django的html模板中获取字典的值