手势交互:
点击屏幕(坐标,支持相对坐标)
d.click(x, y)
d.click(0.5, 0.5)
双击
d.double_click(x, y)
d.double_click(x, y, 1)
长按
d.long_click(x, y)
d.long_click(x, y, 5)
滑动(滑动屏幕)
d.swipe(sx, sy, ex, ey)
d.swipe(sx, sy, ex, ey, 0.5)
SwipeExt 扩展功能
d.swipe_ext("right")
d.swipe_ext("right", scale=0.9)
d.swipe_ext("right", box=(0, 0, 100, 100))
拖动(例:拖动某个APP)
d.drag(sx, sy, ex, ey)
d.drag(sx, sy, ex, ey, 0.5)
多个点连续滑动(典型案例:9宫格解锁)
从point(x0, y0) 滑动到point(x1, y1)然后滑动到point(x2, y2)...等等
d.swipe_points([(x0, y0), (x1, y1), (x2, y2)], 0.2))
d.swipe_points([(0.235, 0.456), (0.503, 0.449), (0.509, 0.601), (0.777, 0.603), (0.771, 0.763), (0.222, 0.75)], 0.2)
模拟按下 - 等待 - 移动 - 松开
d.touch.down(10, 10)
time.sleep(0.01)
d.touch.move(15, 15)
d.touch.up()
截图
d.screenshot(path)
元素选择器
d(text='Clock', className='android.widget.TextView')
后代元素
d(className="android.widget.ListView").child(text="Bluetooth")
兄弟姐妹,同级元素
d(text="Google").sibling(className="android.widget.ImageView")
通过多个层级多个属性定位
d(className="android.widget.ListView", resourceId="android:id/list").child_by_text("Bluetooth",
className="android.widget.LinearLayout")
可以由多个层级往下定位,例:
d(className="###", resourceId="###").child_by_text("Wi-Fi",
className="¥¥¥¥").child(className="****").click()
相对定位
d(A).left(B), 定位A左边的B
d(A).right(B), 定位A右边的B
d(A).up(B), 定位A上边的B
d(A).down(B), 定位A下边的B
多个同属性元素索引
如果元素选择器选择到了多个同属性的元素而无法进行更细致的区分,可以用索引选择指定的元素
d(text="Add new", instance=0)
其他操作:
d(text="Add new").count
len(d(text="Add new"))
d(text="Add new")[0]
迭代:
for view in d(text="Add new"):
view.info
获取所选ui对象的状态及其信息
1. 检查元素是否存在
d(text="Settings").exists
d(text="Settings").exists(timeout=3)
2. 输出指定元素的信息
d(text="Settings").info
3. 获取、输入、清除输入框文本
d(text="Settings").get_text()
d(text="Settings").set_text("My text...")
d(text="Settings").clear_text()
4. 获取指定元素中心点坐标
x, y = d(text="Settings").center()
x, y = d(text="Settings").center(offset=(0, 0))
5. 指定元素点击操作
d(text="Settings").click()
d(text="Settings").click(timeout=10)
d(text="Settings").click(offset=(0.5, 0.5))
clicked = d(text='Skip').click_exists(timeout=10.0)
is_gone = d(text="Skip").click_gone(maxretry=10, interval=1.0)
d(text="Settings").long_click()
特定元素的手势操作
1. 将元素拖向另一个点或另一个元素
备注:安卓4.3以下不能用
d(text="Settings").drag_to(x, y, duration=0.5)
d(text="Settings").drag_to(text="Clock", duration=0.25)
2. 等待元素出现或消失
d(text="Settings").wait(timeout=3.0)
d(text="Settings").wait_gone(timeout=1.0)
3、滚动屏幕
scroll()里面的参数steps默认是滑动一个屏幕的距离
a.向上滚动:d(scrollable=True).scroll(steps=10)
b.向下滑动:d(scrollable=True).scroll.vert.backward()
c.水平向右滚动:d(scrollable=True).scroll.horiz.forward(steps=50)
d.水平向左滚动:d(scrollable=True).scroll.horiz.backward(steps=50)
e.水平滑动到最左边:d(scrollable=True).scroll.horiz.toBeginning(steps=100, max_swipes=1000)
f.水平滑动到最右边:d(scrollable=True).scroll.horiz.toEnd(steps=100, max_swipes=1000)
g.竖直滑动到结尾:d(scrollable=True).scroll.toEnd()
h.竖直滑动到开头:d(scrollable=True).scroll.toBeginning(steps=50)
i.滑动到指定位置:d(scrollable=True).scroll.to(text="Security")
全局设置
d.click_post_delay = 1.5
d.wait_timeout = 30.0
设置元素查找等待时间(默认20s)
d.implicitly_wait(10.0)
输入内容
d.send_keys('str')
Toast
1、显示toast
d.toast.show("Hello world", 1.0)
2、获取toast
d.toast.get_message(5.0, 10.0, "default message")
assert "Short message" in d.toast.get_message(5.0, default="")
d.toast.reset()
xpath
description -> content-desc(xpath)
resourceId -> resource-id(xpath)
d.xpath("//android.widget.TextView").wait(10.0)
d.xpath("//*[@content-desc='分享']").click()
if d.xpath("//android.widget.TextView[contains(@text, 'Se')]").exists:
print("exists")
for elem in d.xpath("//android.widget.TextView").all():
print("Text:", elem.text)
print("Attrib:", elem.attrib)
print("Position:", elem.center())
def add_picture_to_report(filepath):
filebytes = ''
with open(filepath, 'rb') as f:
filebytes = f.read()
allure.attach(filebytes, '运行截图', attachment_type=allure.attachment_type.PNG)
def d_screenshot(self, screenshot_name):
""" 截图 """
screenshot_time = time.strftime(TIMEFORMAT, time.localtime())
filename = '%s_%s_%s_.png' % (screenshot_time, project_conf.PROJECT_SN, screenshot_name)
logging.info('current picture name is %s' % filename)
filepath = os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Screenshot'), 'AiHome')
if not os.path.exists(filepath):
os.makedirs(filepath)
picture_file = os.path.join(filepath, filename)
try:
self.d.screenshot(picture_file)
add_picture_to_report(picture_file)
logging.info('screenshot success')
except ScreenshotError:
logging.exception('screenshot failed')