uiautomator2常见的API操作

click(int x, int y) pressHome()
getCurrentActivityName() pressMenu()
getCurrentPackageName() takeScreenshot(File storePath)
getDisplayHeight()
getDisplayWighth()
isScreenOn()wakeUp()
pressBack()
swipe(int startX, int startY, int endX, int endY, int steps)

1.比如我们打开百度地图
1.根据文本定位
text(String text)
new UiSelector().text(“搜地点、查公交、找路线”)
2.模糊匹配文本
textContains(“公交”)
3.以某个文本开头来匹配
textStartsWith(“搜地点”)
4.根据resourceId定位元素:
resourceId(id)
new UiSelector().resourceId(“com.baidu.BaiduMap:id/tv_searchbox_home_text”)
5.根据className定位元素:
注意:className不是唯一的,在Android页面为元素的类型
new UiSelector().className(“android.widget.Button”)android.widget.TextView
6.根据contenet-des属性定位:
description(contenet-des属性值)
new UiSelector().description(“搜索地点、公交和路线”)

import uiautomator2 as u2;
from time import sleep;
d=u2.connect(‘172.24.188.20’);//连接你手机的ip地址
d.app_start(“com.baidu.BaiduMap”);//开始启动手机地图
sleep(4);
d(resourceId=“com.baidu.BaiduMap:id/tv_searchbox_home_text”).set_text(“北京天安门”); //输入北京天安门
d(resourceId=“com.baidu.BaiduMap:id/tvSearchButton”).click(); //点击查询
d(resourceId=“com.baidu.BaiduMap:classname/android.widget.ImageView”).click(); //点击导航按钮
sleep(14);
d.app_stop(“com.baidu.BaiduMap”);//停止百度地图app

猜你喜欢

转载自blog.csdn.net/weixin_37565521/article/details/106608762